BerriAI/litellm · error · ValueError

Azure AI API key is required for model {model}. Set AZURE_AI

Error message

Azure AI API key is required for model {model}. Set AZURE_AI_API_KEY environment variable or pass api_key parameter.

What it means

The FLUX 2 image-edit transformation authenticates with an Api-Key header. It resolves the key via AzureFoundryModelInfo.get_api_key (parameter or AZURE_AI_API_KEY env var) and, if empty, raises ValueError telling you to set AZURE_AI_API_KEY or pass api_key. This is the Foundry (project) key, not an Azure AD token — FLUX endpoints on Foundry use key auth.

Source

Thrown at litellm/llms/azure_ai/image_edit/flux2_transformation.py:77

    def use_multipart_form_data(self) -> bool:
        """FLUX 2 uses JSON requests, not multipart/form-data."""
        return False

    def validate_environment(
        self,
        headers: dict,
        model: str,
        api_key: str | None = None,
        litellm_params: dict | None = None,
        api_base: str | None = None,
    ) -> dict:
        """
        Validate Azure AI Foundry environment and set up authentication
        """
        api_key = AzureFoundryModelInfo.get_api_key(api_key)

        if not api_key:
            raise ValueError(
                f"Azure AI API key is required for model {model}. Set AZURE_AI_API_KEY environment variable or pass api_key parameter."
            )

        headers.update(
            {
                "Api-Key": api_key,
                "Content-Type": "application/json",
            }
        )
        return headers

    def transform_image_edit_request(
        self,
        model: str,
        prompt: str | None,
        image: FileTypes | None,
        image_edit_optional_request_params: dict,
        litellm_params: GenericLiteLLMParams,

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Pass api_key set to the Foundry project key from the Azure AI Foundry 'Keys' page.
  2. Or export AZURE_AI_API_KEY in the litellm process environment (verify it is non-empty).
  3. In proxy config use api_key: os.environ/AZURE_AI_API_KEY on the flux deployment entry.
  4. Note FLUX routes take Api-Key auth — do not substitute a bearer/AD token.

Example fix

# before
litellm.image_edit(model='azure_ai/flux-2-dev', image=img, prompt='remove background')

# after
litellm.image_edit(
    model='azure_ai/flux-2-dev', image=img, prompt='remove background',
    api_key=os.environ['AZURE_AI_API_KEY'],
)
Defensive patterns

Strategy: validation

Validate before calling

import os

def flux2_key() -> str:
    key = os.getenv('AZURE_AI_API_KEY')
    if not key:  # empty string also fails the handler's truthiness check
        raise RuntimeError('AZURE_AI_API_KEY must be a non-empty Foundry key for FLUX image edit')
    return key

Try / catch

try:
    litellm.image_edit(model='azure_ai/flux-2-dev', image=img, prompt=p, api_key=flux2_key())
except ValueError as e:
    if 'Azure AI API key is required' in str(e):
        raise ConfigurationError(str(e)) from e
    raise

Prevention

When it happens

Trigger: litellm.image_edit(model='azure_ai/flux-2-...') without api_key and without AZURE_AI_API_KEY; passing an Azure AD token where the Foundry Api-Key is expected; env var empty string (falsy) which also triggers the raise.

Common situations: Same service used for chat with Azure AD auth and the key env var never set; AZURE_AI_API_KEY defined but blank in CI; confusion between AZURE_API_KEY (Azure OpenAI) and AZURE_AI_API_KEY (Foundry).

Related errors


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