{"record":{"id":"9b5ed79805c4ec88","repo":"BerriAI/litellm","slug":"azureopenai-client-is-not-an-instance-of-asyncazur","errorCode":null,"errorMessage":"AzureOpenAI client is not an instance of AsyncAzureOpenAI. Make sure you passed an AsyncAzureOpenAI client.","messagePattern":"AzureOpenAI client is not an instance of AsyncAzureOpenAI\\. Make sure you passed an AsyncAzureOpenAI client\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/azure/files/handler.py","lineNumber":80,"sourceCode":"        client: AzureOpenAI | AsyncAzureOpenAI | OpenAI | AsyncOpenAI | None = None,\n        litellm_params: dict | None = None,\n    ) -> OpenAIFileObject | Coroutine[Any, Any, OpenAIFileObject]:\n        openai_client: AzureOpenAI | AsyncAzureOpenAI | OpenAI | AsyncOpenAI | None = self.get_azure_openai_client(\n            litellm_params=litellm_params or {},\n            api_key=api_key,\n            api_base=api_base,\n            api_version=api_version,\n            client=client,\n            _is_async=_is_async,\n        )\n        if openai_client is None:\n            raise ValueError(\n                \"AzureOpenAI client is not initialized. Make sure api_key is passed or OPENAI_API_KEY is set in the environment.\"\n            )\n\n        if _is_async is True:\n            if not isinstance(openai_client, (AsyncAzureOpenAI, AsyncOpenAI)):\n                raise ValueError(\n                    \"AzureOpenAI client is not an instance of AsyncAzureOpenAI. Make sure you passed an AsyncAzureOpenAI client.\"\n                )\n            return self.acreate_file(create_file_data=create_file_data, openai_client=openai_client)\n        response: Final = cast(AzureOpenAI | OpenAI, openai_client).files.create(\n            **self._prepare_create_file_data(create_file_data)\n        )\n        return OpenAIFileObject(**response.model_dump())\n\n    async def afile_content(\n        self,\n        file_content_request: FileContentRequest,\n        openai_client: AsyncAzureOpenAI | AsyncOpenAI,\n    ) -> HttpxBinaryResponseContent:\n        response: Final = await openai_client.files.content(**file_content_request)\n        return HttpxBinaryResponseContent(response=response.response)\n\n    def file_content(\n        self,","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/azure/files/handler.py#L62-L98","documentation":"litellm's Azure file-management wrapper dispatches on the _is_async flag: when True it requires the resolved client to be an AsyncAzureOpenAI (or AsyncOpenAI) instance. This ValueError means the async branch was taken but the client resolved from get_azure_openai_client() is a synchronous AzureOpenAI/OpenAI object. It is a programming/config error raised before any network call is made.","triggerScenarios":"Calling azure_files_instance.create_file(..., _is_async=True) while supplying a sync AzureOpenAI client via the client= argument; or a context where get_azure_openai_client returns the cached sync client (e.g. a client previously constructed without async semantics) and _is_async=True is forced by an async caller path.","commonSituations":"Porting sync litellm file scripts to asyncio and reusing the old sync client object; mixed async frameworks (FastAPI handlers) where a module-level sync AzureOpenAI client is shared; passing a client built as AzureOpenAI(...) instead of AsyncAzureOpenAI(...).","solutions":["Pass an AsyncAzureOpenAI instance: AsyncAzureOpenAI(api_key=..., azure_endpoint=..., api_version=...) in the client= argument.","If you intended a synchronous call, call the sync entrypoint (do not set _is_async=True) so the sync files.create path is used.","Do not reuse a module-level sync client in async code; construct the async client once per event loop instead."],"exampleFix":"# before\nfrom openai import AzureOpenAI\nclient = AzureOpenAI(api_key=key, azure_endpoint=base, api_version=ver)\nawait litellm.azure_files.create_file(create_file_data, client=client, _is_async=True)  # raises\n\n# after\nfrom openai import AsyncAzureOpenAI\nclient = AsyncAzureOpenAI(api_key=key, azure_endpoint=base, api_version=ver)\nawait litellm.azure_files.create_file(create_file_data, client=client, _is_async=True)","handlingStrategy":"type-guard","validationCode":"from openai import AsyncAzureOpenAI, AsyncOpenAI\n\ndef assert_async_client(client: object) -> None:\n    if not isinstance(client, (AsyncAzureOpenAI, AsyncOpenAI)):\n        raise TypeError(f'expected an async client, got {type(client).__name__}')","typeGuard":"from openai import AsyncAzureOpenAI, AsyncOpenAI\n\ndef is_async_client(client: object) -> bool:\n    return isinstance(client, (AsyncAzureOpenAI, AsyncOpenAI))","tryCatchPattern":"try:\n    await handler.create_file(data, client=client, _is_async=True)\nexcept ValueError as e:\n    if 'AsyncAzureOpenAI' in str(e):\n        raise TypeError('sync client passed to async files API') from e\n    raise","preventionTips":["Build the client in the same layer that decides sync vs async so the type always matches the call path.","Name variables async_client / sync_client explicitly instead of a shared 'client'.","In tests, parametrize fixtures over both client kinds and assert dispatch works for each."],"tags":["azure","openai","files","async","client-type","validation"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}