Skip to content

fix(azure): preserve AD token refresh in v1 OpenAI client path#28625

Draft
mateo-berri wants to merge 1 commit into
litellm_internal_stagingfrom
claude/busy-edison-90ZbL
Draft

fix(azure): preserve AD token refresh in v1 OpenAI client path#28625
mateo-berri wants to merge 1 commit into
litellm_internal_stagingfrom
claude/busy-edison-90ZbL

Conversation

@mateo-berri

Copy link
Copy Markdown
Collaborator

Summary

Fixes the Azure AD authentication regression in 1.84.0 reported in #27945. When api_version is v1, latest, or preview, litellm routes Azure calls through a plain OpenAI/AsyncOpenAI client (to hit /openai/v1/). That path only forwarded api_key from azure_client_params, so any AD-only configuration — including enable_azure_ad_token_refresh: true, the case in #27945 — hit OpenAIError: The api_key client option must be set… on every request.

The OpenAI SDK (>= 2.20.0, our minimum) already accepts a Callable[[], str] as api_key and re-invokes it on every request via _refresh_api_key. We now forward azure_ad_token_provider directly — preserving the per-request refresh behavior of the regular AzureOpenAI client and avoiding the expiry hole that resolving the token once at client-creation time would introduce.

  • litellm/llms/azure/common_utils.py: in the _is_azure_v1_api_version branch, set api_key to azure_client_params["api_key"] or azure_ad_token_provider or azure_ad_token. For the async client, wrap the sync provider returned by azure-identity in an async function (AsyncOpenAI requires Callable[[], Awaitable[str]]).
  • Static azure_ad_token strings fall through to api_key as before.
  • Explicit api_key still wins (test added).

Test plan

  • New tests in tests/test_litellm/llms/azure/test_azure_common_utils.py, parametrized over v1 / latest / preview:
    • test_azure_v1_api_with_azure_ad_token_provider — provider forwarded as api_key; SDK stores it as _api_key_provider (sync) or wrapped async provider (async).
    • test_azure_v1_api_async_token_provider_resolves_to_current_token — async wrapper calls the underlying provider on each invocation (token rotation honored).
    • test_azure_v1_api_with_static_azure_ad_token — static AD token used as api_key.
    • test_azure_v1_api_key_wins_over_ad_token — explicit api_key takes precedence; _api_key_provider is None.
  • Existing v1 / traditional Azure tests still pass.

Fixes #27945

https://claude.ai/code/session_01UnzrDSFUUgp5T2wRoPMxq5


Generated by Claude Code

The /openai/v1/ code path (api_version in {"v1", "latest", "preview"})
constructs a plain OpenAI/AsyncOpenAI client, but only forwarded
`api_key` from `azure_client_params`. When `enable_azure_ad_token_refresh`
is set (or any AD-only auth), `api_key` is None and the client
constructor raised "The api_key client option must be set...", breaking
every Azure call with a v1 api_version.

The OpenAI SDK (>=2.20.0) accepts a callable for `api_key` and re-invokes
it on every request via `_refresh_api_key`, so we now forward
`azure_ad_token_provider` directly — preserving the per-request token
refresh behavior of the regular AzureOpenAI client and avoiding the
expiry hole that resolving the token once at client-creation time would
introduce. Static `azure_ad_token` strings fall through to `api_key`.

For the async path we wrap the sync provider returned by azure-identity
in an async function since AsyncOpenAI expects `Callable[[], Awaitable[str]]`.

Fixes #27945

https://claude.ai/code/session_01UnzrDSFUUgp5T2wRoPMxq5
@CLAassistant

CLAassistant commented May 22, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@mateo-berri mateo-berri changed the base branch from main to litellm_internal_staging May 22, 2026 16:58
@codspeed-hq

codspeed-hq Bot commented May 22, 2026

Copy link
Copy Markdown
Contributor

Congrats! CodSpeed is installed 🎉

🆕 16 new benchmarks were detected.

You will start to see performance impacts in the reports once the benchmarks are run from your default branch.

Detected benchmarks


Open in CodSpeed

@codecov

codecov Bot commented May 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Azure OAI Models Auth Broken in 1.84.0

3 participants