{"record":{"id":"9ec953ac50464242","repo":"BerriAI/litellm","slug":"azureopenai-client-is-not-initialized-make-sure-a","errorCode":null,"errorMessage":"AzureOpenAI client is not initialized. Make sure api_key is passed or OPENAI_API_KEY is set in the environment.","messagePattern":"AzureOpenAI client is not initialized\\. Make sure api_key is passed or OPENAI_API_KEY is set in the environment\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"litellm/llms/azure/files/handler.py","lineNumber":74,"sourceCode":"        create_file_data: CreateFileRequest,\n        api_base: str | None,\n        api_key: str | None,\n        api_version: str | None,\n        timeout: float | httpx.Timeout,\n        max_retries: int | None,\n        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,","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/azure/files/handler.py#L56-L92","documentation":"In the Azure files handler, get_azure_openai_client() is asked to build a files-capable client from api_key/api_base/api_version/litellm_params. If it returns None — which happens when no api_key is available and the OPENAI_API_KEY env var is unset — the handler raises ValueError, since Azure file operations need an authenticated client. The isinstance checks that follow distinguish sync/async mismatches.","triggerScenarios":"Calling AzureOpenAI_Files.create_file() without api_key and without OPENAI_API_KEY (or AZURE_API_KEY as resolved by the client factory) in the environment; passing api_base/api_version only, with the key lookup failing in the secret manager.","commonSituations":"Batch/fine-tuning file-upload scripts that set AZURE_API_BASE and AZURE_API_VERSION but forget the key; proxy setups where files endpoints are configured with only a base URL; CI environments missing the secret env var.","solutions":["Pass api_key explicitly to the files call, or export OPENAI_API_KEY/AZURE_API_KEY in the environment.","Verify with: python -c \"import os; print(os.getenv('OPENAI_API_KEY'))\" before running the upload.","In the proxy, ensure the files-capable model entry includes api_key (litellm_params).","If using azure_ad_token-based auth, confirm the token provider path is configured so the client factory can authenticate."],"exampleFix":"# before\nfile_obj = azure_files.create_file(file=open(\"f.jsonl\",\"rb\"), purpose=\"fine-tune\")  # no key anywhere\n\n# after\nfile_obj = azure_files.create_file(\n    file=open(\"f.jsonl\",\"rb\"),\n    purpose=\"fine-tune\",\n    api_key=os.environ[\"AZURE_API_KEY\"],\n    api_base=\"https://my-resource.openai.azure.com\",\n    api_version=\"2024-10-21\",\n)","handlingStrategy":"validation","validationCode":"import os\n\ndef validate_files_config(api_key: str | None) -> None:\n    if not (api_key or os.getenv(\"OPENAI_API_KEY\") or os.getenv(\"AZURE_API_KEY\")):\n        raise ConfigError(\"No API key for Azure files call; pass api_key or set OPENAI_API_KEY\")","typeGuard":"from openai import AzureOpenAI, AsyncAzureOpenAI\n\ndef is_files_client(c: object, is_async: bool) -> bool:\n    return isinstance(c, AsyncAzureOpenAI) if is_async else isinstance(c, AzureOpenAI)","tryCatchPattern":"try:\n    f = azure_files.create_file(file=fh, purpose=\"fine-tune\", api_key=key, api_base=base, api_version=ver)\nexcept ValueError as e:\n    if \"not initialized\" in str(e):\n        raise ConfigError(\"Missing API key for Azure files client\") from e\n    raise","preventionTips":["Pass api_key/api_base/api_version explicitly for file operations instead of relying on env fallbacks.","Smoke-test file upload in CI where env vars are sparsest.","In proxies, keep files-capable model configs complete (key + base + version)."],"tags":["azure","files","authentication","configuration","client-initialization"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}