Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(bedrock): clamp maxTokens to the 16-token minimum for OpenAI GPT models on Converse

Devin AIfc186f5613d7
diff --git a/litellm/llms/bedrock/chat/converse_transformation.py b/litellm/llms/bedrock/chat/converse_transformation.py
index 1a4f27e2ddd..4ef42664b9f 100644
--- a/litellm/llms/bedrock/chat/converse_transformation.py
+++ b/litellm/llms/bedrock/chat/converse_transformation.py
@@ -107,6 +107,7 @@ BEDROCK_COMPUTER_USE_TOOLS: Final = [
"bash_",
"text_editor_",
]
+BEDROCK_OPENAI_GPT_MIN_MAX_TOKENS: Final = 16
# Beta header patterns that are not supported by Bedrock Converse API
# These will be filtered out to prevent errors
@@ -377,6 +378,12 @@ 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 _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
+ return max_tokens
+
def _is_nova_2_model(self, model: str) -> bool:
"""
Check if the model is a Nova 2 model that supports reasoningConfig.
@@ -999,7 +1006,11 @@ class AmazonConverseConfig(BaseConfig):
is_thinking_enabled=is_thinking_enabled,
)
if param == "max_tokens" or param == "max_completion_tokens":
- optional_params["maxTokens"] = value
+ optional_params["maxTokens"] = (
+ self._enforce_min_max_tokens(cast(object, value))
+ if self._is_openai_gpt_reasoning_model(model)
+ else value
+ )
if param == "stream":
optional_params["stream"] = value
if param == "stop":
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 96c78c1cf75..f05b6ae97fe 100644
--- a/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py
+++ b/tests/test_litellm/llms/bedrock/chat/test_converse_transformation.py
@@ -457,6 +457,28 @@ def test_reasoning_with_forced_tool_choice_switches_to_auto():
assert optional_params["tool_choice"] == {"auto": {}}
+ "model, param, value, expected_max_tokens",
+ [
+ ("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),
+ ("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(
+ model: str, param: str, value: int, expected_max_tokens: int
+):
+ optional_params = AmazonConverseConfig().map_openai_params(
+ non_default_params={param: value},
+ optional_params={},
+ model=model,
+ drop_params=False,
+ )
+
+ assert optional_params["maxTokens"] == expected_max_tokens
+
+
@pytest.mark.parametrize(
"model",
[