diff --git a/litellm/llms/xai/chat/transformation.py b/litellm/llms/xai/chat/transformation.py index 14b62db41b..5a488876cd 100644 --- a/litellm/llms/xai/chat/transformation.py +++ b/litellm/llms/xai/chat/transformation.py @@ -50,7 +50,7 @@ class XAIChatConfig(OpenAIGPTConfig): "web_search_options", ] # for some reason, grok-3-mini does not support stop tokens - if "grok-3-mini" not in model: + if self._supports_stop_reason(model): base_openai_params.append("stop") try: if litellm.supports_reasoning( @@ -61,6 +61,13 @@ class XAIChatConfig(OpenAIGPTConfig): verbose_logger.debug(f"Error checking if model supports reasoning: {e}") return base_openai_params + + def _supports_stop_reason(self, model: str) -> bool: + if "grok-3-mini" in model: + return False + elif "grok-4" in model: + return False + return True def map_openai_params( self, diff --git a/tests/llm_translation/test_xai.py b/tests/llm_translation/test_xai.py index 41a39c7971..ba360d7178 100644 --- a/tests/llm_translation/test_xai.py +++ b/tests/llm_translation/test_xai.py @@ -118,6 +118,21 @@ def test_xai_check_for_stop_in_supported_params(): assert "stop" not in supported_params +@pytest.mark.parametrize("model", ["xai/grok-4", "xai/grok-4-0709"]) +def test_xai_grok_4_stop_not_supported(model): + """ + Test that grok-4 models do not support the stop parameter + + Issue: https://github.com/BerriAI/litellm/issues/12635 + """ + supported_params = XAIChatConfig().get_supported_openai_params( + model=model + ) + assert "stop" not in supported_params + + + + @pytest.mark.parametrize("stream", [False, True]) def test_completion_xai(stream): try: @@ -130,9 +145,9 @@ def test_completion_xai(stream): }, ] response = completion( - model="xai/grok-3-mini-beta", + model="xai/grok-4", messages=messages, - stream=stream, + stream=stream ) print(response)