Skip to content

fix(ai): GPT-5 fails when streaming — retry with max_completion_tokens on 400 - #2774

Open
midozy wants to merge 2 commits into
ryanbr:mainfrom
midozy:fix/openai-stream-max-completion-tokens
Open

midozy wants to merge 2 commits into
ryanbr:mainfrom
midozy:fix/openai-stream-max-completion-tokens

Conversation

@midozy

@midozy midozy commented Oct 12, 2026 •

Copy link
Copy Markdown

Problem

Using GPT-5 (or any reasoning model) with the OpenAI or Custom provider on iOS fails with an unsupported max_tokens / max_completion_tokens error.

The chat reply goes through stream(), which always sends max_tokens + temperature and has no fallback. The non-streaming send() already retries with max_completion_tokens and no temperature, and the doc comment on stream() says it falls back to send's retry, but the code never did.

Fix

In OpenAIClient.stream and CustomClient.stream, catch a 400 whose body mentions max_completion_tokens, max_tokens, temperature or unsupported (same match as send). That error arrives before any delta, so fall back to send() (which retries with the modern params) and emit the reply as one delta. Other errors are rethrown unchanged.

Trade-off: those models' replies appear all at once instead of streaming. Switching the streaming request itself to the modern params on retry would be a larger change; happy to do that if preferred.

Second commit

Raises timeoutInterval to 300s on the non-streaming OpenAI/Custom chat requests so the fallback does not time out while a reasoning model is thinking.

Testing

  • xcodebuild -scheme NOOPiOS builds clean.
  • Installed on a device (iPhone 16 Pro Max) and tested end to end with a live GPT-5 call: it now replies with no error. Before the second commit it failed with a network error, because GPT-5 can take over 60s to send the first byte and hit the default URLSession idle timeout; the second commit raises the timeout to 300s on the non-streaming chat requests (OpenAI and Custom).

Not touched: Android. AiCoach.kt has its own request path; I haven't checked it for the same gap.

midozy and others added 2 commits October 12, 2026 08:32
…okens on 400

The streaming path always sent max_tokens + temperature and had no fallback,
so GPT-5 / reasoning models failed with an 'unsupported parameter: max_tokens'
error. The non-streaming send() already retries with max_completion_tokens;
the streaming comment claimed to fall back to it but never did.

On a 400 naming max_tokens/max_completion_tokens/temperature/unsupported
(raised before any delta is emitted), fall back to send() and emit the reply
as a single delta. Applied to OpenAIClient and CustomClient.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015crzUUNTK64Y8i8QgJDkaz
GPT-5 can take over 60s before sending the first byte, which tripped the
default URLSession idle timeout and surfaced as a network error on the
non-streaming fallback.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015crzUUNTK64Y8i8QgJDkaz
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.

1 participant