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
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.
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 6c2dcb801b)
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.
Example fix
# before
completion = litellm.text_completion(model="text-completion-codestral/codestral-latest", prompt="def fib(")
# after
os.environ["CODESTRAL_API_KEY"] = "..."
completion = litellm.text_completion(model="text-completion-codestral/codestral-latest", prompt="def fib(") Defensive patterns
Strategy: validation
Validate before calling
import os
if not (os.environ.get("CODESTRAL_API_KEY") or os.environ.get("MISTRAL_API_KEY")):
raise SystemExit("Set CODESTRAL_API_KEY (separate from the Mistral chat key) for FIM models") Try / catch
try:
resp = litellm.text_completion(model="text-completion-codestral/codestral-latest", prompt=p)
except ValueError as e:
if "CODESTRAL_API_Key" in str(e):
raise RuntimeError("Codestral key missing") from e
raise Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Missing Cloudflare API Key - A call is being made to cloudfl
- GradientAI API key not found
- Error: {response.status_code} - {response.text}
- Missing Authorization header
- Invalid bearer token
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/464bea29138ef134.
Report an issue: GitHub.