Streaming OpenAI Responses
You're building a cooking assistant app that generates recipes in real-time. The OpenAI client has been initialized and configured for you. You'll use the Responses API to stream recipe generation, displaying the text as it arrives to create a dynamic user experience.
This exercise is part of the course
Working with the OpenAI Responses API
Exercise instructions
- Open a streaming context using
client.responses.create()with the model"gpt-5-mini"and the prompt provided. - Loop through the stream events and check if the event type is
"response.output_text.delta"; if it is, appendevent.deltatocurrent_textand print the accumulated text.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
prompt = "List the core ingredients to make classic egg pasta pasta in a single line."
# Open a connection for a streaming request
____ client.responses.create(model="gpt-5-mini", input=____, ____=____) as stream:
current_text = ""
# Complete the output text streaming
for event in stream:
if event.type == "____":
current_text += ____
print(current_text)