Add dedicated xai_key and fallback logic for xAI API key - #18660
Add dedicated xai_key and fallback logic for xAI API key#18660PeterDaveHello wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Hi @PeterDaveHello can you please resolve the merge conflicts?
|
29067af to
b776681
Compare
|
@krrishdholakia sure, it's done! |
|
Hi @krrishdholakia, is there anything I can do here? Thanks! |
- Add `xai_key` global variable in `litellm/__init__.py` - Define fallback order: parameter > xai_key > env variable > api_key - Add basic tests in `test_xai_key_fallback.py` to verify behavior
b776681 to
3cefd71
Compare
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
|
🤔 |
Greptile SummaryThis PR introduces a dedicated module-level variable for xAI API keys and wires it into the key-resolution fallback chain for both Chat and Responses API transformers.
Confidence Score: 4/5Safe to merge with awareness of one silent ordering change in the Responses API path. The responses/transformation.py path silently reverses the resolution priority of litellm.api_key and XAI_API_KEY — users who had both set and relied on the old ordering will see a different key used without any warning. The new order is more intuitive and risk is low, but it is a real behavioural change on an existing code path. litellm/llms/xai/responses/transformation.py — priority reversal between litellm.api_key and XAI_API_KEY
|
| Filename | Overview |
|---|---|
| litellm/init.py | Adds module-level xai_key variable, consistent with existing provider-specific keys. |
| litellm/llms/xai/common_utils.py | Expands get_api_key fallback chain to include litellm.xai_key (new) and litellm.api_key (generic final fallback); logic and ordering are correct. |
| litellm/llms/xai/chat/transformation.py | Delegates key resolution to XAIModelInfo.get_api_key, adding litellm.xai_key and litellm.api_key fallbacks that were previously absent from the Chat API path. |
| litellm/llms/xai/responses/transformation.py | Delegates key resolution to XAIModelInfo.get_api_key, but silently reverses the priority of litellm.api_key vs XAI_API_KEY compared to the old code. |
| tests/litellm/llms/xai/test_xai_key_fallback.py | Three mock-only tests covering the full key-priority chain; state is properly saved and restored in finally blocks. |
Reviews (1): Last reviewed commit: "Add dedicated xai_key and fallback logic..." | Re-trigger Greptile
| or litellm.api_key | ||
| or get_secret_str("XAI_API_KEY") | ||
| ) | ||
| api_key = XAIModelInfo.get_api_key(litellm_params.api_key) |
There was a problem hiding this comment.
Subtle priority reversal in Responses API key resolution
The old code checked litellm.api_key before XAI_API_KEY, whereas XAIModelInfo.get_api_key now checks XAI_API_KEY first and litellm.api_key last. Any Responses API user who had both litellm.api_key and XAI_API_KEY set would silently switch from using litellm.api_key to using XAI_API_KEY after this change. The new ordering is arguably more correct (provider-specific env var should beat the generic fallback), but it is a backwards-incompatible behaviour change for that configuration.
Rule Used: What: avoid backwards-incompatible changes without... (source)
|
I can't reopen this closed PR though, opened #28060. |
|
@krrish-berri-2 it looks strange, help needed 😅 |
|
Can you refile it?
*Krrish Dholakia | *CEO
Book a meeting with me
<https://calendly.com/d/cx9p-5yf-2nm/litellm-introductions>
LinkedIn <https://www.linkedin.com/in/krish-d/> | (770) 878 - 3106
***@***.***
P.S. See how LiteLLM helps LLM Platform teams move fast and stay in control
<https://www.litellm.ai/#features>
…On Sat, May 16, 2026 at 8:18 AM Peter Dave Hello ***@***.***> wrote:
*PeterDaveHello* left a comment (BerriAI/litellm#18660)
<#18660 (comment)>
@krrish-berri-2 <https://github.com/krrish-berri-2> it looks strange,
help needed 😅
—
Reply to this email directly, view it on GitHub
<#18660 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/CARFWGDFIE6S3V3DGIWERNL43CBCRAVCNFSM6AAAAACQYAHEWSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHM2DINRXGI2DCNZYHA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Yes, filed at #28060. |

Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
I have Added testing in the
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsMy PR passes all unit tests on
make test-unitpoetry run pytest tests/test_litellm -x -vv -n 4 --import-mode=importlibModuleNotFoundError: litellm_enterprise.proxy.common_utils.check_responses_cost(enterprise package not installed)tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_experimental_pass_through_messages_handler.py::test_bedrock_converse_budget_tokens_preserved(mock_postnot called)My PR's scope is as isolated as possible, it only solves 1 specific problem
CI (LiteLLM team)
Branch creation CI run
Link:
CI run for the last commit
Link:
Merge / cherry-pick CI run
Links:
Type
🐛 Bug Fix
✅ Test
Changes