{"record":{"id":"54f765d35a9a264b","repo":"microsoft/autogen","slug":"credential-is-required-for-azureaichatcompletioncl","errorCode":null,"errorMessage":"credential is required for AzureAIChatCompletionClient","messagePattern":"credential is required for AzureAIChatCompletionClient","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-ext/src/autogen_ext/models/azure/_azure_ai_client.py","lineNumber":304,"sourceCode":"\n\n    \"\"\"\n\n    def __init__(self, **kwargs: Unpack[AzureAIChatCompletionClientConfig]):\n        config = self._validate_config(kwargs)  # type: ignore\n        self._model_info = config[\"model_info\"]  # type: ignore\n        self._client = self._create_client(config)\n        self._create_args = self._prepare_create_args(config)\n\n        self._actual_usage = RequestUsage(prompt_tokens=0, completion_tokens=0)\n        self._total_usage = RequestUsage(prompt_tokens=0, completion_tokens=0)\n\n    @staticmethod\n    def _validate_config(config: Dict[str, Any]) -> AzureAIChatCompletionClientConfig:\n        if \"endpoint\" not in config:\n            raise ValueError(\"endpoint is required for AzureAIChatCompletionClient\")\n        if \"credential\" not in config:\n            raise ValueError(\"credential is required for AzureAIChatCompletionClient\")\n        if \"model_info\" not in config:\n            raise ValueError(\"model_info is required for AzureAIChatCompletionClient\")\n        validate_model_info(config[\"model_info\"])\n        if _is_github_model(config[\"endpoint\"]) and \"model\" not in config:\n            raise ValueError(\"model is required for when using a Github model with AzureAIChatCompletionClient\")\n        return cast(AzureAIChatCompletionClientConfig, config)\n\n    @staticmethod\n    def _create_client(config: AzureAIChatCompletionClientConfig) -> ChatCompletionsClient:\n        # Only pass the parameters that ChatCompletionsClient accepts\n        # Remove 'model_info' and other client-specific parameters\n        client_config = {k: v for k, v in config.items() if k not in (\"model_info\",)}\n        return ChatCompletionsClient(**client_config)  # type: ignore\n\n    @staticmethod\n    def _prepare_create_args(config: Mapping[str, Any]) -> Dict[str, Any]:\n        create_args = {k: v for k, v in config.items() if k in create_kwargs}\n        return create_args","sourceCodeStart":286,"sourceCodeEnd":322,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/models/azure/_azure_ai_client.py#L286-L322","documentation":"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.","triggerScenarios":"Constructing the client with endpoint and model_info only; passing 'api_key' string instead of a credential object; passing credential=None explicitly.","commonSituations":"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.","solutions":["Pass credential=AzureKeyCredential(os.environ['AZURE_AI_API_KEY']) or credential=DefaultAzureCredential()","pip install azure-identity if using token credentials","Verify the credential object is constructed, not just the key string"],"exampleFix":"# before\nclient = AzureAIChatCompletionClient(endpoint=ep, model_info=info, api_key=key)\n\n# after\nfrom azure.core.credentials import AzureKeyCredential\nclient = AzureAIChatCompletionClient(\n    endpoint=ep, credential=AzureKeyCredential(key), model_info=info,\n)","handlingStrategy":"validation","validationCode":"from azure.core.credentials import AzureKeyCredential\nimport os\n\nkey = os.environ[\"AZURE_AI_API_KEY\"]\ncredential = AzureKeyCredential(key)  # never pass the raw string","typeGuard":"from azure.core.credentials import AzureKeyCredential\nfrom azure.core.credentials_async import AsyncTokenCredential\n\ndef is_valid_credential(cred) -> bool:\n    return isinstance(cred, (AzureKeyCredential, AsyncTokenCredential))","tryCatchPattern":null,"preventionTips":["Always construct a credential object (AzureKeyCredential or DefaultAzureCredential)","pip install azure-identity when using token credentials","Check required env vars before client construction"],"tags":["azure","configuration","authentication","constructor"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}