Appearance
错误码说明
错误响应格式
所有错误均以 JSON 格式返回:
json
{
"error": {
"message": "错误描述信息",
"type": "error_type_code",
"code": "error_code"
}
}常见错误码
4xx 客户端错误
| 状态码 | 错误类型 | 说明 | 解决方案 |
|---|---|---|---|
| 400 | invalid_request_error | 请求参数无效 | 检查请求格式和必填字段 |
| 401 | invalid_api_key | API Key 无效 | 确认 Key 正确且未过期 |
| 401 | insufficient_quota | 余额不足 | 充值后重试 |
| 403 | forbidden | 无权访问此资源 | 检查账户权限 |
| 404 | not_found | 资源不存在 | 检查 URL 和 model 名称 |
| 422 | unprocessable_entity | 参数无法处理 | 检查参数值是否合法 |
| 429 | rate_limit_exceeded | 请求频率超限 | 降低请求频率,添加退避逻辑 |
5xx 服务端错误
| 状态码 | 错误类型 | 说明 | 解决方案 |
|---|---|---|---|
| 500 | internal_error | 服务器内部错误 | 稍后重试,如持续请联系客服 |
| 502 | bad_gateway | 上游服务不可用 | 稍后重试 |
| 503 | service_unavailable | 服务暂时不可用 | 稍后重试 |
| 504 | gateway_timeout | 上游服务超时 | 减少请求长度或稍后重试 |
限流处理建议
当收到 429 错误时:
python
import time
import requests
def call_with_backoff(url, headers, payload, max_retries=5):
for attempt in range(max_retries):
resp = requests.post(url, headers=headers, json=payload)
if resp.status_code == 429:
wait = 2 ** attempt + random.random()
print(f"Rate limited, waiting {wait:.1f}s...")
time.sleep(wait)
continue
return resp
return resp