Skip to content

fix(studio): retry custom gateways with max_completion_tokens after max_tokens 400 (#10787) - #10837

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
Souravrajvi0:feat/custom-max-completion-tokens-3644
Oct 1, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
Souravrajvi0:feat/custom-max-completion-tokens-3644

Conversation

@Souravrajvi0

@Souravrajvi0 Souravrajvi0 commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #10787.

A custom provider pointing at a gateway that only accepts max_completion_tokens (Azure-hosted gpt-5.x / o-series behind OpenAI-compatible proxies, e.g. KI:connect) failed every request with Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead. Only provider_type == "openai" sent max_completion_tokens.

Fix

  • The first request is unchanged (max_tokens for every non-OpenAI provider), so vLLM, llama.cpp and other servers that only know max_tokens see no difference.
  • When the upstream answers 400 and the error names max_tokens (param == "max_tokens" with code == "unsupported_parameter" or a message mentioning max_completion_tokens; substring fallback for non-JSON bodies), the request is resent once with max_completion_tokens.
  • Streaming: _stream_post_retrying_max_tokens wraps the stream("POST", ...) call. The status check happens before anything is yielded, so the retry is invisible to the client; the stream body itself is untouched. Any other 400, or a failing retry, surfaces exactly as before.
  • Non-streaming chat_completion: same one-shot retry before raise_for_status().

Tests

studio/backend/tests/test_external_provider_max_tokens_field.py drives the real client against a loopback HTTP server: stream and non-stream retry on the wire, an unrelated 400 is not retried, and the error-matching cases. The two wire tests fail with the call sites reverted.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

@Souravrajvi0

This comment has been minimized.

Souravrajvi0 and others added 3 commits September 23, 2026 03:23
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.
…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.
@Souravrajvi0
Souravrajvi0 force-pushed the feat/custom-max-completion-tokens-3644 branch from f163c0e to 240d15b Compare September 23, 2026 03:24
@Souravrajvi0

This comment has been minimized.

@Souravrajvi0 Souravrajvi0 changed the title fix(studio): max_completion_tokens for remote custom GPT gateways (#10787) fix(studio): retry custom gateways with max_completion_tokens after max_tokens 400 (#10787) Sep 23, 2026
@danielhanchen danielhanchen self-assigned this Oct 1, 2026
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'.

2 participants