BerriAI/litellm · error · ValueError

Missing CODESTRAL_API_Key - Please add CODESTRAL_API_Key to…

Error message

Missing CODESTRAL_API_Key - Please add CODESTRAL_API_Key to your environment variables

What it means

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.

Solutions

  1. export CODESTRAL_API_KEY=<your key> (La Plateforme console) or add it to your .env / deployment secrets
  2. Or pass the key directly: litellm.text_completion(model='text-completion-codestral', ..., api_key=key)
  3. Confirm the variable name is exactly CODESTRAL_API_KEY (not CODESTRAL_API_Key lowercase issues aside, env is case-sensitive on Linux)
  4. If you only have MISTRAL_API_KEY, pass it explicitly as api_key

Example fix

# before
litellm.text_completion(model="text-completion-codestral", prompt="Hello")
# ValueError: Missing CODESTRAL_API_Key

# after
os.environ["CODESTRAL_API_KEY"] = "your-key"
litellm.text_completion(model="text-completion-codestral", prompt="Hello")
Defensive patterns

Strategy: validation

Validate before calling

import os

api_key = os.environ.get("CODESTRAL_API_KEY") or os.environ.get("MISTRAL_API_KEY")
if not api_key:
    raise RuntimeError("CODESTRAL_API_KEY is not set; create a key at console.mistral.ai")

litellm.text_completion(model="text-completion-codestral", prompt=prompt, api_key=api_key)

Try / catch

try:
    litellm.text_completion(model="text-completion-codestral", prompt=prompt)
except ValueError as e:
    if "CODESTRAL_API_Key" in str(e):
        os.environ["CODESTRAL_API_KEY"] = load_key_from_vault()
        litellm.text_completion(model="text-completion-codestral", prompt=prompt)
    else:
        raise

Prevention

When it happens

Trigger: Invoking litellm text completion with model='text-completion-codestral' without CODESTRAL_API_KEY in the environment and without an explicit api_key argument.

Common situations: 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.

Understand the failure class

Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.

Related errors


AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18). Data as JSON: /api/errors/464bea29138ef134. Report an issue: GitHub.

Appendix: source

Thrown at litellm/llms/codestral/completion/handler.py:86

        api_key="",
        original_response=completion_stream,  # Pass the completion stream for logging
        additional_args={"complete_input_dict": data},
    )

    return completion_stream


class CodestralTextCompletion:
    def __init__(self) -> None:
        super().__init__()

    def _validate_environment(
        self,
        api_key: str | None,
        user_headers: dict,
    ) -> dict:
        if api_key is None:
            raise ValueError("Missing CODESTRAL_API_Key - Please add CODESTRAL_API_Key to your environment variables")
        headers = {
            "content-type": "application/json",
            "Authorization": f"Bearer {api_key}",
        }
        if user_headers is not None and isinstance(user_headers, dict):
            headers = {**headers, **user_headers}
        return headers

    def output_parser(self, generated_text: str):
        """
        Parse the output text to remove any special characters. In our current approach we just check for ChatML tokens.

        Initial issue that prompted this - https://github.com/BerriAI/litellm/issues/763
        """
        chat_template_tokens: Final = [
            "<|assistant|>",
            "<|system|>",
            "<|user|>",

View on GitHub (pinned to 77b7c6c40c)