Repository navigation
Retry with max_completion_tokens when the endpoint rejects max_tokens - #1232
Open
Schro-code wants to merge 2 commits into
Open
Schro-code wants to merge 2 commits into
Schro-code wants to merge 2 commits into
Conversation
added 2 commits
October 14, 2026 18:08
Signed-off-by: Zhoulu <ldazzh@lineadirecta.es>
Signed-off-by: Zhoulu <ldazzh@lineadirecta.es>
WaylandYang
requested changes
Oct 14, 2026
WaylandYang
left a comment
Contributor
There was a problem hiding this comment.
Thank you, the problem is real and reading error.param / error.code is the right way to detect it.
Two things to change:
- In
chat_at_streamingeach attempt rewritesbody["max_tokens"], so after the rename the next request carries it again and the retry can loop without end. - Every call pays the 400 again (once per chunk). Learn the parameter name and a rejected temperature once on the client, as
completion_ceilingalready does.
Please add a wiremock test like the existing "max_tokens is too large" one, and run cargo fmt (the lint failure). Thanks again.
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.
Why
Newer OpenAI reasoning models (o1, o3, GPT-5 family) renamed
max_tokenstomax_completion_tokens. Sending the old name returns a 400 witherror.code: "unsupported_parameter", and every extraction chunk fails. The same models also rejecttemperaturevalues other than their default (1) witherror.code: "unsupported_value".What changes
A
send_with_compat_retriesmethod inspectserror.paramanderror.codein a 400 response and retries once with the corrected parameter:max_tokensis renamed tomax_completion_tokens, and a rejectedtemperatureis dropped.chat_at(non-streaming) routes through it. The same detection is woven intochat_at_streaming's existing retry loop so the streaming path also handles the transition.Each adjustment applies at most once per request — the already-applied key is removed from the body before retrying, so the loop terminates even if the endpoint keeps rejecting.
How it was checked
Tested against an Azure OpenAI endpoint running gpt-5.6-sol, which rejects
max_tokens. Before the fix: 70/70 extraction chunks fail with 400. After: all pass on the first retry.Before review
git commit -s)cargo fmt --all --check,cargo clippy --workspace --all-targets -- -D warningsandcargo test --workspacepassweb/:pnpm buildandpnpm testpass — N/A, no web changescrates/utopia-store/was tested withUTOPIA_DATABASE_URLset — N/A, no SQL changesdev— N/A, no migrationsweb/src/i18n/en.tsandzh.ts— N/A, no UI stringsdocs/decisions/— N/A