{"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/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/codestral/completion/handler.py#L68-L104","documentation":"CodestralTextCompletion._validate_environment requires an API key before any request is sent. If api_key is None (LiteLLM could not find CODESTRAL_API_KEY in the environment and none was passed per-call), it raises this ValueError immediately. The header building (Authorization: Bearer ...) never happens.","triggerScenarios":"Using a codestral/* text-completion model without CODESTRAL_API_KEY exported and without api_key passed to the call; also when the key was configured under a different name (e.g. MISTRAL_API_KEY) for the dedicated Codestral endpoint.","commonSituations":"The Codestral FIM endpoint uses a separate key from the general Mistral API; developers set MISTRAL_API_KEY only and hit this. Also common in fresh containers/CI where env vars were not injected.","solutions":["Export CODESTRAL_API_KEY with the key issued for the Codestral (FIM) endpoint.","Or pass api_key explicitly on the call.","If you intend to use the general Mistral chat endpoint instead, call the mistral/ model rather than the codestral text-completion route.","Note the case in the message: the variable name LiteLLM reads is CODESTRAL_API_KEY."],"exampleFix":"# before\ncompletion = litellm.text_completion(model=\"text-completion-codestral/codestral-latest\", prompt=\"def fib(\")\n\n# after\nos.environ[\"CODESTRAL_API_KEY\"] = \"...\"\ncompletion = litellm.text_completion(model=\"text-completion-codestral/codestral-latest\", prompt=\"def fib(\")","handlingStrategy":"validation","validationCode":"import os\n\nif not (os.environ.get(\"CODESTRAL_API_KEY\") or os.environ.get(\"MISTRAL_API_KEY\")):\n    raise SystemExit(\"Set CODESTRAL_API_KEY (separate from the Mistral chat key) for FIM models\")","typeGuard":null,"tryCatchPattern":"try:\n    resp = litellm.text_completion(model=\"text-completion-codestral/codestral-latest\", prompt=p)\nexcept ValueError as e:\n    if \"CODESTRAL_API_Key\" in str(e):\n        raise RuntimeError(\"Codestral key missing\") from e\n    raise","preventionTips":["Remember the Codestral FIM endpoint uses its own key, distinct from MISTRAL_API_KEY.","Inject secrets via the environment at process start, not mid-session.","Add an integration test exercising each configured provider with a minimal request."],"tags":["codestral","authentication","api-key","configuration"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}