Skip to main content
This example calls asyncio.run() twice while reusing one agent. OpenAIResponses caches an async client bound to the first event loop, so the second async call can fail with RuntimeError: Event loop is closed. Use the one-loop replacement below.
basic.py

Run the Example

1

Set up your virtual environment

2

Install dependencies

3

Export your OpenAI API key

4

Use one async event loop

Replace both asyncio.run(...) lines with one async def main() that awaits the non-streaming call followed by the streaming call, then invoke asyncio.run(main()) once.
5

Run the example

Save the code above as basic.py, then run:
Full source: cookbook/90_models/openai/responses/basic.py