Error Response Format
Codex-LB uses the OpenAI error envelope format for all API errors, ensuring compatibility with existing OpenAI SDK clients and tools.Standard Error Envelope
All errors are returned in this format:- message (string, required) - A human-readable description of the error
- type (string, required) - The type of error (e.g.,
authentication_error,invalid_request_error) - code (string, required) - A machine-readable error code
- param (string, optional) - The parameter that caused the error (for validation errors)
- plan_type (string, optional) - Plan type information (for rate limit errors)
- resets_at (number, optional) - Unix timestamp when rate limit resets
- resets_in_seconds (number, optional) - Seconds until rate limit resets
app/core/errors.py:7-14 for the complete error detail type definition.
HTTP Status Codes
Codex-LB uses standard HTTP status codes to indicate the outcome of API requests:Success Codes
- 200 OK - Request succeeded
- 201 Created - Resource created successfully (management API)
Client Error Codes (4xx)
400 Bad Request
Returned when the request payload is invalid or cannot be processed. Common causes:- Invalid JSON in request body
- Missing required parameters
- Invalid parameter values
- Unsupported transcription model
invalid_request_error
Example (from app/modules/proxy/api.py:118-120):
401 Unauthorized
Returned when authentication fails or credentials are missing/invalid. Common causes:- Missing Authorization header
- Invalid API key
- Expired API key
- Invalid ChatGPT token (for usage endpoint)
authentication_errorError code:
invalid_api_key
Example (from app/core/exceptions.py:21-24):
403 Forbidden
Returned when the authenticated user/key doesn’t have permission to access the requested resource. Common causes:- API key doesn’t have access to the requested model
- Insufficient permissions
permission_errorError code:
model_not_allowed
Example (from app/modules/proxy/api.py:562):
404 Not Found
Returned when the requested resource doesn’t exist. Error type:invalid_request_errorError code:
not_found
422 Unprocessable Entity
Returned when request validation fails (Pydantic validation errors). Common causes:- Type mismatch in request parameters
- Value out of acceptable range
- Enum value not recognized
invalid_request_error
Example (from app/core/handlers/exceptions.py:102-111):
429 Too Many Requests
Returned when rate limits are exceeded. Common causes:- API key request limit exceeded
- API key token limit exceeded
- API key cost limit exceeded
- Upstream rate limit from OpenAI
rate_limit_errorError code:
rate_limit_exceeded
Example (from app/modules/proxy/api.py:540):
Server Error Codes (5xx)
500 Internal Server Error
Returned when an unexpected error occurs on the server. Error type:server_errorError code:
server_error or internal_error
Example (from app/core/handlers/exceptions.py:159-161):
501 Not Implemented
Returned when an endpoint or feature is not implemented. Error type:server_errorError code:
not_implemented
Example (from app/modules/proxy/api.py:445-450):
502 Bad Gateway
Returned when the upstream OpenAI service returns an error. Error type:server_errorError code:
upstream_error
Example (from app/modules/proxy/api.py:606-613):
503 Service Unavailable
Returned when no accounts are available or the upstream service is unavailable. Common causes:- No ChatGPT accounts available in the pool
- All accounts are rate limited
- Upstream service temporarily unavailable
server_errorError code:
no_accounts or upstream_error
Example (from app/modules/proxy/api.py:294-295):
Error Type Reference
Codex-LB uses these error types, matching OpenAI’s convention:authentication_error
Credentials are missing, invalid, or expired. HTTP Status: 401Codes:
invalid_api_key
Defined in app/core/exceptions.py:21-24.
permission_error
The authenticated principal lacks permission for the requested operation. HTTP Status: 403Codes:
model_not_allowed, insufficient_permissions
Defined in app/core/exceptions.py:27-30.
invalid_request_error
The request is malformed or contains invalid parameters. HTTP Status: 400, 404, 422Codes:
invalid_request_error, not_found
rate_limit_error
Rate limit has been exceeded. HTTP Status: 429Codes:
rate_limit_exceeded
Defined in app/core/exceptions.py:33-36.
server_error
An unexpected server-side error occurred. HTTP Status: 500, 501, 502, 503Codes:
server_error, internal_error, upstream_error, no_accounts, not_implemented
Defined in app/core/exceptions.py:39-42.
Error Code Reference
| Code | Type | Status | Description |
|---|---|---|---|
invalid_api_key | authentication_error | 401 | API key is missing, invalid, or expired |
model_not_allowed | permission_error | 403 | API key doesn’t have access to the requested model |
insufficient_permissions | permission_error | 403 | Insufficient permissions for the operation |
rate_limit_exceeded | rate_limit_error | 429 | Rate limit exceeded (includes reset time in message) |
invalid_request_error | invalid_request_error | 400 | Request payload is invalid |
not_found | invalid_request_error | 404 | Requested resource not found |
validation_error | invalid_request_error | 422 | Request validation failed |
server_error | server_error | 500 | Unexpected internal server error |
internal_error | server_error | 500 | Unexpected internal error |
not_implemented | server_error | 501 | Feature not implemented |
upstream_error | server_error | 502/503 | Error from upstream OpenAI service |
no_accounts | server_error | 503 | No ChatGPT accounts available |
Streaming Error Format
When using Server-Sent Events (SSE) streaming, errors are sent as SSE events:Error Event
Response Failed Event
For streaming responses, failures are indicated with aresponse.failed event:
app/core/errors.py:52-76.
Payload Validation Errors
When request payload validation fails, the error includes theparam field indicating which parameter is invalid:
Invalid Parameter Example
param field uses dot notation for nested fields (e.g., messages.0.content).
Validation error handling is in app/modules/proxy/api.py:625-634.
Upstream Error Handling
When the upstream OpenAI service returns an error, Codex-LB:- Parses the error envelope from the upstream response
- Forwards the status code (or maps to 502/503)
- Preserves error details (message, type, code)
- Adds rate limit headers to the response
Status Code Mapping
Special upstream error codes are mapped:no_accounts→ 503 Service Unavailable (app/modules/proxy/api.py:294-295)- Other errors → 502 Bad Gateway (app/modules/proxy/api.py:660-663)
Default Error Envelope
If the upstream error cannot be parsed, a default error is returned:app/modules/proxy/api.py:606-613.
Error Handling Best Practices
Check Status Codes First
Always check the HTTP status code before parsing the response body:Handle Rate Limits Gracefully
For 429 errors, extract the reset time from the error message and implement exponential backoff:Retry on Server Errors
Implement retry logic for 5xx errors with exponential backoff:- 502 Bad Gateway - Retry after a short delay
- 503 Service Unavailable - Wait longer, as accounts may be temporarily unavailable
- 500 Internal Server Error - Retry, but consider it may be a persistent issue
Parse Validation Errors
For 422 errors, use theparam field to identify which field failed validation:
Handle Streaming Errors
When using SSE streaming, watch for error events:Next Steps
- Authentication - Resolve authentication errors
- Rate Limiting - Configure rate limits to avoid 429 errors
- Troubleshooting - Debug common issues