BerriAI/litellm · error · ValueError

Azure client is not an instance of AsyncAzureOpenAI or Async

Error message

Azure client is not an instance of AsyncAzureOpenAI or AsyncOpenAI

What it means

Async counterpart of the sync client check: in acompletion, the client obtained from `get_azure_openai_client` must be an AsyncAzureOpenAI or AsyncOpenAI instance, else this ValueError is raised. Typically caused by injecting a sync client or a custom object via the `client` parameter.

Source

Thrown at litellm/llms/azure/azure.py:419

        azure_ad_token_provider: Callable | None = None,
        convert_tool_call_to_json_mode: bool | None = None,
        client=None,  # this is the AsyncAzureOpenAI
        litellm_params: dict | None = {},
    ):
        response = None
        try:
            # setting Azure client
            azure_client: Final = self.get_azure_openai_client(
                api_version=api_version,
                api_base=api_base,
                api_key=api_key,
                model=model,
                client=client,
                _is_async=True,
                litellm_params=litellm_params,
            )
            if not isinstance(azure_client, (AsyncAzureOpenAI, AsyncOpenAI)):
                raise ValueError("Azure client is not an instance of AsyncAzureOpenAI or AsyncOpenAI")
            ## LOGGING
            logging_obj.pre_call(
                input=data["messages"],
                api_key=azure_client.api_key,
                additional_args={
                    "headers": {
                        "api_key": api_key,
                        "azure_ad_token": azure_ad_token,
                    },
                    "api_base": api_base,
                    "acompletion": True,
                    "complete_input_dict": data,
                },
            )

            headers, response = await self.make_azure_openai_chat_completion_request(
                azure_client=azure_client,
                data=data,

View on GitHub (pinned to 6c2dcb801b)

Solutions

  1. Use openai.AsyncAzureOpenAI(...) when supplying a client to acompletion.
  2. Or drop the client argument and let LiteLLM construct the async client itself.
  3. For sync flows use litellm.completion.

Example fix

# before
client = openai.AzureOpenAI(api_key=k, azure_endpoint=base)
await litellm.acompletion(model='azure/gpt-4o', messages=msgs, client=client)

# after
client = openai.AsyncAzureOpenAI(api_key=k, azure_endpoint=base, api_version='2024-06-01')
await litellm.acompletion(model='azure/gpt-4o', messages=msgs, client=client)
Defensive patterns

Strategy: type-guard

Validate before calling

if client is not None and not isinstance(client, (openai.AsyncAzureOpenAI, openai.AsyncOpenAI)):
    raise TypeError('client must be AsyncAzureOpenAI/AsyncOpenAI for acompletion')

Type guard

from openai import AsyncAzureOpenAI, AsyncOpenAI

def is_async_azure_client(c) -> bool:
    return isinstance(c, (AsyncAzureOpenAI, AsyncOpenAI))

Prevention

When it happens

Trigger: Passing client=openai.AzureOpenAI(...) (sync) to a litellm.acompletion call; passing an httpx client or a mock object instead of an SDK client.

Common situations: Reusing one client factory for both sync and async code paths; test harnesses injecting fakes that don't subclass the SDK classes.

Related errors


AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15). Data as JSON: /api/errors/9bb5383b54c5e13b. Report an issue: GitHub.