OPENAI WORKFLOW · 05 / 10

Stream OpenAI Responses API text in Python

Display text deltas while tracking whether the response actually completes.

Documentation checked · Loomwell editorial guide · Examples have not been live API tested.

Steps

  1. Pass stream=True to receive typed events. Handle text deltas separately from lifecycle events.
  2. Treat interrupted or failed streams as unfinished work even if some text has already appeared.

Example

Python · Python API examples require OPENAI_API_KEY and OPENAI_MODEL in your server environment. API calls may incur OpenAI charges.

import os
from openai import OpenAI

stream = OpenAI().responses.create(
    model=os.environ["OPENAI_MODEL"], input="Explain HTTP streaming.", stream=True,
)
completed = False
for event in stream:
    if event.type == "response.output_text.delta":
        print(event.delta, end="", flush=True)
    elif event.type == "response.completed":
        completed = True
    elif event.type in {"error", "response.failed", "response.incomplete"}:
        raise RuntimeError("Response stream did not complete")
if not completed:
    raise RuntimeError("Stream ended without completion")

Check the result

Expect incremental text and a completion event. In a web app, preserve an explicit failed or interrupted state instead of silently saving partial text as a complete answer.

Official source

Streaming API responses

Check current provider documentation and model support before deployment. This guide is separate from the approved evidence records listed in the catalog.

Use Loomwell with your coding agent

Inspect the published record coverage before starting a trial or connecting MCP.