-
Notifications
You must be signed in to change notification settings - Fork 7.6k
feat(llm): add native Groq provider and fix cache_breakpoint for non-Anthropic models #6314
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
Open
AHMEDDEV2004
wants to merge
2
commits into
crewAIInc:main
Choose a base branch
from
AHMEDDEV2004:feat/add-groq-native-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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,29 @@ | ||
| """Structured tool error formatting for agent consumption. | ||
|
|
||
| When a tool raises an exception, the agent needs structured information | ||
| to decide whether to retry, fix its input, or skip the tool entirely. | ||
| This module provides a consistent error format across all executors. | ||
| """ | ||
|
|
||
| import json | ||
| import traceback | ||
|
|
||
| RETRYABLE_EXCEPTIONS = (TimeoutError, ConnectionError, OSError) | ||
|
|
||
|
|
||
| def format_tool_error(exception: Exception, include_traceback: bool = False) -> str: | ||
| """Format a tool execution error as structured JSON for the agent. | ||
|
|
||
| Returns a string with the "Error executing tool:" prefix (for backward | ||
| compatibility with existing parsing) followed by a JSON object containing | ||
| the exception type, message, and retryability hint. | ||
| """ | ||
| error_data = { | ||
| "error": True, | ||
| "type": type(exception).__name__, | ||
| "message": str(exception), | ||
| "retryable": isinstance(exception, RETRYABLE_EXCEPTIONS), | ||
| } | ||
| if include_traceback: | ||
| error_data["traceback"] = traceback.format_exc(limit=3) | ||
| return f"Error executing tool: {json.dumps(error_data)}" | ||
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,98 @@ | ||
| """Tests for native Groq provider support and cache_breakpoint stripping.""" | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| class TestGroqNativeRouting: | ||
| """Test that Groq models route to the native OpenAI-compatible provider.""" | ||
|
|
||
| def test_groq_in_supported_providers(self): | ||
| from crewai.llm import SUPPORTED_NATIVE_PROVIDERS | ||
|
|
||
| assert "groq" in SUPPORTED_NATIVE_PROVIDERS | ||
|
|
||
| def test_groq_provider_config_exists(self): | ||
| from crewai.llms.providers.openai_compatible.completion import ( | ||
| OPENAI_COMPATIBLE_PROVIDERS, | ||
| ) | ||
|
|
||
| assert "groq" in OPENAI_COMPATIBLE_PROVIDERS | ||
| config = OPENAI_COMPATIBLE_PROVIDERS["groq"] | ||
| assert config.base_url == "https://api.groq.com/openai/v1" | ||
| assert config.api_key_env == "GROQ_API_KEY" | ||
| assert config.api_key_required is True | ||
|
|
||
| def test_groq_model_pattern_matching(self): | ||
| from crewai.llm import LLM | ||
|
|
||
| assert LLM._matches_provider_pattern("llama-3.3-70b-versatile", "groq") is True | ||
| assert LLM._matches_provider_pattern("mixtral-8x7b-32768", "groq") is True | ||
| assert LLM._matches_provider_pattern("gemma-7b-it", "groq") is True | ||
| assert LLM._matches_provider_pattern("whisper-large-v3", "groq") is True | ||
| assert ( | ||
| LLM._matches_provider_pattern("deepseek-r1-distill-llama-70b", "groq") | ||
| is True | ||
| ) | ||
| assert LLM._matches_provider_pattern("gpt-4o", "groq") is False | ||
|
|
||
| def test_groq_routes_to_openai_compatible(self): | ||
| from crewai.llm import LLM | ||
| from crewai.llms.providers.openai_compatible.completion import ( | ||
| OpenAICompatibleCompletion, | ||
| ) | ||
|
|
||
| provider_class = LLM._get_native_provider("groq") | ||
| assert provider_class is OpenAICompatibleCompletion | ||
|
|
||
|
|
||
| class TestCacheBreakpointStripping: | ||
| """Test that cache_breakpoint is stripped for non-Anthropic providers.""" | ||
|
|
||
| def test_strip_cache_breakpoint_for_non_anthropic(self): | ||
| from crewai.llm import LLM | ||
|
|
||
| llm = LLM.__new__(LLM, model="groq/llama-3.3-70b-versatile") | ||
| llm.model = "groq/llama-3.3-70b-versatile" | ||
| llm.is_anthropic = False | ||
|
|
||
| messages = [ | ||
| {"role": "system", "content": "You are helpful.", "cache_breakpoint": True}, | ||
| {"role": "user", "content": "Hello", "cache_breakpoint": True}, | ||
| ] | ||
|
|
||
| result = llm._format_messages_for_provider(messages) | ||
|
|
||
| for msg in result: | ||
| assert "cache_breakpoint" not in msg | ||
|
|
||
| def test_preserve_cache_breakpoint_for_anthropic(self): | ||
| from crewai.llm import LLM | ||
|
|
||
| llm = LLM.__new__(LLM, model="anthropic/claude-sonnet-4-20250514") | ||
| llm.model = "anthropic/claude-sonnet-4-20250514" | ||
| llm.is_anthropic = True | ||
|
|
||
| messages = [ | ||
| {"role": "user", "content": "Hello", "cache_breakpoint": True}, | ||
| ] | ||
|
|
||
| result = llm._format_messages_for_provider(messages) | ||
|
|
||
| assert result[0].get("cache_breakpoint") is True | ||
|
|
||
| def test_strip_does_not_remove_role_or_content(self): | ||
| from crewai.llm import LLM | ||
|
|
||
| llm = LLM.__new__(LLM, model="groq/llama-3.3-70b-versatile") | ||
| llm.model = "groq/llama-3.3-70b-versatile" | ||
| llm.is_anthropic = False | ||
|
|
||
| messages = [ | ||
| {"role": "user", "content": "Test message", "cache_breakpoint": True}, | ||
| ] | ||
|
|
||
| result = llm._format_messages_for_provider(messages) | ||
|
|
||
| assert result[0]["role"] == "user" | ||
| assert result[0]["content"] == "Test message" | ||
| assert "cache_breakpoint" not in result[0] |
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Format the traceback from
exception.__traceback__, not ambient exception state.traceback.format_exc()only works for the currently handled exception. If this helper is reused on a captured/re-raised exception,include_traceback=Truecan returnNoneType: Noneor the wrong stack.Proposed fix
📝 Committable suggestion
🧰 Tools
🪛 ast-grep (0.44.0)
[info] 28-28: use jsonify instead of json.dumps for JSON output
Context: json.dumps(error_data)
Note: [CWE-116] Improper Encoding or Escaping of Output.
(use-jsonify)
🤖 Prompt for AI Agents