Skip to content

fix(ai): retry OpenAI requests without unsupported max_tokens/temperature - #114

Merged
codeErrorSleep merged 1 commit into
codeErrorSleep:mainfrom
LetterXbox:fix/openai-reasoning-model-params
Nov 20, 2026
Merged

codeErrorSleep merged 1 commit into
codeErrorSleep:mainfrom
LetterXbox:fix/openai-reasoning-model-params

Conversation

@LetterXbox

@LetterXbox LetterXbox commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Problem

With the official OpenAI API and newer models such as gpt-5.4-mini or gpt-6.2-sol, every AI chat fails:

AI provider returned 400 Bad Request: { "error": { "message": "Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead.", "type": "invalid_request_error", "param": "max_tokens", "code": "unsupported_parameter" } }

OpenAICompatProvider always sends max_tokens and temperature (0.1). These models require max_completion_tokens, and some of them (e.g. gpt-6.2-sol) also reject any non-default temperature:

{ "error": { "message": "Unsupported value: 'temperature' does not support 0.1 with this model. Only the default (1) value is supported.", "type": "invalid_request_error", "param": "temperature", "code": "unsupported_value" } }

Fix

The provider now adapts to the error instead of keeping a list of model names (which would go stale with every release):

  • The first request is sent exactly as before (same fields and values), so DeepSeek, Ollama and other OpenAI-compatible providers see no change.
  • On a 400 whose error is param: "max_tokens" with code: "unsupported_parameter", the request is resent with the same value as max_completion_tokens.
  • On a 400 whose error is param: "temperature" with code: "unsupported_value" or "unsupported_parameter", the request is resent without temperature.
  • Each adjustment is applied at most once (at most 3 attempts in total). Any other error, including other 400s that mention max_tokens such as context_length_exceeded, is returned unchanged after a single request, in the same AI provider returned {status}: {body} format.
  • chat_once and chat_stream now share one request path. Streaming only retries on the error status, before any stream data is read.

Matching on error.param + error.code rather than the base URL means OpenAI-compatible proxies that forward OpenAI's errors (LiteLLM, OpenRouter, Azure, etc.) also benefit.

Only src-tauri/src/ai/openai_compat.rs changes; no new dependencies.

Testing

  • New unit tests for the request body (default body unchanged; max_completion_tokens and no-temperature variants), for the error classifier (using the two real OpenAI error bodies above, plus negative cases), and loopback HTTP tests for chat_once and chat_stream: max_tokens → temperature → success, the same parameter rejected twice, and an unrelated 400 producing exactly one request. Each loopback test has a timeout so a regression cannot hang the suite.
  • cargo test (26 passed), cargo check --all-targets and cargo fmt --check were run for the ai module in an isolated crate with the same dependencies. I could not build the full Tauri crate locally (no GTK/WebKit dev libraries), so the full cargo check / cargo test --lib relies on CI here.
  • Against the live OpenAI API: gpt-5.4-mini rejects max_tokens only; gpt-6.2-sol rejects max_tokens and then temperature: 0.1. With max_completion_tokens: 2048 and no temperature, both return normal answers, streaming and non-streaming.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved compatibility with AI services that reject certain chat request settings. Requests can now be adjusted and retried for specific parameter errors in both standard and streaming chats, helping responses complete when the service supports an alternative configuration. Other errors and repeated rejections continue to be reported without further retries.

…ture

OpenAI's newer models (for example gpt-5.4-mini and gpt-6.2-sol) reject
`max_tokens` with a 400 `unsupported_parameter` error and ask for
`max_completion_tokens`; some of them also reject a non-default
`temperature` with `unsupported_value`. The OpenAI-compatible provider
always sends both, so every AI chat with these models failed.

The first request is still sent exactly as before, so other
OpenAI-compatible providers see no change. When the response is a 400
whose error names `max_tokens` with code `unsupported_parameter`, the
request is resent with `max_completion_tokens`; when it names
`temperature` with `unsupported_value` or `unsupported_parameter`, it is
resent without `temperature`. Each adjustment happens at most once (max
three attempts) and any other error is returned unchanged. Both
`chat_once` and `chat_stream` share the new request path; streaming
never retries after the response starts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@qodo-code-review

Copy link
Copy Markdown

ⓘ Qodo reviews are paused because the subscription is no longer active. Ask your workspace admin to reactivate the subscription to resume reviews. Manage billing

@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The OpenAI-compatible provider now shares request construction and sending for one-shot and streaming calls. It retries eligible HTTP 400 errors by switching the token parameter or omitting temperature. Each adjustment is applied at most once, with up to three total attempts.

Changes: Chat Request Compatibility

Layer / File(s) Summary
Request options and error matching
src-tauri/src/ai/openai_compat.rs
Requests can omit temperature and use max_completion_tokens instead of max_tokens. Error parsing recognizes eligible HTTP 400 parameter errors. Tests cover request serialization and error matching.
Shared request sending and retry flow
src-tauri/src/ai/openai_compat.rs
One-shot and streaming calls use the shared request sender. It retries eligible errors within the three-attempt limit and returns other provider errors. Tests cover retry success, streaming, and non-retry cases.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant send_chat_request
  participant ChatAPI
  Caller->>send_chat_request: Submit one-shot or streaming request
  send_chat_request->>ChatAPI: Send chat request
  ChatAPI-->>send_chat_request: Eligible HTTP 400 error
  send_chat_request->>send_chat_request: Apply an unused parameter adjustment
  send_chat_request->>ChatAPI: Retry adjusted request
  ChatAPI-->>send_chat_request: Return response
  send_chat_request-->>Caller: Return response or stream
Loading

Suggested reviewers: codeerrorsleep

Merge Risk: Minimal · up to 22e60

This change makes the OpenAI-compatible provider retry once without an unsupported max_tokens or temperature parameter. The first request is unchanged, so other providers are unaffected, and unrelated errors are still returned as before. No merge-blocking risk was found; CI should confirm the full build.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: OpenAI-compatible requests are retried without the unsupported max_tokens or temperature parameters. The raw summary and objectives confirm this is the primary change.…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: OpenAI-compatible requests are retried without the unsupported max_tokens or temperature parameters. The raw summary and objectives confirm this is the primary change. The title is a single, specific sentence and follows the conventional commit style.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

Clippy (1.99.1)

Clippy execution failed
Clippy (1.99.1) exceeded its 180-second time budget. Findings from completed work are retained; remaining checks are incomplete.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c1edf966-5f0d-4eed-86e9-89febbaef7af
Commits

Reviewing files that changed from the base of the PR and between f7a9a41 and 22e60cc.

Files selected for processing: 1

  • src-tauri/src/ai/openai_compat.rs

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

@codeErrorSleep
codeErrorSleep merged commit 5a66901 into codeErrorSleep:main Nov 20, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants