MistralAPIException
mistral_sdk.exceptions.MistralAPIException
Stack trace
mistral_sdk.exceptions.MistralAPIException: API request failed with status code 400: {"error": "Invalid request payload"}
File "app.py", line 25, in <module>
response = client.completions.create(model="mistral-7b", prompt="Hello") # Error here
File "/usr/local/lib/python3.9/site-packages/mistral_sdk/client.py", line 102, in create
raise MistralAPIException(f"API request failed with status code {response.status_code}: {response.text}") Why it happens
This error happens when the Mistral SDK sends a request to the Mistral API that is malformed, missing required parameters, or violates API constraints. The API responds with an error status and message, which the SDK surfaces as MistralAPIException.
Detection
Catch MistralAPIException in your API call code and log the full error message and request payload to identify malformed or invalid requests before retrying.
Causes & fixes
Missing or invalid required parameters in the API request payload
Validate and include all required parameters exactly as specified in the Mistral API documentation before making the request.
Using an unsupported or misspelled model name in the request
Verify the model name against the official Mistral model list and correct any typos or unsupported models.
Expired or invalid API key causing authentication failure
Ensure your API key is valid, not expired, and correctly set in the environment variable before initializing the client.
Network issues or incorrect endpoint URL causing request failures
Check your network connectivity and confirm the Mistral API endpoint URL is correct and reachable.
Code: broken vs fixed
from mistral_sdk import Client
client = Client(api_key="wrong_or_missing_key")
response = client.completions.create(model="mistral-7b", prompt="Hello") # This line raises MistralAPIException
print(response) import os
from mistral_sdk import Client, MistralAPIException
client = Client(api_key=os.environ["MISTRAL_API_KEY"])
try:
response = client.completions.create(model="mistral-7b", prompt="Hello") # Fixed: valid key and model
print(response)
except MistralAPIException as e:
print(f"Mistral API error: {e}") # Log error for debugging Workaround
Wrap your API calls in try/except MistralAPIException, then parse the error message to extract details and retry with corrected parameters or after refreshing the API key.
Prevention
Implement strict input validation for all API request parameters and use environment variables for API keys. Monitor API responses and handle exceptions gracefully to avoid crashes.