Streaming Events
Typed server-sent events emitted by POST /v1/responses when stream: true is enabled.
When stream: true is set on POST /v1/responses, Aivene keeps the
connection open and pushes the response as it is generated using
server-sent events.
Unlike Chat Completions streaming,
every event is typed and the stream has no [DONE] sentinel: it closes after
a terminal event (response.completed, response.incomplete or
response.failed).
Enabling streaming
Set stream: true in the request body. The response arrives as
Content-Type: text/event-stream:
{
"model": "gpt-5.4",
"input": "Hi",
"stream": true
}Consuming the stream
The simplest path is the official openai SDK - each iteration item is a
parsed event:
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.AIVENE_API_KEY,
baseURL: 'https://api.aivene.com/v1'
});
const stream = await client.responses.create({
model: 'gpt-5.4',
input: 'Hi',
stream: true
});
for await (const event of stream) {
if (event.type === 'response.output_text.delta') {
process.stdout.write(event.delta);
} else if (event.type === 'response.completed') {
console.log(event.response.usage);
}
}import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["AIVENE_API_KEY"],
base_url="https://api.aivene.com/v1",
)
stream = client.responses.create(
model="gpt-5.4",
input="Hi",
stream=True,
)
for event in stream:
if event.type == "response.output_text.delta":
print(event.delta, end="", flush=True)
elif event.type == "response.completed":
print(event.response.usage)Wire format
Each event is an event: line with the event type, a data: line with the
JSON payload, and a blank line. The payload's type always equals the
event type:
event: response.created
data: {"type":"response.created","response":{"id":"avn_...","object":"response","created_at":1716825600,"status":"in_progress","error":null,"incomplete_details":null,"model":"gpt-5.4","output":[]}}
event: response.output_item.added
data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","id":"msg_...","status":"in_progress","role":"assistant","content":[]}}
event: response.output_text.delta
data: {"type":"response.output_text.delta","item_id":"msg_...","output_index":0,"content_index":0,"delta":"Hello"}
event: response.output_text.delta
data: {"type":"response.output_text.delta","item_id":"msg_...","output_index":0,"content_index":0,"delta":" there!"}
event: response.output_item.done
data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"msg_...","status":"completed","role":"assistant","content":[{"type":"output_text","text":"Hello there!","annotations":[]}]}}
event: response.completed
data: {"type":"response.completed","response":{"id":"avn_...","object":"response","created_at":1716825600,"status":"completed","error":null,"incomplete_details":null,"model":"gpt-5.4","output":[...],"usage":{"input_tokens":8,"output_tokens":4,"total_tokens":12}}}The response id is stable for the whole stream.
Stream lifecycle
response.created(and usuallyresponse.in_progress) - the response envelope withstatus: "in_progress"and emptyoutput.- One or more output items, each made of:
response.output_item.added- the item'soutput_indexand initial shape.- Delta events for its content (text, reasoning, or tool call arguments).
response.output_item.done- the complete item.
- A terminal event -
response.completed,response.incompleteorresponse.failed, carrying the full response withoutputandusage.
Event reference
| Event | Payload | Meaning |
|---|---|---|
response.created | response | The response has started. |
response.in_progress | response | The response is being generated. |
response.output_item.added | output_index, item | A new output item starts. |
response.output_item.done | output_index, item | The output item is complete. |
response.output_text.delta | item_id, output_index, content_index, delta | A fragment of answer text. |
response.refusal.delta | item_id, output_index, content_index, delta | A fragment of a refusal. |
response.reasoning_summary_text.delta | item_id, output_index, summary_index, delta | A fragment of a reasoning summary. |
response.reasoning_text.delta | item_id, output_index, content_index, delta | A fragment of reasoning text, on models that expose it. |
response.function_call_arguments.delta | item_id, output_index, delta | A fragment of a tool call's arguments. |
response.completed | response | Terminal. Generation finished normally. |
response.incomplete | response | Terminal. Generation stopped early; see response.incomplete_details.reason. |
response.failed | response | Terminal. Generation failed; see response.error. |
error | code, message, param | Terminal. The stream failed. |
Some models also send response.content_part.added,
response.content_part.done, response.output_text.done,
response.reasoning_summary_text.done and
response.function_call_arguments.done. Don't depend on them: rebuild
content from the delta events, and take complete items from
response.output_item.done or the terminal event's output.
Tool call streaming
A tool call starts with response.output_item.added carrying a
function_call item with empty arguments. Its arguments then stream as
response.function_call_arguments.delta events with the same item_id:
event: response.output_item.added
data: {"type":"response.output_item.added","output_index":1,"item":{"type":"function_call","id":"fc_...","call_id":"call_abc123","name":"get_weather","arguments":"","status":"in_progress"}}
event: response.function_call_arguments.delta
data: {"type":"response.function_call_arguments.delta","item_id":"fc_...","output_index":1,"delta":"{\"city\":"}
event: response.function_call_arguments.delta
data: {"type":"response.function_call_arguments.delta","item_id":"fc_...","output_index":1,"delta":"\"Tokyo\"}"}
event: response.output_item.done
data: {"type":"response.output_item.done","output_index":1,"item":{"type":"function_call","id":"fc_...","call_id":"call_abc123","name":"get_weather","arguments":"{\"city\":\"Tokyo\"}","status":"completed"}}Use the call_id from the item when you send the function_call_output
back.
Reasoning streaming
Reasoning models emit a reasoning item before the answer. With
reasoning.summary set, its summary text streams as
response.reasoning_summary_text.delta events. With
include: ["reasoning.encrypted_content"], the item on
response.output_item.done carries encrypted_content. Pass the whole
item back in the next request's input, unchanged. See
Reasoning.
Token usage in a streamed response
The terminal event's response.usage carries the token totals:
{
"input_tokens": 42,
"output_tokens": 128,
"total_tokens": 170,
"input_tokens_details": { "cached_tokens": 32 },
"output_tokens_details": { "reasoning_tokens": 64 }
}| Field | Meaning |
|---|---|
input_tokens | Tokens billed for the input, cached tokens included. |
output_tokens | Tokens generated, reasoning and tool call arguments included. |
total_tokens | input_tokens + output_tokens. |
input_tokens_details.cached_tokens | Subset of input_tokens served from prompt cache. Present when the model reports it. |
output_tokens_details.reasoning_tokens | Subset of output_tokens spent on reasoning. Present when the model reports it. |
Errors during a stream
A failure after the stream has started arrives in-band, in one of two ways.
A response.failed event, with the cause in response.error:
event: response.failed
data: {"type":"response.failed","response":{"id":"avn_...","object":"response","status":"failed","error":{"code":"content_filter","message":"The response was filtered due to the prompt triggering content management policy. Please modify your prompt and retry."},...}}Or an error event:
event: error
data: {"type":"error","code":"upstream_error","message":"The model is temporarily unavailable. Please try again later.","param":null}Either way the stream then closes. code is one of:
code | Meaning |
|---|---|
upstream_error | The model failed or the stream was cut. Safe to retry. |
content_filter | The model's safety filter blocked the request or output. |
context_length_exceeded | The request is too large for the model's context window. |
request_too_large | The request has more bytes of text than the model accepts. Shorten the conversation, attachments, or tool output. |
The OpenAI SDKs surface both as events in the iteration; handle them alongside the normal ones.