diff --git a/litellm/llms/bedrock/chat/converse_transformation.py b/litellm/llms/bedrock/chat/converse_transformation.py index 2b1d9619743..801ec571376 100644 --- a/litellm/llms/bedrock/chat/converse_transformation.py +++ b/litellm/llms/bedrock/chat/converse_transformation.py @@ -107,7 +107,7 @@ BEDROCK_COMPUTER_USE_TOOLS: Final = [ "bash_", "text_editor_", ] -BEDROCK_OPENAI_GPT_MIN_MAX_TOKENS: Final = 16 +BEDROCK_OPENAI_COMPAT_MIN_MAX_TOKENS: Final = 16 # Beta header patterns that are not supported by Bedrock Converse API # These will be filtered out to prevent errors @@ -378,10 +378,14 @@ class AmazonConverseConfig(BaseConfig): def _is_openai_gpt_reasoning_model(model: str) -> bool: return re.search(r"openai\.gpt-\d", model) is not None + @staticmethod + def _requires_min_max_tokens(model: str) -> bool: + return re.search(r"openai\.gpt-\d|xai\.grok-", model) is not None + @staticmethod def _enforce_min_max_tokens(max_tokens: object) -> object: - if isinstance(max_tokens, int) and max_tokens < BEDROCK_OPENAI_GPT_MIN_MAX_TOKENS: - return BEDROCK_OPENAI_GPT_MIN_MAX_TOKENS + if isinstance(max_tokens, int) and max_tokens < BEDROCK_OPENAI_COMPAT_MIN_MAX_TOKENS: + return BEDROCK_OPENAI_COMPAT_MIN_MAX_TOKENS return max_tokens def _is_nova_2_model(self, model: str) -> bool: @@ -1008,7 +1012,7 @@ class AmazonConverseConfig(BaseConfig): if param == "max_tokens" or param == "max_completion_tokens": optional_params["maxTokens"] = ( self._enforce_min_max_tokens(value) - if self._is_openai_gpt_reasoning_model(model) and isinstance(value, int) + if self._requires_min_max_tokens(model) and isinstance(value, int) else value ) if param == "stream": diff --git a/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py b/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py index f05b6ae97fe..9d8bf786829 100644 --- a/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py +++ b/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py @@ -463,10 +463,13 @@ def test_reasoning_with_forced_tool_choice_switches_to_auto(): ("us.openai.gpt-6-astra", "max_tokens", 1, 16), ("us.openai.gpt-6-astra", "max_completion_tokens", 1, 16), ("us.openai.gpt-6-astra", "max_tokens", 64, 64), + ("us.xai.grok-4.6", "max_tokens", 1, 16), + ("global.xai.grok-4.6", "max_completion_tokens", 1, 16), + ("us.xai.grok-4.6", "max_tokens", 32, 32), ("anthropic.claude-sonnet-4-5-20250929-v1:0", "max_tokens", 1, 1), ], ) -def test_map_openai_params_enforces_minimum_max_tokens_for_openai_gpt_reasoning_models( +def test_map_openai_params_enforces_minimum_max_tokens_for_openai_compat_models( model: str, param: str, value: int, expected_max_tokens: int ): optional_params = AmazonConverseConfig().map_openai_params(