-
-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Add dedicated xai_key and fallback logic for xAI API key #18660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import os | ||
| import litellm | ||
| from litellm.llms.xai.chat.transformation import XAIChatConfig | ||
| from litellm.llms.xai.common_utils import XAIModelInfo | ||
| from litellm.llms.xai.responses.transformation import XAIResponsesAPIConfig | ||
|
|
||
|
|
||
| def test_get_api_key_priority(): | ||
| """ | ||
| Test the fallback order of XAI API key resolution: | ||
| 1. api_key parameter | ||
| 2. litellm.xai_key | ||
| 3. XAI_API_KEY environment variable | ||
| 4. litellm.api_key | ||
| 5. None | ||
| """ | ||
|
|
||
| original_env = os.environ.get("XAI_API_KEY") | ||
| had_env = "XAI_API_KEY" in os.environ | ||
| original_xai_key = litellm.xai_key | ||
| original_api_key = litellm.api_key | ||
|
|
||
| try: | ||
| # Case 1: api_key parameter is passed | ||
| litellm.xai_key = "xai_key_value" | ||
| litellm.api_key = "common_api_key" | ||
| os.environ["XAI_API_KEY"] = "env_api_key" | ||
| result = XAIModelInfo.get_api_key("param_api_key") | ||
| assert result == "param_api_key" | ||
|
|
||
| # Case 2: api_key not passed, use litellm.xai_key | ||
| result = XAIModelInfo.get_api_key(None) | ||
| assert result == "xai_key_value" | ||
|
|
||
| # Case 3: api_key and xai_key not set, prefer XAI_API_KEY over litellm.api_key | ||
| litellm.xai_key = None | ||
| result = XAIModelInfo.get_api_key(None) | ||
| assert result == "env_api_key" | ||
|
|
||
| # Case 4: Empty XAI_API_KEY falls through to litellm.api_key | ||
| os.environ["XAI_API_KEY"] = "" | ||
| result = XAIModelInfo.get_api_key(None) | ||
| assert result == "common_api_key" | ||
|
|
||
| # Case 5: None of the above, return None | ||
| os.environ.pop("XAI_API_KEY", None) | ||
| litellm.api_key = None | ||
| result = XAIModelInfo.get_api_key(None) | ||
| assert result is None | ||
| finally: | ||
| if had_env: | ||
| os.environ["XAI_API_KEY"] = original_env | ||
| else: | ||
| os.environ.pop("XAI_API_KEY", None) | ||
| litellm.xai_key = original_xai_key | ||
| litellm.api_key = original_api_key | ||
|
|
||
|
|
||
| def test_chat_config_uses_xai_key_fallback(): | ||
| original_env = os.environ.get("XAI_API_KEY") | ||
| had_env = "XAI_API_KEY" in os.environ | ||
| original_xai_key = litellm.xai_key | ||
| original_api_key = litellm.api_key | ||
|
|
||
| try: | ||
| litellm.xai_key = "xai_key_value" | ||
| litellm.api_key = None | ||
| os.environ.pop("XAI_API_KEY", None) | ||
| _, api_key = XAIChatConfig()._get_openai_compatible_provider_info(None, None) | ||
| assert api_key == "xai_key_value" | ||
| finally: | ||
| if had_env: | ||
| os.environ["XAI_API_KEY"] = original_env | ||
| else: | ||
| os.environ.pop("XAI_API_KEY", None) | ||
| litellm.xai_key = original_xai_key | ||
| litellm.api_key = original_api_key | ||
|
|
||
|
|
||
| def test_responses_config_uses_xai_key_fallback(): | ||
| original_env = os.environ.get("XAI_API_KEY") | ||
| had_env = "XAI_API_KEY" in os.environ | ||
| original_xai_key = litellm.xai_key | ||
| original_api_key = litellm.api_key | ||
|
|
||
| try: | ||
| litellm.xai_key = "xai_key_value" | ||
| litellm.api_key = None | ||
| os.environ.pop("XAI_API_KEY", None) | ||
| headers = XAIResponsesAPIConfig().validate_environment( | ||
| {}, "xai/grok-3-mini", None | ||
| ) | ||
| assert headers["Authorization"] == "Bearer xai_key_value" | ||
| finally: | ||
| if had_env: | ||
| os.environ["XAI_API_KEY"] = original_env | ||
| else: | ||
| os.environ.pop("XAI_API_KEY", None) | ||
| litellm.xai_key = original_xai_key | ||
| litellm.api_key = original_api_key |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old code checked
litellm.api_keybeforeXAI_API_KEY, whereasXAIModelInfo.get_api_keynow checksXAI_API_KEYfirst andlitellm.api_keylast. Any Responses API user who had bothlitellm.api_keyandXAI_API_KEYset would silently switch from usinglitellm.api_keyto usingXAI_API_KEYafter 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)