Skip to content
Developer API3 min read

Read streams and handle errors

Streaming API responses use server-sent events. Preserve partial data between network reads and identify complete event boundaries.

Read the event name and data according to the deployed endpoint’s actual response format. Do not assume a TCP or HTTP read delivers exactly one event. Preserve unfinished lines and events across reads, decode text incrementally, and parse JSON only after receiving a complete payload.

For /api/v1/responses, process response.output_text.delta and a terminal response.completed, response.incomplete or response.failed. Chat Completions uses compatible chunks; Messages uses message/content-block events. These are different wire formats, not interchangeable parsers.

Treat terminal success, incomplete output and failure as separate outcomes. HTTP 200 or the first text chunk does not establish successful completion. A transport disconnect does not establish whether the server finished generating or settled usage. Use request IDs, logs and supported idempotency replay instead of blindly submitting another paid request.

Status Reviewed cause Response
400 Invalid request or unsupported parameter Compare the body with the deployed format/schema
401 Invalid, revoked, or expired key Check credentials and key status
402 Insufficient funding or spend cap Check the approved funding source and budget
403 Account, scope, tier or model restriction Check audience/account approval and key/project permissions
404 Unpublished/unknown model or API disabled Check the current contract and discovery response
410 Legacy key endpoint retired Move /api/chat/* key calls to /api/v1
429 Key/account rate or concurrency limit Read limit_source and respect Retry-After
500 Key validation or service failure Record the response and retry cautiously if appropriate
503 Stale/unavailable control plane or trust-rate dependency Record the request ID; use bounded retries without disabling protections

Live trust concurrency normally returns Retry-After: 2; trust RPM and 503 rate_limit_unavailable return Retry-After: 5. Apply bounded backoff/jitter and an overall deadline. Changing keys does not bypass an account-wide cap.

Errors can also occur inside an established stream. Handle them instead of displaying a partial answer as successful completion. Avoid automatic repeated retries of a generation request without considering duplicate work and usage.

For a support report, include the endpoint, response status, request identifier if supplied, and a redacted example body. Never include the raw API key.

Structured mock output does not establish live structured-output support.