ComenzarEmpieza gratis

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.

Este ejercicio forma parte del curso

Working with the OpenAI Responses API

Ver curso

Instrucciones del ejercicio

  • 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, append event.delta to current_text and print the accumulated text.

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

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)
Editar y ejecutar código