Errors
For errors, OpenRoute returns a JSON response with the following shape:
type ErrorResponse = {
error: {
code: number;
message: string;
metadata?: Record<string, unknown>;
};
};
The HTTP Response will have the same status code as error.code
, forming a request error if:
- Your original request is invalid
- Your API key/account is out of credits
Otherwise, the returned HTTP response status will be S200_OK
and any error occurred while the LLM is producing the output will be emitted in the response body or as an SSE data event.
Example code for printing errors in JavaScript:
const request = await fetch('https://api.openroute.cn/...');
console.log(request.status); // Will be an error code unless the model started processing your request
const response = await request.json();
console.error(response.error?.status); // Will be an error code
console.error(response.error?.message);
Error Codes
- 400: Bad Request (invalid or missing params, CORS)
- 401: Invalid credentials (OAuth session expired, disabled/invalid API key)
- 402: Your account or API key has insufficient credits. Add more credits and retry the request.
- 403: Your chosen model requires moderation and your input was flagged
- 408: Your request timed out
- 429: You are being rate limited
- 502: Your chosen model is down or we received an invalid response from it
- 503: There is no available model provider that meets your routing requirements
Moderation Errors
If your input was flagged, the error.metadata
will contain information about the issue. The shape of the metadata is as follows:
type ModerationErrorMetadata = {
reasons: string[]; // Why your input was flagged
flagged_input: string; // The text segment that was flagged, limited to 100 characters. If the flagged input is longer than 100 characters, it will be truncated in the middle and replaced with ...
provider_name: string; // The name of the provider that requested moderation
model_slug: string;
};
Provider Errors
If the model provider encounters an error, the error.metadata
will contain information about the issue. The shape of the metadata is as follows:
type ProviderErrorMetadata = {
provider_name: string; // The name of the provider that encountered the error
raw: unknown; // The raw error from the provider
};
When No Content is Generated
Occasionally, the model may not generate any content. This typically occurs when:
- The model is warming up from a cold start
- The system is scaling up to handle more requests
Warm-up times usually range from a few seconds to a few minutes, depending on the model and provider.
If you encounter persistent no-content issues, consider implementing a simple retry mechanism or trying again with a different provider or model that has more recent activity.
Additionally, be aware that in some cases, you may still be charged for the prompt processing cost by the upstream provider, even if no content is generated.
Streaming Error Formats
When using streaming mode (stream: true
), errors are handled differently depending on when they occur:
Pre-Stream Errors
Errors that occur before any tokens are sent follow the standard error format above, with appropriate HTTP status codes.
Mid-Stream Errors
Errors that occur after streaming has begun are sent as Server-Sent Events (SSE) with a unified structure that includes both the error details and a completion choice:
type MidStreamError = {
id: string;
object: 'chat.completion.chunk';
created: number;
model: string;
provider: string;
error: {
code: string | number;
message: string;
};
choices: [{
index: 0;
delta: { content: '' };
finish_reason: 'error';
native_finish_reason?: string;
}];
};
Example SSE data:
data: {"id":"cmpl-abc123","object":"chat.completion.chunk","created":1234567890,"model":"gpt-3.5-turbo","provider":"openai","error":{"code":"server_error","message":"Provider disconnected"},"choices":[{"index":0,"delta":{"content":""},"finish_reason":"error"}]}
Key characteristics:
- The error appears at the top level alongside standard response fields
- A
choices
array is included withfinish_reason: "error"
to properly terminate the stream - The HTTP status remains 200 OK since headers were already sent
- The stream is terminated after this event
OpenAI Responses API Error Events
The OpenAI Responses API (/api/alpha/responses
) uses specific event types for streaming errors:
Error Event Types
-
response.failed
- Official failure event{ "type": "response.failed", "response": { "id": "resp_abc123", "status": "failed", "error": { "code": "server_error", "message": "Internal server error" } } }
-
response.error
- Error during response generation{ "type": "response.error", "error": { "code": "rate_limit_exceeded", "message": "Rate limit exceeded" } }
-
error
- Plain error event (undocumented but sent by OpenAI){ "type": "error", "error": { "code": "invalid_api_key", "message": "Invalid API key provided" } }
Error Code Transformations
The Responses API transforms certain error codes into successful completions with specific finish reasons:
Error Code | Transformed To | Finish Reason |
---|---|---|
context_length_exceeded | Success | length |
max_tokens_exceeded | Success | length |
token_limit_exceeded | Success | length |
string_too_long | Success | length |
This allows for graceful handling of limit-based errors without treating them as failures.
API-Specific Error Handling
Different OpenRoute API endpoints handle errors in distinct ways:
OpenAI Chat Completions API (/api/v1/chat/completions
)
- No tokens sent: Returns standalone
ErrorResponse
- Some tokens sent: Embeds error information within the
choices
array of the final response - Streaming: Errors sent as SSE events with top-level error field
OpenAI Responses API (/api/alpha/responses
)
- Error transformations: Certain errors become successful responses with appropriate finish reasons
- Streaming events: Uses typed events (
response.failed
,response.error
,error
) - Graceful degradation: Handles provider-specific errors with fallback behavior
Error Response Type Definitions
// Standard error response
interface ErrorResponse {
error: {
code: number;
message: string;
metadata?: Record<string, unknown>;
};
}
// Mid-stream error with completion data
interface StreamErrorChunk {
error: {
code: string | number;
message: string;
};
choices: Array<{
delta: { content: string };
finish_reason: 'error';
native_finish_reason: string;
}>;
}
// Responses API error event
interface ResponsesAPIErrorEvent {
type: 'response.failed' | 'response.error' | 'error';
error?: {
code: string;
message: string;
};
response?: {
id: string;
status: 'failed';
error: {
code: string;
message: string;
};
};
}
Last updated on