Skip to content

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

Closed
PeterDaveHello wants to merge 1 commit into
BerriAI:mainfrom
PeterDaveHelloKitchen:Support-xai_key
Closed

Add dedicated xai_key and fallback logic for xAI API key#18660
PeterDaveHello wants to merge 1 commit into
BerriAI:mainfrom
PeterDaveHelloKitchen:Support-xai_key

Conversation

@PeterDaveHello

@PeterDaveHello PeterDaveHello commented Jan 5, 2026

Copy link
Copy Markdown
Contributor

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 details

  • My PR passes all unit tests on make test-unit

    • Ran: poetry run pytest tests/test_litellm -x -vv -n 4 --import-mode=importlib
    • Failed: ModuleNotFoundError: litellm_enterprise.proxy.common_utils.check_responses_cost (enterprise package not installed)
    • Failed: tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_experimental_pass_through_messages_handler.py::test_bedrock_converse_budget_tokens_preserved (mock_post not called)
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

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:

Type

🐛 Bug Fix
✅ Test

Changes

  • Add xai_key fallback for xAI API key resolution
  • Ensure xAI API key fallback test restores global/environment state

@vercel

vercel Bot commented Jan 5, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
litellm Ready Ready Preview, Comment Jan 18, 2026 4:20pm

Request Review

@ghost

ghost commented Jan 6, 2026

Copy link
Copy Markdown

Hi @PeterDaveHello can you please resolve the merge conflicts?

Screenshot 2026-01-07 at 12 06 44 AM

@PeterDaveHello

Copy link
Copy Markdown
Contributor Author

@krrishdholakia sure, it's done!

@PeterDaveHello

Copy link
Copy Markdown
Contributor Author

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
@github-actions

Copy link
Copy Markdown
Contributor

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.

@github-actions github-actions Bot added the stale label Apr 19, 2026
@github-actions github-actions Bot closed this Apr 26, 2026
@PeterDaveHello

Copy link
Copy Markdown
Contributor Author

🤔

@krrish-berri-2

Copy link
Copy Markdown
Contributor

@greptile

@greptile-apps

greptile-apps Bot commented May 15, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This 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.

  • litellm/__init__.py adds xai_key, consistent with other provider-specific variables like groq_key.
  • XAIModelInfo.get_api_key now resolves in priority order: caller-supplied → litellm.xai_keyXAI_API_KEY env var → litellm.api_key; both transformers delegate to this single method.
  • Three mock-only tests cover each fallback step and restore global state in finally blocks.

Confidence Score: 4/5

Safe 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

Important Files Changed

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 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)

@PeterDaveHello

Copy link
Copy Markdown
Contributor Author

I can't reopen this closed PR though, opened #28060.

@PeterDaveHello

Copy link
Copy Markdown
Contributor Author

@krrish-berri-2 it looks strange, help needed 😅

@krrish-berri-2

krrish-berri-2 commented May 16, 2026 via email

Copy link
Copy Markdown
Contributor

@PeterDaveHello

Copy link
Copy Markdown
Contributor Author

Can you refile it? Krrish Dholakia | CEO Book a meeting with me <calendly.com/d/cx9p-5yf-2nm/litellm-introductions> LinkedIn <linkedin.com/in/krish-d> | (770) 878 - 3106 @.* P.S. See how LiteLLM helps LLM Platform teams move fast and stay in control <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.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants