Repository navigation
Retry an OpenAI-compatible upstream that rejects max_tokens as max_completion_tokens (#10787) - #10856
deepspace28 wants to merge 2 commits into
Conversation
…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.
There was a problem hiding this comment.
💡 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") |
There was a problem hiding this comment.
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 👍 / 👎.
|
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. |
|
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. |
|
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. |
…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.
…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]>
|
Closing this since #10837 landed the same |
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:The existing rename only fires for
provider_type == "openai";customproviders sendmax_tokensverbatim. 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"anderror.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 withmax_completion_tokens: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.chat_completion): same one-shot retry beforeraise_for_status().A two-field probe (send both) is not an option — upstreams reject that too.
Tests
New
test_external_provider_max_tokens_retry.pydrives the real client against a loopbackhttp.server(same pattern astest_external_provider_sampling_over_the_wire.py):test_a_rejected_max_tokens_is_retried_as_max_completion_tokens: two bodies hit the server — first withmax_tokens, retry withmax_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 withmoved inside a two-attempt loop (mechanical re-indent); the streaming logic itself is unchanged.