HKUDS/DeepTutor · error · ValueError

Azure OpenAI api_key is required

Error message

Azure OpenAI api_key is required

What it means

AzureOpenAIProvider.__init__ requires a non-empty api_key because Azure OpenAI rejects anonymous requests — unlike some local endpoints, there is no keyless mode.

Source

Thrown at deeptutor/services/llm/provider_core/azure_openai_provider.py:84


class AzureOpenAIProvider(LLMProvider):
    """Azure OpenAI provider using the Responses API."""

    def __init__(
        self,
        api_key: str = "",
        api_base: str = "",
        default_model: str = "gpt-5.2-chat",
        extra_headers: dict[str, str] | None = None,
        api_version: str | None = None,
    ):
        super().__init__(api_key, api_base)
        self.default_model = default_model
        self.extra_headers = extra_headers or {}

        if not api_key:
            raise ValueError("Azure OpenAI api_key is required")
        if not api_base:
            raise ValueError("Azure OpenAI api_base is required")

        base_url = normalize_azure_base_url(api_base)

        # Azure authenticates API keys through ``api-key``; the SDK only sends
        # ``Authorization: Bearer``, which the service reserves for Entra tokens.
        headers = {"x-session-affinity": uuid.uuid4().hex, "api-key": api_key}
        if extra_headers:
            headers.update(extra_headers)

        # The ``/openai/v1`` surface supersedes ``?api-version=``, so a classic
        # dated version configured for the probe's URL would be rejected here.
        # Only ``preview`` is forwarded, since Azure still gates preview-only
        # Responses features behind it.
        default_query = (
            {"api-version": "preview"} if (api_version or "").strip().lower() == "preview" else None
        )

View on GitHub (pinned to 3e82f13042)

Solutions

  1. Set the Azure OpenAI API key in Settings > Catalog (or the mapped env var) and re-instantiate the provider.
  2. Confirm you're using an Azure key, not an OpenAI platform sk- key.
  3. Check that config loading isn't silently dropping the key field.

Example fix

# before
prov = AzureOpenAIProvider(api_key=None, api_base='https://x.openai.azure.com/')
# after
prov = AzureOpenAIProvider(api_key=os.environ['AZURE_OPENAI_API_KEY'], api_base='https://x.openai.azure.com/')
Defensive patterns

Strategy: validation

Validate before calling

import os

def azure_key_present() -> bool:
    return bool(os.getenv('AZURE_OPENAI_API_KEY'))

Try / catch

try:
    prov = AzureOpenAIProvider(api_key=key, api_base=base)
except ValueError as e:
    if 'api_key' in str(e):
        key = os.environ['AZURE_OPENAI_API_KEY']; prov = AzureOpenAIProvider(key, base)
    else:
        raise

Prevention

When it happens

Trigger: Constructing AzureOpenAIProvider with api_key=None/'' — e.g. the catalog/settings entry for the Azure provider lacks the key, or the env var it maps from is unset.

Common situations: AZURE_OPENAI_API_KEY missing from the environment; provider spec created from a template without filling the key; key stored under the wrong settings key name.

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 HKUDS/DeepTutor@3e82f13042 (2026-08-27). Data as JSON: /api/errors/29261f566c0c50fd. Report an issue: GitHub.