BerriAI/litellm · error · ValueError

api_base is required for Azure OpenAI LLM provider. Either s

Error message

api_base is required for Azure OpenAI LLM provider. Either set it dynamically or set the AZURE_API_BASE environment variable.

What it means

ValueError raised on the Azure OpenAI chat path when no api_base resolves from the chain: api_base argument > litellm.api_base > AZURE_API_BASE environment variable. Azure requires the per-resource endpoint URL (https://<resource>.openai.azure.com/) to build the request, so an empty value aborts before any HTTP call.

Source

Thrown at litellm/main.py:1393

    api_version = ctx.api_version
    client: Final = _dispatch_client_azure(ctx)
    extra_headers: Final = ctx.extra_headers
    headers = ctx.headers
    litellm_params: Final = ctx.litellm_params
    logger_fn: Final = ctx.logger_fn
    logging: Final = ctx.logging
    messages: Final = ctx.messages
    model: Final = ctx.model
    model_response: Final = ctx.model_response
    optional_params: Final = ctx.optional_params
    timeout: Final = ctx.timeout

    api_type: Final = get_secret_str("AZURE_API_TYPE") or "azure"

    api_base = api_base or litellm.api_base or get_secret_str("AZURE_API_BASE")

    if api_base is None:
        raise ValueError(
            "api_base is required for Azure OpenAI LLM provider. Either set it dynamically or set the AZURE_API_BASE environment variable."
        )

    api_version = api_version or litellm.api_version or get_secret_str("AZURE_API_VERSION")

    api_key = (
        api_key
        or litellm.api_key
        or litellm.azure_key
        or get_secret_str("AZURE_OPENAI_API_KEY")
        or get_secret_str("AZURE_API_KEY")
    )

    azure_ad_token: Final = optional_params.get("extra_body", {}).pop("azure_ad_token", None) or get_secret_str(
        "AZURE_AD_TOKEN"
    )

    azure_ad_token_provider_value: Final = litellm_params.get("azure_ad_token_provider", None)

View on GitHub (pinned to 77b7c6c40c)

Solutions

  1. Export AZURE_API_BASE=https://<your-resource>.openai.azure.com/
  2. Or pass api_base='https://<resource>.openai.azure.com/' per call / in the Router model_list entry
  3. Confirm with a quick check: litellm.get_secret_str('AZURE_API_BASE') returns the URL in the failing environment
  4. If using azure-ad auth, still set api_base -- the endpoint URL is required regardless of auth mode

Example fix

# before
resp = litellm.completion(model='azure/my-deploy', messages=m)  # ValueError

# after
import os
os.environ['AZURE_API_BASE'] = 'https://my-resource.openai.azure.com/'
os.environ['AZURE_API_KEY'] = '...'
resp = litellm.completion(model='azure/my-deploy', messages=m)
Defensive patterns

Strategy: validation

Validate before calling

import os
api_base = os.getenv('AZURE_API_BASE') or litellm.api_base
if not api_base:
    raise SystemExit('AZURE_API_BASE not set; refusing to call azure models')

Type guard

def azure_base_resolved(api_base: str | None) -> bool:
    return isinstance(api_base, str) and api_base.startswith('https://')

Try / catch

try:
    resp = litellm.completion(model='azure/my-deploy', messages=m)
except ValueError as e:
    if 'api_base is required for Azure' in str(e):
        raise RuntimeError('Azure endpoint unconfigured: set AZURE_API_BASE') from e
    raise

Prevention

When it happens

Trigger: completion(model='azure/<deployment>') (or azure/ provider routing) with none of: api_base kwarg, litellm.api_base set, AZURE_API_BASE exported.

Common situations: Local script works (env set in shell) but CI/container/lambda lacks AZURE_API_BASE; key rotation scripts set only AZURE_API_KEY and forget the base; deploying with azure-ad token auth but no endpoint URL configured.

Related errors


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