错误
错误由 HTTP 状态码和一个说明问题的 JSON 响应体组成。响应体采用你所调用的 API 的错误格式,所以各家的 SDK 会抛出它们一贯的异常。
大多数接口采用 OpenAI 的格式。只有这种格式带有 code,在你的代码里应该用它来判断错误类型。
{ "error": { "message": "Insufficient balance for this request. Add credit at https://exoticrouter.com/dashboard/billing/ or in Telegram with @ExoticRouterBot, or lower max_tokens.", "type": "insufficient_quota", "param": null, "code": "insufficient_balance" }}{ "type": "error", "error": { "type": "billing_error", "message": "Insufficient balance for this request. Add credit at https://exoticrouter.com/dashboard/billing/ or in Telegram with @ExoticRouterBot, or lower max_tokens." }}{ "error": { "code": 402, "message": "Insufficient balance for this request. Add credit at https://exoticrouter.com/dashboard/billing/ or in Telegram with @ExoticRouterBot, or lower max_tokens.", "status": "FAILED_PRECONDITION" }}三种格式里的消息内容相同。在 Anthropic 和 Gemini 格式里,类型由 HTTP 状态码决定:
| 状态码 | Anthropic error.type |
Gemini error.status |
|---|---|---|
| 400 | invalid_request_error |
INVALID_ARGUMENT |
| 401 | authentication_error |
UNAUTHENTICATED |
| 402 | billing_error |
FAILED_PRECONDITION |
| 404 | not_found_error |
NOT_FOUND |
| 413 | request_too_large |
INVALID_ARGUMENT |
| 429 | rate_limit_error |
RESOURCE_EXHAUSTED |
| 503 | overloaded_error |
UNAVAILABLE |
| 504 | timeout_error |
DEADLINE_EXCEEDED |
| 其他 | api_error |
INTERNAL |
| 状态码 | code |
发生了什么 | 怎么处理 |
|---|---|---|---|
| 400 | invalid_request |
请求格式不正确;消息里会指出具体字段 | 修正请求 |
| 400 | context_length_exceeded |
提示词超出了模型的上下文窗口 | 缩短输入,或调低 max_tokens |
| 400 | content_filter |
模型的内容过滤器拦截了请求 | 修改提示词 |
| 400 | request_rejected |
模型拒绝了这个请求,通常是因为某个参数不被支持 | 对照模型检查参数 |
| 401 | missing_api_key |
请求里没有密钥 | 带上密钥 |
| 401 | invalid_api_key |
密钥不存在、已停用或已删除 | 换用有效的密钥 |
| 402 | insufficient_balance |
余额不足以支付这个请求 | 充值,或调低 max_tokens |
| 404 | model_not_found |
模型不存在,或者该模型不支持这个接口 | 在列表里核对 ID 和它的 formats |
| 404 | not_found |
路径不存在,或者视频 id、文件链接无法打开 | 检查地址 |
| 405 | method_not_allowed |
路径存在,但不支持该请求方法 | 使用正确的请求方法 |
| 409 | not_ready |
视频尚未生成完成 | 查询状态并等待 |
| 413 | request_too_large |
请求对模型来说太大 | 减少发送的内容 |
| 429 | rate_limit_exceeded |
密钥超出了每分钟的请求数上限 | 等待 retry-after 指定的秒数后重试 |
| 500 | internal_error |
我们这边出了故障 | 重试 |
| 503 | model_unavailable |
模型暂时无法回答 | 等待 retry-after 指定的秒数后重试,或换用其他模型 |
| 503 | service_unavailable |
网关暂时无法访问 | 等待 retry-after 指定的秒数后重试 |
| 504 | timeout |
模型响应超时 | 重试;较长的回答请使用流式输出 |
- 模型自身服务对被拒绝请求给出的原始报错不会透传。你会收到上面某一个 400 错误和一条通用消息,所以
code是最好的线索,原因通常是某个参数该模型不支持。 - 在产生任何回答之前就返回错误的请求不会扣费。
- 所有 POST 请求的响应(包括错误)都带有
x-request-id响应头。
200 之后的错误
Section titled “200 之后的错误”回答一旦开始,状态码就无法再改变。此后发生的失败会在响应内容里报告:
| 位置 | 失败的表现 |
|---|---|
| Chat Completions 流 | 最后一个 data: 数据帧里是一个 error 对象,之后没有 [DONE] |
Messages、Responses 或 Gemini(alt=sse)流 |
最后一个名为 error 的事件,采用该接口自己的格式 |
不带 alt=sse 的 Gemini 流 |
JSON 数组提前中断,没有正常闭合 |
| 耗时较长的图像生成 | 状态码为 200,但响应体是 error 对象而不是 data |
data: {"id":"chatcmpl-8Zr0…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Sunlight is"}}]}
data: {"error":{"message":"The model is temporarily unavailable. Please retry shortly.","type":"api_error","param":null,"code":"model_unavailable"}}所以,没有以结束标记([DONE]、message_stop、response.completed)收尾的流就是没有完成。已经生成的部分会扣费。
哪些错误可以重试
Section titled “哪些错误可以重试”| 状态码 | 是否重试 |
|---|---|
| 429 | 是,等待 retry-after 指定的秒数之后 |
| 500, 503, 504 | 是,重试几次,每次等待的时间逐渐加长 |
| 400, 401, 402, 404, 405, 409, 413 | 否:同样的请求会以同样的方式失败 |
OpenAI 和 Anthropic 的 SDK 会自动对 429 和 5xx 状态码重试几次。503 到达你这里之前,网关已经对模型尝试过不止一次,所以如果连续出现多个 503,说明该模型暂时不可用:请换用其他模型。