Standard HTTP Errors
All synchronous requests return errors using standard HTTP status codes (4xx/5xx) and a JSON body.Format
- type: Categorizes the error (e.g.,
invalid_request_error,insufficient_quota). - message: Describes the specific issue.
Common Status Codes
| Code | Type | Description |
|---|---|---|
| 400 | invalid_request_error | Invalid input (e.g., missing fields, bad syntax). |
| 401 | authentication_error | Invalid or missing API key. |
| 402 | insufficient_quota | Insufficient credits to perform the request. |
| 403 | inappropriate_content | Request blocked by content moderation policies. |
| 429 | rate_limit_exceeded | Too many requests. |
| 500 | server_error | Internal server error or upstream provider failure. |
Streaming Errors
When using Server-Sent Events (SSE) withstream=true, most requests either succeed or fail before the stream starts (you’ll get a normal HTTP 4xx/5xx response).
In rare cases, a request can fail after the initial 200 OK stream connection is established (for example, during late-stage generation). If you want more robust clients, you can also handle these “mid-stream” errors.
Architectural Overview
We strictly separate content generation from system errors:- Content: Returned in
choices[0].delta(OpenAI-compatible). - Errors: Returned as a top-level
errorobject.
This ensures clients can programmatically distinguish between text generation and failure signals without parsing the content itself.
Error Event Format
If an error occurs mid-stream, the API emits a final JSON event with theerror field and closes the connection.
In SSE, each event is terminated by a blank line. After emitting an
error event, the server closes the connection.Handling Late Failures (e.g., Image Generation)
Some requests, such as image generation withgemini-3-pro-image-preview, may fail late in the process due to content filtering or generation refusal.
Instead of a generic 500 error, these are mapped to specific error types to indicate why the request failed.
Example: Image Generation Refusal
If the model refused to generate the image (for example, the generated image did not pass the provider’s internal moderation), the stream will emit something like this: