{"record":{"id":"3448399a2a7e82e6","repo":"BerriAI/litellm","slug":"azure-client-is-not-an-instance-of-asyncazureopena-344839","errorCode":null,"errorMessage":"azure_client is not an instance of AsyncAzureOpenAI","messagePattern":"azure_client is not an instance of AsyncAzureOpenAI","errorType":"http","errorClass":"AzureOpenAIError","httpStatus":500,"severity":"error","filePath":"litellm/llms/azure/completion/handler.py","lineNumber":216,"sourceCode":"        azure_ad_token: str | None = None,\n        client=None,  # this is the AsyncAzureOpenAI\n        litellm_params: dict = {},\n    ):\n        response = None\n        try:\n            # init AzureOpenAI Client\n            # setting Azure client\n            azure_client: Final = self.get_azure_openai_client(\n                api_version=api_version,\n                api_base=api_base,\n                api_key=api_key,\n                model=model,\n                _is_async=True,\n                client=client,\n                litellm_params=litellm_params,\n            )\n            if not isinstance(azure_client, AsyncAzureOpenAI):\n                raise AzureOpenAIError(\n                    status_code=500,\n                    message=\"azure_client is not an instance of AsyncAzureOpenAI\",\n                )\n\n            ## LOGGING\n            logging_obj.pre_call(\n                input=data[\"prompt\"],\n                api_key=azure_client.api_key,\n                additional_args={\n                    \"headers\": {\"Authorization\": f\"Bearer {azure_client.api_key}\"},\n                    \"api_base\": azure_client._base_url._uri_reference,\n                    \"acompletion\": True,\n                    \"complete_input_dict\": data,\n                },\n            )\n            raw_response: Final = await azure_client.completions.with_raw_response.create(**data, timeout=timeout)\n            response = raw_response.parse()\n            return openai_text_completion_config.convert_to_chat_model_response_object(","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/azure/completion/handler.py#L198-L234","documentation":"The async text-completion path (acompletion) requires the client returned by get_azure_openai_client(..., _is_async=True) to be an AsyncAzureOpenAI instance; otherwise it raises AzureOpenAIError 500 before logging pre-call data. It is the async mirror of 1030 and catches sync clients, plain OpenAI clients, or mocks being injected into the async path.","triggerScenarios":"Passing a sync AzureOpenAI or OpenAI instance as client= to an await-ed azure text completion; a client factory configured without _is_async; stale clients reused after switching call styles.","commonSituations":"Codebases with both sync and async entry points sharing one client variable; FastAPI handlers refactoring from sync to async while keeping the module-level client; test fixtures mocking only the sync class.","solutions":["Remove the client= argument and let LiteLLM build the async client, or pass AsyncAzureOpenAI(...) explicitly.","Keep separate clients per mode: azure_client (sync) and azure_async_client (async).","When mocking, patch with AsyncAzureOpenAI instances or MagicMock(spec=AsyncAzureOpenAI)."],"exampleFix":"# before\nclient = AzureOpenAI(...)  # sync\nresp = await llm.acompletion(..., client=client)\n\n# after\nclient = AsyncAzureOpenAI(api_key=..., azure_endpoint=..., api_version=...)\nresp = await llm.acompletion(..., client=client)","handlingStrategy":"type-guard","validationCode":"from openai import AsyncAzureOpenAI\n\ndef validate_async_client(client) -> None:\n    if client is not None and not isinstance(client, AsyncAzureOpenAI):\n        raise ConfigError(f\"async path needs AsyncAzureOpenAI, got {type(client).__name__}\")","typeGuard":"from openai import AsyncAzureOpenAI\nfrom typing import TypeGuard\n\ndef is_async_azure_client(c: object) -> TypeGuard[AsyncAzureOpenAI]:\n    return isinstance(c, AsyncAzureOpenAI)","tryCatchPattern":"try:\n    resp = await llm.acompletion(...)\nexcept AzureOpenAIError as e:\n    if \"not an instance of AsyncAzureOpenAI\" in str(e):\n        raise ConfigError(\"Pass AsyncAzureOpenAI or omit client=\") from e\n    raise","preventionTips":["Construct one AsyncAzureOpenAI per app and use it only on async paths.","In tests, use MagicMock(spec=AsyncAzureOpenAI).","Static-type your client variables so sync/async mixing fails at typecheck time."],"tags":["azure","async","client-initialization","type-guard"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}