fix(openai): clamp max_tokens to per-model limits to prevent overflow errors#5696
Merged
Conversation
… errors Clients like Claude Code may request max_tokens values that exceed what the target OpenAI model supports (e.g. 32000 for gpt-4o-mini which caps at 16384), causing BadRequestError from the OpenAI API. Add a static per-model max_output_tokens map and clamp incoming requests accordingly, with prefix matching for dated snapshot variants and a warning log for unknown models. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Signed-off-by: Sébastien Han <seb@redhat.com>
…napshot prefixes. Signed-off-by: Sébastien Han <seb@redhat.com>
Member
Author
|
With this patch i can do: |
Signed-off-by: Sébastien Han <seb@redhat.com>
cdoern
approved these changes
May 4, 2026
cdoern
left a comment
Collaborator
There was a problem hiding this comment.
lots of new unit tests but lgtm
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
Fixes
BadRequestError: max_tokens is too largewhen clients (e.g. Claude Code) sendmax_tokensvalues that exceed what the target OpenAI model supports. For example, Claude Code requestsmax_tokens: 32000butgpt-4o-minionly supports 16384.Adds a static per-model
max_output_tokensmap to the OpenAI provider adapter and clamps incomingmax_tokensat request time. Supports prefix matching for dated snapshot variants (e.g.gpt-4o-2024-08-06inherits fromgpt-4o). Logs a warning once per unknown model so operators know the map needs updating when new models are released.Also populates
max_output_tokensin model metadata viaconstruct_model_from_identifier(), exposing it through the/v1/modelsendpoint'scustom_metadatafield.Test Plan
Output: