{"record":{"id":"0f20f982aca7ad62","repo":"BerriAI/litellm","slug":"azure-client-is-not-an-instance-of-azureopenai","errorCode":null,"errorMessage":"azure_client is not an instance of AzureOpenAI","messagePattern":"azure_client is not an instance of AzureOpenAI","errorType":"http","errorClass":"AzureOpenAIError","httpStatus":500,"severity":"error","filePath":"litellm/llms/azure/completion/handler.py","lineNumber":154,"sourceCode":"                        \"api_base\": api_base,\n                        \"complete_input_dict\": data,\n                    },\n                )\n                if not isinstance(max_retries, int):\n                    raise AzureOpenAIError(status_code=422, message=\"max retries must be an int\")\n                # init AzureOpenAI Client\n                azure_client: Final = self.get_azure_openai_client(\n                    api_key=api_key,\n                    api_base=api_base,\n                    api_version=api_version,\n                    client=client,\n                    litellm_params=litellm_params,\n                    _is_async=False,\n                    model=model,\n                )\n\n                if not isinstance(azure_client, AzureOpenAI):\n                    raise AzureOpenAIError(\n                        status_code=500,\n                        message=\"azure_client is not an instance of AzureOpenAI\",\n                    )\n\n                raw_response: Final = azure_client.completions.with_raw_response.create(**data, timeout=timeout)\n                response: Final = raw_response.parse()\n                stringified_response: Final = response.model_dump()\n                ## LOGGING\n                logging_obj.post_call(\n                    input=prompt,\n                    api_key=api_key,\n                    original_response=stringified_response,\n                    additional_args={\n                        \"headers\": headers,\n                        \"api_version\": api_version,\n                        \"api_base\": api_base,\n                    },\n                )","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/azure/completion/handler.py#L136-L172","documentation":"After get_azure_openai_client() builds the client for the sync text-completion path, LiteLLM asserts it is an instance of the OpenAI SDK's AzureOpenAI class; otherwise it raises AzureOpenAIError 500. The check catches a misconfigured client factory — e.g. the async class, a plain OpenAI client, or a mock — before calling .completions.with_raw_response.create.","triggerScenarios":"Passing a custom client= argument that is an AsyncAzureOpenAI or OpenAI instance into the sync path; get_azure_openai_client returning a plain OpenAI client because api_version/base resolution degenerated; test doubles replacing the client.","commonSituations":"Sharing one client object between sync and async code paths; DI frameworks injecting the wrong client type; upgrading the openai package where class identity moved between module aliases.","solutions":["Do not pass client= for the sync path; let LiteLLM construct the AzureOpenAI client from api_key/api_base/api_version.","If you inject a client, ensure it is openai.types/AsyncAzureOpenAI counterpart per path: AzureOpenAI for completion/streaming, AsyncAzureOpenAI for acompletion/astreaming.","Pin compatible openai SDK version matching your litellm version so isinstance checks see the same classes."],"exampleFix":"# before\nasync_client = AsyncAzureOpenAI(...)\nllm.completion(..., client=async_client)  # sync path gets async client\n\n# after\nsync_client = AzureOpenAI(api_key=..., azure_endpoint=..., api_version=...)\nllm.completion(..., client=sync_client)","handlingStrategy":"type-guard","validationCode":"from openai import AzureOpenAI\n\ndef validate_sync_client(client) -> None:\n    if client is not None and not isinstance(client, AzureOpenAI):\n        raise ConfigError(f\"sync path needs AzureOpenAI, got {type(client).__name__}\")","typeGuard":"from openai import AzureOpenAI\nfrom typing import TypeGuard\n\ndef is_sync_azure_client(c: object) -> TypeGuard[AzureOpenAI]:\n    return isinstance(c, AzureOpenAI)","tryCatchPattern":"try:\n    resp = llm.completion(...)\nexcept AzureOpenAIError as e:\n    if \"not an instance of AzureOpenAI\" in str(e):\n        raise ConfigError(\"Wrong client type injected for sync path\") from e\n    raise","preventionTips":["Don't inject clients; pass api_key/api_base/api_version and let LiteLLM build them.","Keep distinct sync/async client variables in shared code.","Pin openai SDK version to the one your litellm release was tested against."],"tags":["azure","client-initialization","type-guard","sdk"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}