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.
Cet exercice fait partie du cours
Working with the OpenAI Responses API
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.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de 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)