Error Codes
Comprehensive guide to MX4 Atlas API error codes and how to handle them.
Error Response Format
All API errors follow a consistent JSON format. The error object contains an error code, message, and optional details.
1{2 "error": {3 "code": "invalid_request_error",4 "message": "The request is not valid.",5 "details": {6 "field": "model",7 "issue": "Model not found"8 }9 }10}
Common Error Codes
400 - Bad Request
invalid_request_errorThe request was malformed or contained invalid parameters.
401 - Unauthorized
authentication_errorAuthentication failed or API key is invalid.
403 - Forbidden
permission_deniedAccess to the requested resource is forbidden.
404 - Not Found
not_found_errorThe requested resource was not found.
429 - Too Many Requests
rate_limit_errorRate limit exceeded. See Rate Limits for details.
500 - Internal Server Error
internal_errorAn unexpected error occurred on our servers.
503 - Service Unavailable
service_unavailableThe service is temporarily unavailable.
Error Response Examples
Here are complete error response examples for common scenarios:
Authentication Error
1HTTP/1.1 401 Unauthorized2Content-Type: application/json34{5 "error": {6 "code": "authentication_error",7 "message": "Invalid API key provided",8 "details": {9 "reason": "api_key_invalid"10 }11 }12}
Bad Request Error
1HTTP/1.1 400 Bad Request2Content-Type: application/json34{5 "error": {6 "code": "invalid_request_error",7 "message": "Missing required field: model",8 "details": {9 "field": "model",10 "issue": "required_field_missing"11 }12 }13}
Error Handling Best Practices
Implement robust error handling in your applications to gracefully manage API failures.
1import openai2from openai import OpenAI34client = OpenAI(5 api_key="mx4-sk-...",6 base_url="https://api.mx4.ai/v1"7)89def handle_api_errors():10 try:11 response = client.chat.completions.create(12 model="mx4-atlas-core",13 messages=[{"role": "user", "content": "مرحبا"}]14 )15 return response16 except openai.AuthenticationError:17 print("Authentication failed. Check your API key.")18 return None19 except openai.RateLimitError:20 print("Rate limit exceeded. Please wait before retrying.")21 return None22 except openai.BadRequestError as e:23 print(f"Bad request: {e}")24 return None25 except openai.InternalServerError:26 print("Internal server error. Please try again later.")27 return None28 except Exception as e:29 print(f"Unexpected error: {e}")30 return None
Advanced Error Handling Pattern
1import openai2import time3import logging45client = openai.OpenAI(api_key="mx4-sk-...")6logger = logging.getLogger(__name__)78async def make_api_call_with_logging(messages, max_retries=3):9 for attempt in range(max_retries):10 try:11 response = client.chat.completions.create(12 model="mx4-atlas-core",13 messages=messages14 )15 logger.info(f"API call successful on attempt {attempt + 1}")16 return response1718 except openai.AuthenticationError as e:19 logger.error(f"Authentication error: {e}")20 raise e # Don't retry auth errors2122 except openai.RateLimitError as e:23 if attempt == max_retries - 1:24 logger.error(f"Rate limit exceeded after {max_retries} retries")25 raise e26 wait_time = 2 ** attempt27 logger.warning(f"Rate limited. Retrying in {wait_time}s (attempt {attempt + 1})")28 await asyncio.sleep(wait_time)2930 except openai.InternalServerError as e:31 if attempt == max_retries - 1:32 logger.error(f"Server error after {max_retries} retries: {e}")33 raise e34 wait_time = 5 * (attempt + 1)35 logger.warning(f"Server error. Retrying in {wait_time}s")36 time.sleep(wait_time)3738 except Exception as e:39 logger.error(f"Unexpected error: {e}")40 raise e
Error Prevention Guide
Validate Inputs
Check request parameters before sending to API to catch 400 errors early.
Secure API Keys
Store keys in environment variables, rotate regularly, never commit to version control.
Monitor Rate Limits
Check rate limit headers and implement queue systems to stay within limits.
Handle Timeouts
Set appropriate request timeouts and implement graceful fallback mechanisms.
Tip: Always check the error code and message in the response. For persistent issues, contact support with the full error details and request ID from your logs.