Skip to content

Retry an OpenAI-compatible upstream that rejects max_tokens as max_completion_tokens (#10787) - #10856

Closed
deepspace28 wants to merge 2 commits into
unslothai:mainfrom
deepspace28:fix/max-tokens-unsupported-retry
Closed

deepspace28 wants to merge 2 commits into
unslothai:mainfrom
deepspace28:fix/max-tokens-unsupported-retry

Conversation

@deepspace28

Copy link
Copy Markdown
Contributor

Fixes #10787.

Problem

A custom provider pointing at an upstream that only accepts max_completion_tokens (Azure-hosted gpt-5.x / o-series behind OpenAI-compatible proxies) failed every request with:

Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead. (unsupported_parameter)

The existing rename only fires for provider_type == "openai"; custom providers send max_tokens verbatim. The reporter's upstream is exactly that shape (provider=custom, a gpt-5 model behind an institutional gateway).

Fix

Detect the condition from the error body — 400 with error.code == "unsupported_parameter" and error.param == "max_tokens" (JSON), with a substring fallback for non-JSON bodies — rather than guessing from the model name, since custom providers route to arbitrary upstreams. On detection, retry once with max_completion_tokens:

  • Streaming path (stream_chat_completion): the status check happens before any SSE byte is yielded, so the retry is invisible to the client. Any other 400 (or a retry that also fails) surfaces exactly as before, through _friendly_provider_error_text.
  • Non-streaming path (chat_completion): same one-shot retry before raise_for_status().

A two-field probe (send both) is not an option — upstreams reject that too.

Tests

New test_external_provider_max_tokens_retry.py drives the real client against a loopback http.server (same pattern as test_external_provider_sampling_over_the_wire.py):

  • test_a_rejected_max_tokens_is_retried_as_max_completion_tokens: two bodies hit the server — first with max_tokens, retry with max_completion_tokens — and the client consumes the successful stream, never the 400.
  • test_an_unrelated_400_is_not_retried: a 400 naming a different parameter surfaces immediately, single request.
  • test_non_streaming_chat_completion_retries_too.
  • test_unsupported_max_tokens_error_detection: JSON match, substring fallback, and the negatives (other param, other status).
  • test_rename_max_tokens_body_moves_only_that_field: the first attempt's body is untouched.

Note: the diff is large only because the streaming async with moved inside a two-attempt loop (mechanical re-indent); the streaming logic itself is unchanged.

…ens (unslothai#10787)

Custom providers route to whatever upstream they front, and Azure-hosted
gpt-5.x / o-series answer 400 unsupported_parameter naming max_tokens
and asking for max_completion_tokens. The existing provider_type check
only renamed the field for provider_type == 'openai', so a custom
provider pointing at such an upstream failed every request.

Detect the condition from the error body (JSON error.code/param, with a
substring fallback) rather than guessing from the model name, and retry
once with the renamed field. In the streaming path nothing has been
yielded at the status check, so the retry is invisible to the client;
the non-streaming chat_completion path retries the same way. Any other
400 surfaces exactly as before.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3574a83125

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if status_code != 400:
return False
try:
error = _json.loads(error_text).get("error")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Guard against non-object JSON error bodies

When a custom compatible upstream returns a 400 whose body is valid JSON but not an object (for example [] or "bad request"), json.loads(...) returns a list or string and this .get raises AttributeError, which is not caught. Both streaming and non-streaming retry paths call this helper while handling the upstream 400, so the provider error is replaced by an unhandled exception instead of being relayed to the caller; check the decoded value is a dict before accessing error (or catch AttributeError).

Useful? React with 👍 / 👎.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed this hits external_provider.py where the max_completion_tokens rename is gated on provider_type being openai, so a custom provider fronting one of those upstreams still sends max_tokens. Will get this reviewed.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed this hits the path in studio/backend/core/inference/external_provider.py where the max_completion_tokens rename only fires for provider_type "openai", so custom gateways still send max_tokens. #10837 takes a different route to the same problem, so I will review the two together before deciding.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed that main only renames max_tokens for the openai provider type (external_provider.py around line 1522), so custom endpoints on Azure gpt-5 still fail every request. Will get this reviewed.

Souravrajvi0 added a commit to Souravrajvi0/unsloth that referenced this pull request Sep 23, 2026
…i#10787)

Per review on unslothai#10837, rely on provider_type openai for max_completion_tokens
and error-driven retry for custom gateways (same approach as unslothai#10856), avoiding
hostname-based field inference on custom base URLs.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 1, 2026
…tokens 400 (unslothai#10837)

* fix(studio): narrow unslothai#10787 max_tokens field selection and retry

Use max_completion_tokens for OpenAI and Azure Foundry base URLs via
_set_chat_completions_max_tokens. For other custom OpenAI-compatible
gateways (e.g. KI:connect), keep max_tokens on the first attempt and
retry once with max_completion_tokens when the upstream returns 400
unsupported_parameter for max_tokens.

Adds focused regression tests; streaming change is a mechanical re-indent
for the existing retry loop only.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix(studio): drop proactive Azure URL max_tokens field rule (unslothai#10787)

Per review on unslothai#10837, rely on provider_type openai for max_completion_tokens
and error-driven retry for custom gateways (same approach as unslothai#10856), avoiding
hostname-based field inference on custom base URLs.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Daniel Han <[email protected]>
@LeoBorcherding

Copy link
Copy Markdown
Collaborator

Closing this since #10837 landed the same max_completion_tokens retry on main on 10-01, covering both the streaming and non-streaming paths, and it closed #10787. The two approaches were very close and Daniel went with that one. Thanks for working on it, @deepspace28.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Unsupported parameter: 'max_tokens' instead of 'max_completion_tokens'.

3 participants