Skip to main content
Streaming lets you display model output to users as it is generated rather than waiting for the full response. Piramyd uses the standard Server-Sent Events (SSE) format — the same format used by OpenAI — so any SSE client or SDK that works with OpenAI works with Piramyd without modification. Both the chat completions and responses endpoints support streaming.

Chat Completions Streaming

Set stream: true in your request body to enable SSE output. Add stream_options: {"include_usage": true} to receive a final token-count chunk before the [DONE] sentinel.
The response body is a sequence of data: lines, each containing a JSON chunk:
Each chunk type signals a different phase:

Responses Endpoint Streaming

The POST /v1/responses endpoint emits structured named events instead of generic chat.completion.chunk objects. Each SSE line includes both an event: field and a data: field. This format maps closely to the OpenAI Responses API event schema. The full event sequence for a streaming response looks like this:
The response.completed event includes the full usage object — you do not need a separate stream_options flag on the responses endpoint.

Streaming Error Handling

Errors that occur mid-stream arrive as a data: event with an error key — not as an HTTP error status code. Your SSE parser must check for this shape on every chunk:
A minimal error-aware loop in Python looks like this:

Streaming Tool Calls

When a model returns a tool call during a streamed response, the function.arguments field arrives as a sequence of fragments across multiple chunks. You must accumulate all fragments before calling JSON.parse — individual chunks are not valid JSON on their own. Piramyd’s tool call integrity feature automatically buffers argument deltas and emits a single validated chunk, so the JSON you receive is always complete and well-formed. You still need to accumulate across chunks, but you will not receive truncated or malformed JSON.
Always include stream_options: {"include_usage": true} when streaming chat completions. The token count arrives in the final chunk before [DONE] and is the only reliable way to track usage during a streamed session.