Skip to content

Add dedicated xai_key and fallback logic for xAI API key - #28060

Closed
PeterDaveHello wants to merge 6 commits into
BerriAI:shin_agent_oss_staging_05_19_2026from
PeterDaveHelloKitchen:Support-xai_key
Closed

Add dedicated xai_key and fallback logic for xAI API key#28060
PeterDaveHello wants to merge 6 commits into
BerriAI:shin_agent_oss_staging_05_19_2026from
PeterDaveHelloKitchen:Support-xai_key

Conversation

@PeterDaveHello

@PeterDaveHello PeterDaveHello commented May 16, 2026

Copy link
Copy Markdown
Contributor

Relevant issues

Replaces #18660 because GitHub would not allow reopening it after the branch
was force-pushed/recreated.

Addresses #18660 (comment).

Linear ticket

N/A

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/test_litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • My PR passes all unit tests on make test-unit
    • Ran: .venv/bin/pytest tests/test_litellm/llms/xai/test_xai_key_fallback.py -q
    • Ran: .venv/bin/pytest tests/test_litellm/llms/xai/test_xai_chat_transformation.py tests/test_litellm/llms/xai/responses/test_xai_responses_transformation.py -q
  • My PR's scope is as isolated as possible, it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

CI (LiteLLM team)

CI status guideline:

  • 50-55 passing tests: main is stable with minor issues.
  • 45-49 passing tests: acceptable but needs attention
  • <= 40 passing tests: unstable; be careful with your merges and assess the risk.
  • Branch creation CI run
    Link:

  • CI run for the last commit
    Link:

  • Merge / cherry-pick CI run
    Links:

Screenshots / Proof of Fix

.venv/bin/pytest tests/test_litellm/llms/xai/test_xai_key_fallback.py -q
20 passed in 0.49s

.venv/bin/pytest tests/test_litellm/llms/xai/test_xai_chat_transformation.py tests/test_litellm/llms/xai/responses/test_xai_responses_transformation.py -q
18 passed in 1.13s

Type

🐛 Bug Fix
✅ Test

Changes

  • Add litellm.xai_key fallback for xAI API key resolution
  • Share xAI key resolution across chat, responses, and realtime paths
  • Preserve Responses API and realtime legacy litellm.api_key before XAI_API_KEY behavior
  • Add focused tests for chat, responses, realtime, resolver, and model listing fallback order

FabrizioCafolla and others added 4 commits May 15, 2026 10:36
Squash-merged by litellm-agent from FabrizioCafolla's PR.
Squash-merged by litellm-agent from tomdee's PR.
Squash-merged by litellm-agent from escon1004's PR.
@codecov

codecov Bot commented May 16, 2026

Copy link
Copy Markdown

@greptile-apps

greptile-apps Bot commented May 16, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR introduces litellm.xai_key as a dedicated, provider-specific API key for xAI, and centralizes key resolution for all xAI endpoints (chat, responses, realtime, model listing) into a single XAIModelInfo.get_api_key helper.

  • New fallback slot: litellm.xai_key is inserted between an explicitly passed key and the XAI_API_KEY env var across all xAI paths, while responses and realtime endpoints preserve their historical litellm.api_key precedence over the env var via the legacy_generic_before_env flag.
  • No regressions for existing callers: callers who don't set xai_key see the same effective priority chain as before; the new slot is simply None by default.
  • 20 focused mock tests validate every permutation of the fallback order for the resolver, chat config, responses config, realtime, and model-listing paths.

Confidence Score: 5/5

Safe to merge — the change is purely additive, backward-compatible, and well-covered by tests.

All three xAI code paths (chat, responses, realtime) correctly delegate to the centralized resolver, which is additive by design: the new xai_key slot defaults to None, leaving existing behavior unchanged for callers who don't set it. The legacy_generic_before_env paths preserve the pre-existing litellm.api_key → XAI_API_KEY order for responses and realtime. Tests are mock-only and cover all priority-chain permutations.

No files require special attention.

Important Files Changed

Filename Overview
litellm/llms/xai/common_utils.py Adds legacy_generic_before_env flag to get_api_key, centralizing xAI key resolution with two distinct priority chains; error message improved. No logic errors found.
litellm/llms/xai/chat/transformation.py Delegates key resolution to XAIModelInfo.get_api_key; behavior preserved for existing callers, new xai_key fallback added cleanly.
litellm/llms/xai/responses/transformation.py Migrates key resolution to the shared helper with legacy_generic_before_env=True, preserving the historical litellm.api_keyXAI_API_KEY ordering. Updated error message is more descriptive.
litellm/realtime_api/main.py Replaces inline 3-step fallback with XAIModelInfo.get_api_key(..., legacy_generic_before_env=True), keeping the same effective order while gaining xai_key support.
tests/test_litellm/llms/xai/test_xai_key_fallback.py 20 new mock-only tests covering all fallback orderings for the resolver, chat, responses, realtime, and model-listing paths. No real network calls.

Reviews (2): Last reviewed commit: "Add dedicated xai_key and fallback logic..." | Re-trigger Greptile

@oss-pr-review-agent-shin

Copy link
Copy Markdown
Contributor

🤖 litellm-agent: Auto-merge skipped — the staging branch shin_agent_oss_staging_05_16_2026 has 4 commit(s) not in your branch. Merging as-is would produce a confusing diff on the staging PR.

Please rebase your branch onto shin_agent_oss_staging_05_16_2026 and push; the agent will re-review automatically.

@CLAassistant

CLAassistant commented May 16, 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 all sign our Contributor License Agreement before we can accept your contribution.
5 out of 6 committers have signed the CLA.

✅ Cyberfilo
✅ FabrizioCafolla
✅ escon1004
✅ PeterDaveHello
✅ tomdee
❌ krrish-berri-2
You have signed the CLA already but the status is still pending? Let us recheck it.

@PeterDaveHello

Copy link
Copy Markdown
Contributor Author

Rebased onto shin_agent_oss_staging_05_16_2026 and pushed as requested.

@PeterDaveHello

Copy link
Copy Markdown
Contributor Author

The requested rebase looks ... strange.

@PeterDaveHello
PeterDaveHello changed the base branch from litellm_internal_staging to shin_agent_oss_staging_05_16_2026 May 16, 2026 15:18
Add a provider-specific litellm.xai_key fallback for xAI chat,
responses, and realtime requests.

Keep the Responses API and realtime fallback order compatible by
preserving litellm.api_key before XAI_API_KEY when no explicit
provider-specific key is set.
@krrish-berri-2
krrish-berri-2 changed the base branch from shin_agent_oss_staging_05_16_2026 to shin_agent_oss_staging_05_19_2026 May 19, 2026 02:51
@oss-pr-review-agent-shin

Copy link
Copy Markdown
Contributor

🤖 litellm-agent: Auto-merge skipped — the staging branch shin_agent_oss_staging_05_20_2026 has 7 commit(s) not in your branch. Merging as-is would produce a confusing diff on the staging PR.

Please rebase your branch onto shin_agent_oss_staging_05_20_2026 and push; the agent will re-review automatically.

@Sameerlite
Sameerlite deleted the branch BerriAI:shin_agent_oss_staging_05_19_2026 May 22, 2026 12:07
@Sameerlite Sameerlite closed this May 22, 2026
@PeterDaveHello

Copy link
Copy Markdown
Contributor Author

@Sameerlite, is there anything I can do to make it right?

@Sameerlite

Copy link
Copy Markdown
Contributor

Extremely sorry about this. the branch was deleted as a part of cleanup process. Can you please create a new one with litellm_internal_staging

@PeterDaveHello

Copy link
Copy Markdown
Contributor Author

@Sameerlite, thanks, I'm willing to, but also a little bit frustrated and afraid of the same thing happening once again. Is there any chance of preventing this situation?

@Sameerlite

Copy link
Copy Markdown
Contributor

litellm_internal_staging doesn't get deleted. It's the main branch. So it should not happen

@PeterDaveHello

Copy link
Copy Markdown
Contributor Author

Hi @Sameerlite, I opened a fresh replacement PR targeting litellm_internal_staging: #28647. Thanks again for the guidance.

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.

8 participants