BerriAI/litellm · error · ValueError
Azure OpenAI client is not initialized. Make sure api_key is
Error message
Azure OpenAI client is not initialized. Make sure api_key is passed or AZURE_API_KEY is set in the environment.
What it means
AzureFineTuningAPI.create_fine_tuning_job builds an OpenAI-family client via get_openai_client(api_key, api_base, ...); if that resolver returns None (no client= argument, no api_key, and AZURE_API_KEY absent from the environment) this ValueError is raised. It is a pre-flight credential check — no fine-tuning API request is made.
Source
Thrown at litellm/llms/azure/fine_tuning/handler.py:83
timeout: float | httpx.Timeout,
max_retries: int | None,
organization: str | None,
client: OpenAI | AsyncOpenAI | AzureOpenAI | AsyncAzureOpenAI | None = None,
) -> LiteLLMFineTuningJob | Coroutine[Any, Any, LiteLLMFineTuningJob]:
self._ensure_training_type(create_fine_tuning_job_data)
openai_client: Final[OpenAI | AsyncOpenAI | AzureOpenAI | AsyncAzureOpenAI | None] = self.get_openai_client(
api_key=api_key,
api_base=api_base,
timeout=timeout,
max_retries=max_retries,
organization=organization,
client=client,
_is_async=_is_async,
api_version=api_version,
)
if openai_client is None:
raise ValueError(
"Azure OpenAI client is not initialized. Make sure api_key is passed or AZURE_API_KEY is set in the environment."
)
if _is_async is True:
if not isinstance(openai_client, (AsyncOpenAI, AsyncAzureOpenAI)):
raise ValueError(
"OpenAI client is not an instance of AsyncOpenAI. Make sure you passed an AsyncOpenAI client."
)
return self.acreate_fine_tuning_job(
create_fine_tuning_job_data=create_fine_tuning_job_data,
openai_client=openai_client,
)
verbose_logger.debug("creating fine tuning job, args= %s", create_fine_tuning_job_data)
response: Final = cast(OpenAI, openai_client).fine_tuning.jobs.create(**create_fine_tuning_job_data)
return _litellm_fine_tuning_job_from_response(response, is_azure=True)
def cancel_fine_tuning_job(View on GitHub (pinned to 6c2dcb801b)
Solutions
- Pass api_key=os.environ['AZURE_API_KEY'] to create_fine_tuning_job.
- Or export AZURE_API_KEY in the runtime environment.
- Or pass a pre-built AzureOpenAI/AsyncAzureOpenAI client via client=.
- Add a startup validation step that fails fast when no Azure credential is discoverable.
Example fix
# before
job = azure_finetune.create_fine_tuning_job(data)
# after
job = azure_finetune.create_fine_tuning_job(
data,
api_key=os.environ['AZURE_API_KEY'],
api_base='https://<resource>.openai.azure.com',
) Defensive patterns
Strategy: validation
Validate before calling
import os
def validate_fts_credentials(api_key: str | None, client: object | None) -> None:
if client is None and not api_key and not os.environ.get('AZURE_API_KEY'):
raise RuntimeError('fine-tuning needs api_key, a client, or AZURE_API_KEY in env') Try / catch
try:
job = ft.create_fine_tuning_job(data, api_key=api_key)
except ValueError as e:
if 'not initialized' in str(e):
raise RuntimeError('Azure fine-tuning credential missing (AZURE_API_KEY)') from e
raise Prevention
- Set AZURE_API_KEY via your secret manager, not ad-hoc exports.
- Validate credentials in a preflight check before submitting training data.
- Note this handler checks AZURE_API_KEY, not OPENAI_API_KEY.
When it happens
Trigger: create_fine_tuning_job(create_fine_tuning_job_data) with no api_key, no client, and AZURE_API_KEY unset; pipelines running in containers where the Azure key secret was not injected.
Common situations: Automation that sets OPENAI_API_KEY for OpenAI jobs but forgets AZURE_API_KEY for the Azure variant; rotated/expired Azure keys removed from the environment; multi-region deployments where one region lacks the env var.
Related errors
- Missing Authorization header
- Invalid bearer token
- Invalid API key
- Prompt '{prompt_id}' not found
- max retries must be an int
AI-assisted analysis of BerriAI/litellm@6c2dcb801b (2026-08-15).
Data as JSON: /api/errors/2c0400d3215dccd8.
Report an issue: GitHub.