Skip to main content

Streaming Responses

The OpenAI Python SDK provides support for streaming responses using Server-Side Events (SSE). This allows you to receive model output in real-time as it’s generated, rather than waiting for the entire response to complete.

Basic Streaming

To enable streaming, set the stream parameter to True when calling client.responses.create():

Stream Events

When streaming is enabled, the API returns a Stream[ResponseStreamEvent] object. Each event in the stream represents a different type of update:

Event Types

The stream emits various event types to communicate the progress of the response:
  • response.created - Initial event when the response starts
  • response.in_progress - Response is being generated
  • response.output_item_added - A new output item was added
  • response.content_part_added - A new content part was added
  • response.text_delta - Text content delta (incremental update)
  • response.text_done - Text content is complete
  • response.output_item_done - An output item is complete
  • response.completed - Response generation is complete
  • response.failed - Response generation failed
  • response.error - An error occurred
  • response.incomplete - Response is incomplete

Tool Call Events

When using tools, additional events are emitted:
  • response.function_call_arguments_delta - Function call arguments delta
  • response.function_call_arguments_done - Function call arguments complete
  • response.web_search_call_searching - Web search in progress
  • response.web_search_call_completed - Web search completed
  • response.file_search_call_searching - File search in progress
  • response.file_search_call_completed - File search completed
  • response.code_interpreter_call_interpreting - Code interpreter running
  • response.code_interpreter_call_code_delta - Code delta
  • response.code_interpreter_call_code_done - Code complete
  • response.code_interpreter_call_completed - Code interpreter completed

Reasoning Events

For reasoning models (o-series and gpt-5):
  • response.reasoning_text_delta - Reasoning text delta
  • response.reasoning_text_done - Reasoning text complete
  • response.reasoning_summary_part_added - Reasoning summary part added
  • response.reasoning_summary_text_delta - Reasoning summary text delta
  • response.reasoning_summary_text_done - Reasoning summary text complete
  • response.reasoning_summary_part_done - Reasoning summary part complete

Async Streaming

The async client uses the same interface:

Processing Text Deltas

Here’s an example of processing only the text content as it streams:

Handling Different Event Types

Stream Options

You can configure streaming behavior using the stream_options parameter:

Streaming with Tools

Error Handling

Best Practices

  1. Use flush=True - When printing text deltas, use flush=True to ensure immediate output:
  2. Handle all event types - Make sure to handle different event types gracefully, especially error events.
  3. Close streams properly - When using context managers or manual stream handling, ensure streams are properly closed to free up resources.
  4. Monitor token usage - Use stream_options to include usage information in the final event to track costs.
  5. Implement timeouts - Set appropriate timeouts to prevent indefinite waiting:

Return Type

When stream=True, the method returns:
  • Sync: Stream[ResponseStreamEvent]
  • Async: AsyncStream[ResponseStreamEvent]
When stream=False or omitted, the method returns:
  • Response - The complete response object

See Also