OpenAI streaming
Read native streaming output incrementally and handle partial failures.
Set stream: true on a supported Chat Completions or Responses request. The response uses Server-Sent Events.
Python example
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["MUSESPARK_API_KEY"],
base_url="https://api.musespark.ai/proxy/openai/v1",
)
stream = client.chat.completions.create(
model="YOUR_AUTHORIZED_MODEL_ID",
messages=[{"role": "user", "content": "Describe a paper city."}],
stream=True,
)
for chunk in stream:
for choice in chunk.choices:
if choice.delta.content:
print(choice.delta.content, end="", flush=True)Parse the protocol
An SSE event can span multiple network chunks. Use an SSE parser or SDK instead of treating each HTTP read as a complete JSON object.
Chat chunks contain incremental deltas. A chunk may describe role, content, tool arguments, a finish reason, or usage rather than text. The gateway requests usage reporting for chat streams; clients must handle usage-only chunks with an empty choices list.
Responses uses typed events rather than the Chat Completions chunk structure. Select a parser for the operation being called.
Handle interruptions
An HTTP 200 response does not guarantee the stream finishes successfully. Handle error events, normal protocol completion, and transport interruptions separately.
Retain any request identity and partial output. An interrupted paid request can already have executed; do not automatically replay it with a new identity.