{"record":{"id":"464bea29138ef134","repo":"BerriAI/litellm","slug":"missing-codestral-api-key-please-add-codestral-a","errorCode":null,"errorMessage":"Missing CODESTRAL_API_Key - Please add CODESTRAL_API_Key to your environment variables","messagePattern":"Missing CODESTRAL_API_Key - Please add CODESTRAL_API_Key to your environment variables","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/codestral/completion/handler.py","lineNumber":86,"sourceCode":"        api_key=\"\",\n        original_response=completion_stream,  # Pass the completion stream for logging\n        additional_args={\"complete_input_dict\": data},\n    )\n\n    return completion_stream\n\n\nclass CodestralTextCompletion:\n    def __init__(self) -> None:\n        super().__init__()\n\n    def _validate_environment(\n        self,\n        api_key: str | None,\n        user_headers: dict,\n    ) -> dict:\n        if api_key is None:\n            raise ValueError(\"Missing CODESTRAL_API_Key - Please add CODESTRAL_API_Key to your environment variables\")\n        headers = {\n            \"content-type\": \"application/json\",\n            \"Authorization\": f\"Bearer {api_key}\",\n        }\n        if user_headers is not None and isinstance(user_headers, dict):\n            headers = {**headers, **user_headers}\n        return headers\n\n    def output_parser(self, generated_text: str):\n        \"\"\"\n        Parse the output text to remove any special characters. In our current approach we just check for ChatML tokens.\n\n        Initial issue that prompted this - https://github.com/BerriAI/litellm/issues/763\n        \"\"\"\n        chat_template_tokens: Final = [\n            \"<|assistant|>\",\n            \"<|system|>\",\n            \"<|user|>\",","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/codestral/completion/handler.py#L68-L104","documentation":"ValueError raised by CodestralTextCompletion._validate_environment when the api_key argument is None. The Codestral text-completion path (model text-completion-codestral) builds an Authorization: Bearer header from this key, so a missing key is a hard configuration error before any HTTP request is made.","triggerScenarios":"Invoking litellm text completion with model='text-completion-codestral' without CODESTRAL_API_KEY in the environment and without an explicit api_key argument.","commonSituations":"Using a Mistral API key under a different variable name; .env not loaded before the call; CI/production secret not provisioned; switching from the chat-completion codestral route (which uses MISTRAL_API_KEY) to the text-completion route.","solutions":["export CODESTRAL_API_KEY=<your key> (La Plateforme console) or add it to your .env / deployment secrets","Or pass the key directly: litellm.text_completion(model='text-completion-codestral', ..., api_key=key)","Confirm the variable name is exactly CODESTRAL_API_KEY (not CODESTRAL_API_Key lowercase issues aside, env is case-sensitive on Linux)","If you only have MISTRAL_API_KEY, pass it explicitly as api_key"],"exampleFix":"# before\nlitellm.text_completion(model=\"text-completion-codestral\", prompt=\"Hello\")\n# ValueError: Missing CODESTRAL_API_Key\n\n# after\nos.environ[\"CODESTRAL_API_KEY\"] = \"your-key\"\nlitellm.text_completion(model=\"text-completion-codestral\", prompt=\"Hello\")","handlingStrategy":"validation","validationCode":"import os\n\napi_key = os.environ.get(\"CODESTRAL_API_KEY\") or os.environ.get(\"MISTRAL_API_KEY\")\nif not api_key:\n    raise RuntimeError(\"CODESTRAL_API_KEY is not set; create a key at console.mistral.ai\")\n\nlitellm.text_completion(model=\"text-completion-codestral\", prompt=prompt, api_key=api_key)","typeGuard":null,"tryCatchPattern":"try:\n    litellm.text_completion(model=\"text-completion-codestral\", prompt=prompt)\nexcept ValueError as e:\n    if \"CODESTRAL_API_Key\" in str(e):\n        os.environ[\"CODESTRAL_API_KEY\"] = load_key_from_vault()\n        litellm.text_completion(model=\"text-completion-codestral\", prompt=prompt)\n    else:\n        raise","preventionTips":["Store CODESTRAL_API_KEY in the same secret manager slot as other provider keys","Validate required env vars in a boot hook for services using text completion","Remember the codestral text-completion path reads CODESTRAL_API_KEY, not MISTRAL_API_KEY"],"tags":["codestral","mistral","api-key","config","auth"],"backgroundTag":"missing-api-key","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}