BerriAI/litellm · error · ValueError
AzureOpenAI client is not initialized. Make sure api_key is
Error message
AzureOpenAI client is not initialized. Make sure api_key is passed or OPENAI_API_KEY is set in the environment.
What it means
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.
Source
Thrown at litellm/llms/azure/files/handler.py:74
create_file_data: CreateFileRequest,
api_base: str | None,
api_key: str | None,
api_version: str | None,
timeout: float | httpx.Timeout,
max_retries: int | None,
client: AzureOpenAI | AsyncAzureOpenAI | OpenAI | AsyncOpenAI | None = None,
litellm_params: dict | None = None,
) -> OpenAIFileObject | Coroutine[Any, Any, OpenAIFileObject]:
openai_client: AzureOpenAI | AsyncAzureOpenAI | OpenAI | AsyncOpenAI | None = self.get_azure_openai_client(
litellm_params=litellm_params or {},
api_key=api_key,
api_base=api_base,
api_version=api_version,
client=client,
_is_async=_is_async,
)
if openai_client is None:
raise ValueError(
"AzureOpenAI client is not initialized. Make sure api_key is passed or OPENAI_API_KEY is set in the environment."
)
if _is_async is True:
if not isinstance(openai_client, (AsyncAzureOpenAI, AsyncOpenAI)):
raise ValueError(
"AzureOpenAI client is not an instance of AsyncAzureOpenAI. Make sure you passed an AsyncAzureOpenAI client."
)
return self.acreate_file(create_file_data=create_file_data, openai_client=openai_client)
response: Final = cast(AzureOpenAI | OpenAI, openai_client).files.create(
**self._prepare_create_file_data(create_file_data)
)
return OpenAIFileObject(**response.model_dump())
async def afile_content(
self,
file_content_request: FileContentRequest,
openai_client: AsyncAzureOpenAI | AsyncOpenAI,View on GitHub (pinned to 6c2dcb801b)
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.
Example fix
# before
file_obj = azure_files.create_file(file=open("f.jsonl","rb"), purpose="fine-tune") # no key anywhere
# after
file_obj = azure_files.create_file(
file=open("f.jsonl","rb"),
purpose="fine-tune",
api_key=os.environ["AZURE_API_KEY"],
api_base="https://my-resource.openai.azure.com",
api_version="2024-10-21",
) Defensive patterns
Strategy: validation
Validate before calling
import os
def validate_files_config(api_key: str | None) -> None:
if not (api_key or os.getenv("OPENAI_API_KEY") or os.getenv("AZURE_API_KEY")):
raise ConfigError("No API key for Azure files call; pass api_key or set OPENAI_API_KEY") Type guard
from openai import AzureOpenAI, AsyncAzureOpenAI
def is_files_client(c: object, is_async: bool) -> bool:
return isinstance(c, AsyncAzureOpenAI) if is_async else isinstance(c, AzureOpenAI) Try / catch
try:
f = azure_files.create_file(file=fh, purpose="fine-tune", api_key=key, api_base=base, api_version=ver)
except ValueError as e:
if "not initialized" in str(e):
raise ConfigError("Missing API key for Azure files client") from e
raise Prevention
- 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).
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- api_key is required for Azure AI Speech transcription.
- AZURE_CLIENT_ID and AZURE_TENANT_ID must be set
- GEMINI_API_KEY is required for Google AI Studio file operati
- api_key is required
- Error: {response.status_code} - {response.text}
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/9ec953ac50464242.
Report an issue: GitHub.