Minimal code
What your loop will see
What to tune
Common mistakes
- Forgetting
flush=Truein Python. Without it stdout buffers and the tokens arrive in clumps. - Treating an error frame as a network failure. A stream that dies upstream ends with one final frame carrying the standard error envelope, on the same HTTP 200. Handle it inside your frame loop.
- Breaking the loop too early. The final content frame carries
finish_reasonand an emptydelta.content. Keep reading until the stream closes. - Streaming through a buffering proxy. Some edges buffer server-sent events. Stream direct, or disable buffering on that layer.
- Mixing
n > 1with streaming. Allowed, but every delta carries its ownchoices[].index. Route by index or the outputs interleave.
Next steps
Stream reference
Exact frame shapes, and the usage frame.
Tool calling
Let the model act, then answer.
Structured output
Get JSON that matches your schema.