microsoft/autogen · error · ValueError

credential is required for AzureAIChatCompletionClient

Error message

credential is required for AzureAIChatCompletionClient

What it means

Raised by AzureAIChatCompletionClient._validate_config when kwargs lack 'credential'. The client supports AzureKeyCredential or any AsyncTokenCredential (e.g. DefaultAzureCredential) and must have one to authenticate to Azure AI Foundry or GitHub Models.

Source

Thrown at python/packages/autogen-ext/src/autogen_ext/models/azure/_azure_ai_client.py:304


    """

    def __init__(self, **kwargs: Unpack[AzureAIChatCompletionClientConfig]):
        config = self._validate_config(kwargs)  # type: ignore
        self._model_info = config["model_info"]  # type: ignore
        self._client = self._create_client(config)
        self._create_args = self._prepare_create_args(config)

        self._actual_usage = RequestUsage(prompt_tokens=0, completion_tokens=0)
        self._total_usage = RequestUsage(prompt_tokens=0, completion_tokens=0)

    @staticmethod
    def _validate_config(config: Dict[str, Any]) -> AzureAIChatCompletionClientConfig:
        if "endpoint" not in config:
            raise ValueError("endpoint is required for AzureAIChatCompletionClient")
        if "credential" not in config:
            raise ValueError("credential is required for AzureAIChatCompletionClient")
        if "model_info" not in config:
            raise ValueError("model_info is required for AzureAIChatCompletionClient")
        validate_model_info(config["model_info"])
        if _is_github_model(config["endpoint"]) and "model" not in config:
            raise ValueError("model is required for when using a Github model with AzureAIChatCompletionClient")
        return cast(AzureAIChatCompletionClientConfig, config)

    @staticmethod
    def _create_client(config: AzureAIChatCompletionClientConfig) -> ChatCompletionsClient:
        # Only pass the parameters that ChatCompletionsClient accepts
        # Remove 'model_info' and other client-specific parameters
        client_config = {k: v for k, v in config.items() if k not in ("model_info",)}
        return ChatCompletionsClient(**client_config)  # type: ignore

    @staticmethod
    def _prepare_create_args(config: Mapping[str, Any]) -> Dict[str, Any]:
        create_args = {k: v for k, v in config.items() if k in create_kwargs}
        return create_args

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Pass credential=AzureKeyCredential(os.environ['AZURE_AI_API_KEY']) or credential=DefaultAzureCredential()
  2. pip install azure-identity if using token credentials
  3. Verify the credential object is constructed, not just the key string

Example fix

# before
client = AzureAIChatCompletionClient(endpoint=ep, model_info=info, api_key=key)

# after
from azure.core.credentials import AzureKeyCredential
client = AzureAIChatCompletionClient(
    endpoint=ep, credential=AzureKeyCredential(key), model_info=info,
)
Defensive patterns

Strategy: validation

Validate before calling

from azure.core.credentials import AzureKeyCredential
import os

key = os.environ["AZURE_AI_API_KEY"]
credential = AzureKeyCredential(key)  # never pass the raw string

Type guard

from azure.core.credentials import AzureKeyCredential
from azure.core.credentials_async import AsyncTokenCredential

def is_valid_credential(cred) -> bool:
    return isinstance(cred, (AzureKeyCredential, AsyncTokenCredential))

Prevention

When it happens

Trigger: Constructing the client with endpoint and model_info only; passing 'api_key' string instead of a credential object; passing credential=None explicitly.

Common situations: Migrating from clients that accept a raw api_key string; assuming environment-based auth is picked up automatically (it is not — you must construct a credential object); missing azure-identity package so DefaultAzureCredential import failed earlier.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/54f765d35a9a264b. Report an issue: GitHub.