AI / Core OpenAI Codex Application Fundamentals Interview Questions
What is streaming in the OpenAI API and how do you implement it?
Streaming delivers the model's output token by token as it is generated, rather than waiting for the complete response. This dramatically improves perceived performance in interactive applications by showing text appearing in real time.
from openai import OpenAI client = OpenAI() # Chat Completions streaming with client.chat.completions.stream( model="gpt-5.5", messages=[{"role": "user", "content": "Write a Python web scraper."}], ) as stream: for text in stream.text_stream: print(text, end="", flush=True) # Access final complete response: final = stream.get_final_completion() # Responses API streaming with client.responses.stream( model="gpt-5.5", input="Explain async/await in Python with examples.", ) as stream: for event in stream: # Events: response.created, response.in_progress, # response.output_item.added, response.content_part.delta, # response.completed, etc. if event.type == "response.output_text.delta": print(event.delta, end="", flush=True) final_response = stream.get_final_response() # Async streaming for FastAPI/async applications: async def stream_response(): async with client.responses.stream( model="gpt-5.5", input="Review this code: ...", ) as stream: async for text in stream.text_stream: yield text # SSE to browser
| Event | When it fires |
|---|---|
| response.created | Once at the start - response object initialised |
| response.output_text.delta | For each text token chunk as it arrives |
| response.output_item.added | When a new output item (text block, tool call) starts |
| response.completed | Once when the full response is finished |
Both APIs support streaming. The Responses API streaming events are more granular and expose tool call streaming - you can see tool call arguments being built token by token, enabling more sophisticated streaming UIs.
More Related questions...