Skip to content

fix(providers/openai): send max_completion_tokens for every Chat Completions model - #85

Merged
ethanndickson merged 2 commits into
coder_2_33from
issue-31714-max-completion-tokens
Nov 10, 2026
Merged

ethanndickson merged 2 commits into
coder_2_33from
issue-31714-max-completion-tokens

Conversation

@ethanndickson

@ethanndickson ethanndickson commented Nov 10, 2026 •

Copy link
Copy Markdown
Member

Relates to CODAGT-1541

The OpenAI provider sends the output token limit as max_tokens unless the model ID looks like a reasoning model, and that check only knows the o1, o3, o4, oss and gpt-5 names. Reasoning models reject max_tokens, so a newer model, or an Azure deployment name that doesn't say what it serves, fails with:

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

Setting max_completion_tokens on a model that didn't look like a reasoning model also sent both fields.

To fix, DefaultPrepareCallFunc now always sends max_completion_tokens, which OpenAI and Azure OpenAI accept for every chat model, and a configured max_completion_tokens still wins. openaicompat, OpenRouter and Vercel use their own prepare functions, so they keep max_tokens. LangChain does the same rename unconditionally, whereas upstream, the Vercel AI SDK and LiteLLM still go by the model name. I've updated the recorded gpt-4o and gpt-4o-mini requests to match.

One catch: older Azure models such as gpt-4 (turbo-2024-04-09) reject max_completion_tokens, so those deployments need openaicompat, which still sends max_tokens.

…letions model

The OpenAI provider sent the output token limit as max_tokens unless the
model ID looked like a reasoning model. OpenAI and Azure OpenAI deprecate
max_tokens for every chat model and reject it for reasoning models, and a
model ID such as an Azure deployment name cannot reveal one. A configured
max_completion_tokens setting on a non-reasoning model also sent both
fields.

DefaultPrepareCallFunc now always sends max_completion_tokens, and the
max_completion_tokens setting wins. The OpenAI-compatible providers
replace that function, so they keep max_tokens. The recorded gpt-4o and
gpt-4o-mini requests now carry max_completion_tokens.
@linear-code

linear-code Bot commented Nov 10, 2026

Copy link
Copy Markdown

CODAGT-1541

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T07:05:15.420630Z 764e4a1 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@ethanndickson
ethanndickson merged commit 764e4a1 into coder_2_33 Nov 10, 2026
6 checks passed
ethanndickson added a commit to coder/coder that referenced this pull request Nov 10, 2026
The fantasy OpenAI provider sent the output token limit as max_tokens unless the model ID looked like a reasoning model. OpenAI and Azure OpenAI reject max_tokens for reasoning models, and an Azure deployment name cannot show what model it serves. coder/fantasy#85 sends max_completion_tokens on every OpenAI Chat Completions request. The OpenAI-compatible providers keep max_tokens. A new test checks the field on the OpenAI and OpenAI-compatible routes, and the models docs no longer limit Max Completion Tokens to reasoning models.
@ethanndickson

Copy link
Copy Markdown
Member Author

@codex review

@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: 764e4a1985

ℹ️ 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".

ethanndickson added a commit to coder/coder that referenced this pull request Nov 10, 2026
Part of CODAGT-1430
Relates to CODAGT-1541

Stacked on #31743.

This bumps fantasy to pick up coder/fantasy#84 and coder/fantasy#85.

A Chat Completions tool message can only hold text, so fantasy sends
tool result media (images, audio, and now PDFs from #31384) in a `role:
"user"` message right after it. That gives whatever the tool returned
the authority of the user's own words, which Codex flagged on #31383 as
a prompt injection risk for MCP tools that return untrusted files.

coder/fantasy#84 starts that user message with a text part naming the
tool call and saying the content is tool output, not instructions from
the user. It only changes the Chat Completions paths (`openai` without
the Responses API, `openaicompat`, OpenRouter and Vercel); Anthropic and
Responses keep the media inside the tool result. AI Bridge records the
last user message's text as the prompt, so on the step after a tool
returns media, it'll now record the label. I think that's fine for now.

coder/fantasy#85 fixes the output token limit field. The OpenAI provider
sent `max_tokens` unless the model ID looked like a reasoning model, and
reasoning models reject it:

```
Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead.
```

That breaks any reasoning model whose ID fantasy doesn't recognise, e.g.
an Azure deployment name, which doesn't say what model it serves (Azure
providers start using this client in #31952). The OpenAI provider now
sends `max_completion_tokens` on every Chat Completions request, which
is what OpenAI deprecated `max_tokens` in favour of. `openaicompat`,
OpenRouter and Vercel keep `max_tokens`, so self-hosted servers behind
the OpenAI Compatible provider are unaffected. I've added a test that
checks the field on each route, and the models docs no longer say Max
Completion Tokens is only for reasoning models.
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.

1 participant