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.
Diese Übung ist Teil des Kurses
Working with the OpenAI Responses API
Anleitung zur Übung
- 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.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
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)