Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions litellm/llms/openai/responses/transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

from ..common_utils import OpenAIError

OPENAI_RESPONSES_API_MIN_MAX_OUTPUT_TOKENS = 16

if TYPE_CHECKING:
from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj

Expand Down Expand Up @@ -92,6 +94,10 @@ def map_openai_params(
"""
params = dict(response_api_optional_params)

max_output_tokens = params.get("max_output_tokens")
if isinstance(max_output_tokens, int) and max_output_tokens < OPENAI_RESPONSES_API_MIN_MAX_OUTPUT_TOKENS:
params["max_output_tokens"] = OPENAI_RESPONSES_API_MIN_MAX_OUTPUT_TOKENS

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The max_output_tokens value is silently raised without any log line. When a caller passes 1 and receives a response generated with up to 16 tokens instead, there is no observable signal — unexpected token usage or cost discrepancies would be impossible to trace back to this clamping. A single verbose_logger.debug call here would make the behaviour discoverable without any user-facing noise.

Suggested change
max_output_tokens = params.get("max_output_tokens")
if isinstance(max_output_tokens, int) and max_output_tokens < OPENAI_RESPONSES_API_MIN_MAX_OUTPUT_TOKENS:
params["max_output_tokens"] = OPENAI_RESPONSES_API_MIN_MAX_OUTPUT_TOKENS
max_output_tokens = params.get("max_output_tokens")
if isinstance(max_output_tokens, int) and max_output_tokens < OPENAI_RESPONSES_API_MIN_MAX_OUTPUT_TOKENS:
verbose_logger.debug(
"OpenAI Responses API requires max_output_tokens >= %d; clamping %d -> %d",
OPENAI_RESPONSES_API_MIN_MAX_OUTPUT_TOKENS,
max_output_tokens,
OPENAI_RESPONSES_API_MIN_MAX_OUTPUT_TOKENS,
)
params["max_output_tokens"] = OPENAI_RESPONSES_API_MIN_MAX_OUTPUT_TOKENS


if self._is_gpt_5_model(model=model):
temperature = params.get("temperature")
if temperature is not None and temperature != 1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@ def test_map_openai_params(self):
# The function should return the params unchanged
assert result == test_params

@pytest.mark.parametrize("max_output_tokens", [1, 15])
def test_map_openai_params_clamps_max_output_tokens_below_minimum(self, max_output_tokens):
"""OpenAI's Responses API rejects max_output_tokens < 16.

Claude Code (via the Anthropic Messages -> Responses adapter) sends a
max_tokens=1 warmup probe when running `/model`, which produced:
"Invalid 'max_output_tokens': integer below minimum value.
Expected a value >= 16, but got 1 instead."
Clamp anything below the minimum up to 16 instead of erroring.
"""
result = self.config.map_openai_params(
response_api_optional_params={"max_output_tokens": max_output_tokens},
model=self.model,
drop_params=False,
)

assert result["max_output_tokens"] == 16

def test_map_openai_params_preserves_max_output_tokens_at_or_above_minimum(self):
"""Values already >= 16 must pass through untouched."""
result = self.config.map_openai_params(
response_api_optional_params={"max_output_tokens": 256},
model=self.model,
drop_params=False,
)

assert result["max_output_tokens"] == 256

def validate_responses_api_request_params(self, params, expected_fields):
"""
Validate that the params dict has the expected structure of ResponsesAPIRequestParams
Expand Down
Loading