-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: implement LLM provider adapter pattern to eliminate scattered provider branching #1307
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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.
Tighten
_detect_provider()to the route prefix/endpoints, not model substrings.These checks scan the whole model id for family names, so routed ids like
groq/llama-3.3-70b-versatilebecome Ollama, and any.../google/gemini-*or.../anthropic/claude-*route becomes Gemini/Anthropic even when the transport is OpenAI-compatible. Once that happens,_apply_ollama_defaults(),_supports_streaming_tools(), and_supports_prompt_caching()all pick the wrong adapter, so callers can suddenly get Ollama tool coercion, Gemini no-streaming, or Anthropiccache_controlpayloads on the wrong backend. This also drops theOPENAI_BASE_URL/OPENAI_API_BASE/:11434Ollama checks that_is_ollama_provider()still uses, so the same instance can be classified differently in different branches.🎯 Safer provider detection
def _detect_provider(self) -> str: if not self.model: return "default" model_lower = self.model.lower() + provider_prefix = model_lower.split("/", 1)[0] if "/" in model_lower else None + + if provider_prefix == "ollama": + return "ollama" + if provider_prefix in {"anthropic", "claude"}: + return "anthropic" + if provider_prefix in {"gemini", "google"} and "gemini" in model_lower: + return "gemini" - # Check base_url for provider hints - if self.base_url and "ollama" in self.base_url.lower(): + base_urls = [self.base_url, os.getenv("OPENAI_BASE_URL", ""), os.getenv("OPENAI_API_BASE", "")] + if any(url and ("ollama" in url.lower() or ":11434" in url) for url in base_urls): return "ollama" - # Check model name patterns - if any(prefix in model_lower for prefix in ['claude', 'anthropic/']): + if model_lower.startswith("claude"): return "anthropic" - if any(prefix in model_lower for prefix in ['gemini', 'gemini/', 'google/gemini']): + if model_lower.startswith("gemini") or model_lower.startswith("google/gemini"): return "gemini" - - # Check for Ollama models without prefix - ... return "openai"🤖 Prompt for AI Agents