Skip to content

fix(openai): send max_completion_tokens for gpt-5 models - #61

Open
hlubek wants to merge 1 commit into
teilomillet:mainfrom
networkteam:fix/openai-gpt5-max-completion-tokens
Open

fix(openai): send max_completion_tokens for gpt-5 models#61
hlubek wants to merge 1 commit into
teilomillet:mainfrom
networkteam:fix/openai-gpt5-max-completion-tokens

Conversation

@hlubek

@hlubek hlubek commented Jun 14, 2026

Copy link
Copy Markdown

Problem

gpt-5-class models reject the max_tokens parameter with HTTP 400 — they require max_completion_tokens instead. Any caller that sets a token budget against a gpt-5 model (e.g. gpt-5, gpt-5-mini, gpt-5.4-2026-03-05) currently gets a 400.

Fix

needsMaxCompletionTokens() already recognizes the o-series and 4o models that need max_completion_tokens; this extends it to gpt-5*. Both SetOption and the PrepareRequest variants route through that single predicate, so every request path is covered by the one change.

Tests

  • Detection table cases for gpt-5, gpt-5-mini, and gpt-5.4-2026-03-05.
  • A PrepareRequest test asserting a gpt-5 request body carries max_completion_tokens and never max_tokens, with the token budget preserved across the conversion.

Summary by Sourcery

Ensure OpenAI provider uses max_completion_tokens for gpt-5-class models instead of max_tokens to avoid API errors.

Tests:

  • Add detection cases confirming gpt-5 variants require max_completion_tokens.
  • Add a PrepareRequest test verifying gpt-5 requests send max_completion_tokens, never max_tokens, while preserving the token budget.

gpt-5-class models reject max_tokens with HTTP 400 and require
max_completion_tokens instead. needsMaxCompletionTokens already
detects o-series and 4o models; extend it to gpt-5 (gpt-5,
gpt-5-mini, gpt-5.4-2026-03-05, ...). Both SetOption and the
PrepareRequest variants route through this single predicate, so
every request path is covered by the one change.

Adds table cases for gpt-5 detection plus a PrepareRequest test
asserting the request body carries max_completion_tokens and never
max_tokens.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@sourcery-ai

sourcery-ai Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Extends OpenAI provider handling so that all gpt-5* models use max_completion_tokens instead of max_tokens, and adds tests to verify detection and request preparation behavior.

Sequence diagram for OpenAIProvider request preparation using max_completion_tokens for gpt-5 models

sequenceDiagram
    actor Caller
    participant OpenAIProvider

    Caller->>OpenAIProvider: SetOption(max_tokens)
    OpenAIProvider->>OpenAIProvider: needsMaxCompletionTokens()
    alt model is gpt-5*
        OpenAIProvider->>OpenAIProvider: store max_completion_tokens
    else other model
        OpenAIProvider->>OpenAIProvider: store max_tokens
    end

    Caller->>OpenAIProvider: PrepareRequest()
    OpenAIProvider->>OpenAIProvider: needsMaxCompletionTokens()
    alt model is gpt-5*
        OpenAIProvider-->>Caller: request body with max_completion_tokens
    else other model
        OpenAIProvider-->>Caller: request body with max_tokens
    end
Loading

File-Level Changes

Change Details Files
Update model detection to treat all gpt-5* chat models as requiring max_completion_tokens instead of max_tokens.
  • Extend needsMaxCompletionTokens predicate to return true when the configured model name starts with gpt-5.
  • Document in a comment that gpt-5-class models reject max_tokens with HTTP 400 and give examples of affected model IDs.
providers/openai.go
Add unit tests to ensure gpt-5* models are recognized correctly and that requests for these models send max_completion_tokens only.
  • Add gpt-5, gpt-5-mini, and gpt-5.4-2026-03-05 cases to the existing TestNeedsMaxCompletionTokens table.
  • Add TestPrepareRequestGPT5UsesMaxCompletionTokens to assert that PrepareRequest for a gpt-5* model strips max_tokens, sets max_completion_tokens, and preserves the configured token budget.
providers/openai_test.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

hlubek added a commit to networkteam/sdd that referenced this pull request Jun 16, 2026
Repins the networkteam/gollm replace to 19e75fe, which teaches the
OpenAI provider to emit max_completion_tokens for gpt-5-class models
(upstream PR teilomillet/gollm#61). Fixes the HTTP 400 that broke
OpenAI gpt-5 summary and pre-flight calls (s-tac-t96).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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