Skip to main content

Stream a response

Pass stream=True to completion. The stream yields openai.types.chat.ChatCompletionChunk objects for every provider, Claude included.
With stream_options={"include_usage": True}, the final chunk carries usage and an empty choices list, exactly as OpenAI does. Check chunk.choices before indexing it.
Use the stream in a with block so the connection is released if you stop reading early.
Errors raised mid-stream, such as an overloaded provider, are mapped to the same typed exceptions as non-streaming calls. See Error handling.

Run async

acompletion takes exactly the same arguments as completion and returns the same types:

Stream async

With acompletion(..., stream=True), use async for and async with:

Fan out across models

Async calls make it easy to send the same prompt to several providers at once:
HTTP clients are cached per event loop and reuse pooled connections, so repeated calls skip the TLS handshake. Idle connections stay open for keepalive_expiry seconds (60 by default).