Skip to main content

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:
Fields:
  • 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
See 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
Error type: 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)
Error type: authentication_error
Error 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
Error type: permission_error
Error 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_error
Error 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
Error type: 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
Error type: rate_limit_error
Error 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_error
Error 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_error
Error 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_error
Error 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
Error type: server_error
Error 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: 401
Codes: invalid_api_key
Defined in app/core/exceptions.py:21-24.

permission_error

The authenticated principal lacks permission for the requested operation. HTTP Status: 403
Codes: 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, 422
Codes: invalid_request_error, not_found

rate_limit_error

Rate limit has been exceeded. HTTP Status: 429
Codes: rate_limit_exceeded
Defined in app/core/exceptions.py:33-36.

server_error

An unexpected server-side error occurred. HTTP Status: 500, 501, 502, 503
Codes: server_error, internal_error, upstream_error, no_accounts, not_implemented
Defined in app/core/exceptions.py:39-42.

Error Code Reference

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 a response.failed event:
Defined in app/core/errors.py:52-76.

Payload Validation Errors

When request payload validation fails, the error includes the param field indicating which parameter is invalid:

Invalid Parameter Example

The 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:
  1. Parses the error envelope from the upstream response
  2. Forwards the status code (or maps to 502/503)
  3. Preserves error details (message, type, code)
  4. Adds rate limit headers to the response

Status Code Mapping

Special upstream error codes are mapped:
  • no_accounts503 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:
Defined in 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 the param field to identify which field failed validation:

Handle Streaming Errors

When using SSE streaming, watch for error events:

Next Steps