fix: prevent Ollama key from polluting global litellm state#2325
Open
fix: prevent Ollama key from polluting global litellm state#2325
Conversation
When OLLAMA.API_KEY is configured, __init__() sets litellm.api_key globally. Since litellm is a module-level singleton, this overwrites the API key for ALL providers (Groq, XAI, OpenAI, Anthropic, etc.). If Ollama is configured alongside another provider, the Ollama key replaces theirs and all non-Ollama calls fail authentication. Fix: store the Ollama key on the handler instance (self.ollama_api_key) and inject it per-request via litellm's api_key kwarg only when the model is an Ollama model (ollama/ or ollama_chat/ prefix). This builds on the prior fixes in #2288 and #2293 which addressed related symptoms but left the root cause (global mutation) in place. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
Review Summary by QodoPrevent Ollama key from polluting global litellm state
WalkthroughsDescription• Store Ollama API key on handler instance instead of global litellm state • Inject Ollama key per-request only for ollama/ prefixed models • Prevent Ollama key from overwriting keys for other providers (Groq, XAI, OpenAI, etc.) • Update test to verify correct per-provider key isolation Diagramflowchart LR
A["OLLAMA.API_KEY configured"] -->|"Previously: global mutation"| B["litellm.api_key overwritten"]
B -->|"Breaks other providers"| C["Non-Ollama calls fail"]
A -->|"Now: instance storage"| D["self.ollama_api_key"]
D -->|"Per-request injection"| E["Only for ollama/ models"]
E -->|"Preserves other keys"| F["Groq/XAI/OpenAI work correctly"]
File Changes1. pr_agent/algo/ai_handlers/litellm_ai_handler.py
|
Contributor
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require reviewⓘ The new review experience is currently in Beta. Learn more |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
LiteLLMAIHandler.__init__()setslitellm.api_keyglobally whenOLLAMA.API_KEYis configured. Sincelitellmis a module-level singleton, this overwrites the API key for ALL providers (Groq, xAI, OpenAI, Anthropic, etc.). Any non-Ollama call then sends the Ollama key and fails authentication.self.ollama_api_key) instead of globally, and inject it per-request via litellm'sapi_keykwarg only when the model uses theollama/orollama_chat/prefix.test_ollama_and_groq_coexisttest now verifies correct per-provider key isolation -- Ollama models get the Ollama key, non-Ollama models get the Groq key.This builds on prior fixes #2288 and #2293 which addressed related symptoms (Gemini broken by Ollama key, dummy key forwarding) but left the root cause (global
litellm.api_keymutation) in place.Note on GitLab submodule content fetching
There is a separate issue in
gitlab_provider.pywhere_expand_submodule_changes()creates synthetic diff entries with paths likesubmodule_path/file.py, butget_diff_files()still callsget_pr_file_content()against the parent project ID. These paths don't exist in the parent repo -- the content lives in the submodule project. This is a real bug but requires more invasive changes (threading submodule project resolution through content fetching) and is left for a separate PR.Test plan
test_litellm_api_key_guard.pypasstest_ollama_and_groq_coexistnow correctly verifies Ollama key goes only to Ollama models and Groq key goes to non-Ollama models🤖 Generated with Claude Code