BerriAI/litellm · error · ValueError

Missing Azure AI API Key - A call is being made to Azure AI

Error message

Missing Azure AI API Key - A call is being made to Azure AI but no key is set either in the environment variables or via params

What it means

Raised by the Azure AI OCR (non-DI) path when no API key is available: neither an explicit api_key parameter nor AZURE_AI_API_KEY (AZURE_AI_OCR_API_KEY_ENV_VAR) is set. This handler authenticates with a Bearer token built from that key, so header construction aborts before any request. Note this is the azure_ai/ocr transformation, distinct from Document Intelligence which uses AZURE_DOCUMENT_INTELLIGENCE_API_KEY.

Source

Thrown at litellm/llms/azure_ai/ocr/transformation.py:57

        self,
        headers: dict,
        model: str,
        api_key: str | None = None,
        api_base: str | None = None,
        litellm_params: dict | None = None,
        **kwargs,
    ) -> dict:
        """
        Validate environment and return headers for Azure AI OCR.

        Azure AI uses Bearer token authentication with AZURE_AI_API_KEY.
        """
        # Get API key from environment if not provided
        if api_key is None:
            api_key = get_secret_str(AZURE_AI_OCR_API_KEY_ENV_VAR)

        if api_key is None:
            raise ValueError(
                "Missing Azure AI API Key - A call is being made to Azure AI but no key is set either in the environment variables or via params"
            )

        # Validate API base is provided
        if api_base is None:
            api_base = get_secret_str("AZURE_AI_API_BASE")

        if api_base is None:
            raise ValueError(
                "Missing Azure AI API Base - Set AZURE_AI_API_BASE environment variable or pass api_base parameter"
            )

        headers = {
            "Authorization": f"Bearer {api_key}",
            "Content-Type": "application/json",
            **headers,
        }

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Set AZURE_AI_API_KEY in the runtime environment (export it / add to container or .env).
  2. Or pass api_key explicitly on the OCR call.
  3. Confirm you're using the env vars matching the model route: AZURE_AI_API_KEY/AZURE_AI_API_BASE for azure_ai OCR vs AZURE_DOCUMENT_INTELLIGENCE_* for doc-intelligence.

Example fix

# before
os.environ["AZURE_DOCUMENT_INTELLIGENCE_API_KEY"] = "..."  # wrong var for azure_ai OCR
resp = litellm.aocr(model="azure_ai/<ocr-model>", document=doc)

# after
os.environ["AZURE_AI_API_KEY"] = "..."
os.environ["AZURE_AI_API_BASE"] = "https://my-resource.services.ai.azure.com"
resp = litellm.aocr(model="azure_ai/<ocr-model>", document=doc)
Defensive patterns

Strategy: validation

Validate before calling

import os
assert os.getenv("AZURE_AI_API_KEY"), "AZURE_AI_API_KEY not set for azure_ai OCR"

Try / catch

try:
    resp = litellm.aocr(model="azure_ai/<model>", document=doc)
except ValueError as e:
    if "Missing Azure AI API Key" in str(e):
        raise ConfigError("set AZURE_AI_API_KEY for the azure_ai OCR route") from e
    raise

Prevention

When it happens

Trigger: Invoking the Azure AI OCR model (not azure_ai/doc-intelligence/...) while AZURE_AI_API_KEY is unset and no api_key is passed in the call or litellm_params.

Common situations: Confusing the two Azure OCR env vars (setting the Document Intelligence ones for the Azure AI path or vice versa); env var missing in containers/serverless; variable set but not exported to the running process.

Related errors


AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15). Data as JSON: /api/errors/82f8c33bcf2af705. Report an issue: GitHub.