Skip to content

Send logprobs=True when top_logprobs is set on Chat Completions#3763

Merged
seratch merged 1 commit into
openai:mainfrom
winklemad:fix/chatcompletions-logprobs
Jul 9, 2026
Merged

Send logprobs=True when top_logprobs is set on Chat Completions#3763
seratch merged 1 commit into
openai:mainfrom
winklemad:fix/chatcompletions-logprobs

Conversation

@winklemad

Copy link
Copy Markdown
Contributor

What

The Chat Completions request builder forwards top_logprobs from ModelSettings but never sets logprobs:

"top_logprobs": self._non_null_or_omit(model_settings.top_logprobs),
# there is no "logprobs" key anywhere in create_kwargs

The OpenAI Chat Completions API requires logprobs=true whenever top_logprobs is set, and rejects the request otherwise:

400 - 'top_logprobs' requires 'logprobs' to be set to true

So any run using ModelSettings(top_logprobs=...) with a Chat Completions model fails — even though the code clearly intends logprobs to work (it reads first_choice.logprobs from the response and the stream handler emits logprobs events).

The Responses path is unaffected because it enables logprobs via include (message.output_text.logprobs); this only hit the Chat Completions provider.

Fix

Set logprobs=True whenever top_logprobs is provided, and omit it otherwise so ordinary calls are unchanged.

Tests

Adds two tests to tests/test_logprobs.py that capture the outgoing Chat Completions request kwargs:

  • ModelSettings(top_logprobs=2) → request has logprobs is True and top_logprobs == 2
  • no top_logprobslogprobs is omitted

Both fail on the current code (the request has no logprobs key) and pass with the fix. ruff check, ruff format, and mypy are clean; the model test suite passes.

@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: d8eb3f441e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/models/openai_chatcompletions.py Outdated
@winklemad winklemad force-pushed the fix/chatcompletions-logprobs branch from d8eb3f4 to dc4ce7d Compare July 9, 2026 07:27

@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: dc4ce7d974

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/agents/models/openai_chatcompletions.py
The Chat Completions request forwarded top_logprobs from ModelSettings but never
set logprobs, so any run with ModelSettings(top_logprobs=...) was rejected by the
API (400: 'top_logprobs' requires 'logprobs' to be set to true). The Responses
path already enables logprobs via include, so this only affected the Chat
Completions provider.

Add logprobs=True when top_logprobs is set, but only when the caller has not
already supplied logprobs via extra_args. That keeps extra_args={"logprobs": ...}
working and leaves the existing top_logprobs + extra_args["logprobs"] workaround
intact instead of colliding with the duplicate-arg check. Adds tests covering all
four combinations of top_logprobs and extra_args.
@winklemad winklemad force-pushed the fix/chatcompletions-logprobs branch from dc4ce7d to 8ffce48 Compare July 9, 2026 07:35
@seratch seratch added this to the 0.18.x milestone Jul 9, 2026
@seratch seratch enabled auto-merge (squash) July 9, 2026 07:53
@seratch seratch disabled auto-merge July 9, 2026 07:54
@seratch seratch enabled auto-merge (squash) July 9, 2026 07:54
@seratch seratch merged commit 532acdf into openai:main Jul 9, 2026
9 checks passed
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.

2 participants