Add dedicated xai_key and fallback logic for xAI API key - #28647
Conversation
Greptile SummaryThis PR introduces
Confidence Score: 4/5The change is additive and backward-compatible; no existing key resolution chain is narrowed, only a new litellm.xai_key slot is inserted above the existing fallbacks. All three xAI request paths now share a single resolver, the fallback orderings are covered by 20 unit tests, and no network calls or external state are touched. The only rough edge is the internal legacy_generic_before_env flag whose name requires reading the docstring to understand — a minor readability concern that does not affect correctness. No files require special attention; litellm/llms/xai/common_utils.py contains the flag whose naming could be improved but is otherwise correct.
|
| Filename | Overview |
|---|---|
| litellm/init.py | Adds xai_key module-level attribute, consistent with the existing pattern for other provider keys such as groq_key and gigachat_key |
| litellm/llms/xai/common_utils.py | Centralizes xAI API key resolution with new legacy_generic_before_env flag to preserve two different fallback orderings; get_models error message improved |
| litellm/llms/xai/chat/transformation.py | Delegates key resolution to XAIModelInfo.get_api_key, adding litellm.xai_key support while keeping existing behavior (no litellm.api_key fallback) |
| litellm/llms/xai/responses/transformation.py | Delegates key resolution to shared helper with legacy_generic_before_env=True, preserving the legacy litellm.api_key → XAI_API_KEY precedence; error message updated |
| litellm/realtime_api/main.py | Delegates xAI key resolution to shared helper with legacy_generic_before_env=True, matching previous litellm.api_key fallback behavior |
| tests/test_litellm/llms/xai/test_xai_key_fallback.py | New test file with 20 focused unit tests covering all fallback orderings for chat, responses, realtime, resolver, and model listing; all mocked, no real network calls |
Reviews (1): Last reviewed commit: "Add dedicated xai_key and fallback logic..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
PR overviewAdds xAI-specific API key fallbackThis PR introduces a module-level Security review
Risk: 2/10 |
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.
983787b to
a09ace4
Compare
|
Can you please share the logic behind this? what problem are you trying to solve? |
This is the follow-up to the earlier xAI key feedback from #18660 / #28060, now rebased onto The goal is to add a provider-specific module-level key for xAI, consistent with existing provider keys like The implementation preserves the existing fallback order for each xAI path, and the redaction update prevents the new module-level credential from being exposed in proxy startup logs. |
|
@Sameerlite Thank you! Do you also have a chance to take a look at #28649? Thanks a lot! |
Relevant issues
Replaces #28060 after the temporary OSS staging branch was deleted during cleanup.
Linear ticket
N/A
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/test_litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unit.venv/bin/pytest tests/test_litellm/llms/xai/test_xai_key_fallback.py -q.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@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer reviewDelays 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)
Branch creation CI run
Link:
CI run for the last commit
Link:
Merge / cherry-pick CI run
Links:
Screenshots / Proof of Fix
Type
🐛 Bug Fix
✅ Test
Changes
litellm.xai_keyfallback for xAI API key resolutionlitellm.api_keybeforeXAI_API_KEYbehavior