{"library":"mistralai","title":"Mistral AI Python Client","description":"The `mistralai` library is the official Python Client SDK for interacting with the Mistral AI API, providing programmatic access to Mistral's large language models and other AI services. It is currently at version 2.3.0 and actively maintained with regular updates and feature enhancements.","status":"active","version":"2.3.0","language":"en","source_language":"en","source_url":"https://github.com/mistralai/client-python.git","tags":["AI","LLM","Mistral AI","client","NLP","generative AI"],"install":[{"cmd":"pip install mistralai","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Minimum required Python version for the SDK.","package":"python","optional":false},{"reason":"Used internally for making HTTP API calls.","package":"httpx","optional":false}],"imports":[{"note":"Starting with v2.x, the main client class was moved into the `client` submodule. Direct import from `mistralai` will result in an `AttributeError`.","wrong":"from mistralai import Mistral","symbol":"Mistral","correct":"from mistralai.client import Mistral"}],"quickstart":{"code":"import os\nfrom mistralai.client import Mistral\n\n# Ensure MISTRAL_API_KEY environment variable is set\napi_key = os.environ.get('MISTRAL_API_KEY', '')\n\nif not api_key:\n    print(\"Error: MISTRAL_API_KEY environment variable not set.\")\n    print(\"Please set it using: export MISTRAL_API_KEY='your_api_key_here'\")\nelse:\n    try:\n        client = Mistral(api_key=api_key)\n        chat_response = client.chat.complete(\n            model=\"mistral-small-latest\",\n            messages=[\n                {\"role\": \"user\", \"content\": \"What is the capital of France?\"}\n            ]\n        )\n        print(chat_response.choices[0].message.content)\n    except Exception as e:\n        print(f\"An error occurred: {e}\")","lang":"python","description":"This quickstart initializes the Mistral client and performs a simple chat completion request. It assumes the `MISTRAL_API_KEY` environment variable is set for authentication. The response content is then printed."},"warnings":[{"fix":"Update your imports to `from mistralai.client import Mistral`.","message":"The import path for the `Mistral` client class changed significantly from v1.x to v2.x. Old code importing `from mistralai import Mistral` will fail.","severity":"breaking","affected_versions":"2.0.0 and later"},{"fix":"Refer to the official migration guide (e.g., `MIGRATION.md` on GitHub) to adapt your code to the new response payload structure. For chat completions, content is now often accessed via `response.choices[0].message.content`.","message":"The structure of response objects, specifically `response.choices[]`, changed in v2.x for methods like `mistral.chat.complete()`, `mistral.fim.complete()`, and `mistral.agents.complete()`.","severity":"breaking","affected_versions":"2.0.0 and later"},{"fix":"Set `export MISTRAL_API_KEY='your_api_key_here'` in your environment or explicitly pass `api_key='your_api_key_here'` during client initialization.","message":"API Key (`MISTRAL_API_KEY`) is crucial for authentication. Not setting it as an environment variable or passing it directly to the client will lead to authentication errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure that the data you send in API requests (e.g., messages, tool definitions) strictly adheres to the format specified in the Mistral AI API documentation.","message":"Incorrect data serialization when sending requests to the API can lead to 'Data Serialization Error'.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Implement robust error handling, including retries with exponential backoff for transient errors (429). For 413, review prompt length and model context window limits. Ensure API key is valid for 401 errors.","message":"Common API errors include 401 (Unauthorized), 413 (Payload Too Large/Context Window Overflow), and 429 (Rate Limit Exceeded).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Update to newer, recommended models for continued support and improved performance. Check the latest model availability via the API or official documentation.","message":"Several older models, such as `open-mistral-7b`, `mistral-tiny`, `mistral-medium-2312`, and certain dated model aliases, have been deprecated.","severity":"deprecated","affected_versions":"Refer to Mistral AI's official model documentation for specific deprecation dates."}],"env_vars":null,"last_verified":"2026-04-05T00:00:00.000Z","next_check":"2026-07-04T00:00:00.000Z"}