Streaming with Semantic Events

Working with the OpenAI Responses API

James Chapman

AI Curriculum Manager, DataCamp

Why Stream?

streaming_off.gif

Working with the OpenAI Responses API

Why Stream?

streaming_on.gif

Working with the OpenAI Responses API

Semantic Events

  • Structured updates that describe what's happening
Working with the OpenAI Responses API

Semantic Events

  • Structured updates that describe what's happening
Event Type Description
response.created The model has started generating
response.output_text.delta Partial text update
response.output_text.done Text block complete
response.function_call.arguments.delta Streaming tool arguments
response.completed The entire response is finished
Working with the OpenAI Responses API

Example: Basic Text Streaming

prompt = "Explain the seasons of the year concisely to a child."

with client.responses.create(model="gpt-5-mini", input=prompt, stream=True) as stream:
current_text = ""
for event in stream: if event.type == "response.output_text.delta":
current_text += event.delta print(current_text)
Working with the OpenAI Responses API

Example: Basic Text Streaming

text_streaming.gif

Working with the OpenAI Responses API

Example: Handling Multiple Events

prompt = "Explain how a neural network learns concisely for a child."

with client.responses.create(model="gpt-5-mini", input=prompt, stream=True) as stream:

for event in stream: if event.type == "response.created":
print("Response started...\n")
elif event.type == "response.output_text.done":
print("\n\n--- Text block finished ---\n")
elif event.type == "response.completed":
print(f"\nFull response:\n{current_text}")
Working with the OpenAI Responses API

Example: Handling Multiple Events

multiple_events.gif

Working with the OpenAI Responses API

Example: Streaming Tool Events

convert_currency() -> str:

  • date_time: str
  • from_timezone: str
  • to_timezone: str
tools = [
    {
        "type": "function",
        "name": "convert_currency",
        ...
     }
]

frankfurter.png

Working with the OpenAI Responses API

Example: Streaming Tool Events

prompt = "How much is 120 euros in British pounds using the current exchange rate?"

with client.responses.create(model="gpt-5-mini", input=prompt, tools=tools, stream=True) as stream:
current_args = ""
for event in stream: if event.type == "response.function_call_arguments.delta":
current_args += event.delta print("Streaming args:", current_args)
elif event.type == "response.function_call_arguments.done":
print("\nFinal arguments:", event.arguments)
elif event.type == "response.completed":
print("\n--- Completed ---")
Working with the OpenAI Responses API

Example: Streaming Tool Events

streaming_tool_events.gif

Working with the OpenAI Responses API

Summary

chatgpt_semantic_events.gif

1 Semantic Events being used in ChatGPT
Working with the OpenAI Responses API

Let's practice!

Working with the OpenAI Responses API

Preparing Video For Download...