Skip to content

fix(responses): carry logprobs across the chat completions bridge - #49536

Open
mateo-berri wants to merge 16 commits into
mainfrom
litellm_responses_bridge_logprobs
Open

mateo-berri wants to merge 16 commits into
mainfrom
litellm_responses_bridge_logprobs

Conversation

@mateo-berri

@mateo-berri mateo-berri commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Handwritten 1-3 sentence description

Details

TLDR

Problem this solves:

  • Responses top_logprobs on a chat-bridged deployment 400s on OpenAI chat and Vertex Gemini
  • Chat choices[].logprobs never reached Responses output_text.logprobs, non-stream or stream

How it solves it:

  • The bridge sends logprobs: true alongside top_logprobs
  • choices[].logprobs becomes output_text.logprobs on the response
  • Stream events output_text.delta, output_text.done, content_part.done carry the same tokens
  • logprobs is only present when the chat choice sent some, so other bridged replies are unchanged
  • include: ["message.output_text.logprobs"] without top_logprobs also turns on chat logprobs, the way OpenAI's native Responses API returns token probabilities for it

User Flow

Before: a developer asking a chat-only deployment for token probabilities through the Responses API gets a 400

  1. They send POST https://litellm-domain/v1/responses with "model": "vertex_ai/gemini-2.5-flash", "input": "Say hello.", "top_logprobs": 2
  2. The proxy answers 400 logprobs is not supported without setting response_logprobs to be true (OpenAI chat deployments say 'top_logprobs' is only allowed when 'logprobs' is enabled)
  3. With "stream": true the same request opens the stream and the first event is response.failed with that 400

After: the same request answers with the probabilities

  1. They send the same POST https://litellm-domain/v1/responses with "top_logprobs": 2
  2. The proxy answers 200 and output[0].content[0].logprobs lists each token with its logprob and two top_logprobs alternatives
  3. With "stream": true every response.output_text.delta carries that chunk's logprobs, and response.output_text.done and response.content_part.done carry the whole list

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Two proxies, one per commit, on the same config: vertex_ai/gemini-2.5-flash (Vertex service account, location global) and openai-chat/gpt-5.4-mini (openai/gpt-5.4-mini with use_chat_completions_api: true), no database, no mocks. Request body for every case:

{"model": "<deployment>", "instructions": "You are a terse assistant.", "input": "Say hello.", "max_output_tokens": 1024, "top_logprobs": 2}

Before (9312a25)

Vertex Gemini, non-stream

  1. curl -s http://127.0.0.1:4101/v1/responses -H "Authorization: Bearer $KEY" -H "content-type: application/json" -d '<body>'
  2. 400 Vertex_aiException BadRequestError - "Unable to submit request because logprobs is not supported without setting response_logprobs to be true"

OpenAI chat, non-stream

  1. Same curl with "model": "openai-chat/gpt-5.4-mini"
  2. 400 OpenAIException - The 'top_logprobs' parameter is only allowed when 'logprobs' is enabled.

OpenAI chat, stream

  1. Same curl with "stream": true
  2. The body is that same 400 error object, no events

Fake-provider translation case

  1. python tests/integration/run.py providers tests/integration/translation/responses/sampling/test_responses_sampling_vertex_ai.py
  2. FAILED, the body sent to Gemini is "generationConfig": {"max_output_tokens": 1024, "logprobs": 2} with no responseLogprobs

After (0e0b8bd)

Vertex Gemini, non-stream

  1. Same curl against the head proxy on port 4102
  2. 200, status: completed, text Hello., and output[0].content[0].logprobs:
[{"token": "Hello", "logprob": -0.02188889, "bytes": null, "top_logprobs": [{"token": "Hello", "logprob": -0.02188889, "bytes": null}, {"token": "Hi", "logprob": -4.384569, "bytes": null}]},
 {"token": ".", "logprob": -2.0860223e-05, "bytes": null, "top_logprobs": [{"token": ".", "logprob": -2.0860223e-05, "bytes": null}, {"token": "<ctrl100>", "logprob": -11.861645, "bytes": null}]}]

OpenAI chat, non-stream

  1. Same curl with "model": "openai-chat/gpt-5.4-mini"
  2. 200, text Hello., output[0].content[0].logprobs:
[{"token": "Hello", "logprob": -3.814697265625e-06, "bytes": [72, 101, 108, 108, 111], "top_logprobs": [{"token": "Hello", "logprob": -3.814697265625e-06, "bytes": [72, 101, 108, 108, 111]}]},
 {"token": ".", "logprob": -0.01387786865234375, "bytes": [46], "top_logprobs": [{"token": ".", "logprob": -0.01387786865234375, "bytes": [46]}]}]

OpenAI chat, stream

  1. Same curl with "stream": true
  2. response.output_text.delta for Hello carries "logprobs": [{"token": "Hello", "logprob": -3.814697265625e-06, "bytes": [72, 101, 108, 108, 111], "top_logprobs": [...]}]
  3. response.output_text.done and response.content_part.done carry both tokens in logprobs

Fake-provider translation cases

  1. python tests/integration/run.py providers tests/integration/translation/responses/sampling/test_responses_sampling_vertex_ai.py "tests/integration/translation/responses/basic/test_responses_basic_vertex_ai.py::test_responses_basic_vertex_ai[vertex_ai/gemini-2.5-flash-basic]"
  2. 2 passed. The top_logprobs case replays a real Vertex logprobsResult captured with this exact request body and checks the request now carries "responseLogprobs": true, "logprobs": 2 and the response carries the mapped output_text.logprobs

Unit tests

  1. pytest tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py tests/unit/responses/litellm_completion_transformation/test_streaming_iterator_transformation.py
  2. 512 passed, 6 of them new (request mapping, response mapping, stream events, and the no-logprobs shapes staying unchanged)

Commits after 0e0b8bd regenerate litellm/proxy/_lazy_openapi_snapshot.json and ui/litellm-dashboard/src/lib/http/schema.d.ts for the new OutputTextLogprob types (040f5cb, cd3c338, required by the ui-api-types job) and make bytes NotRequired on the two logprob TypedDicts (1988c24, with the snapshot and schema.d.ts regenerated for it in 4580538) so a ContentPartDoneEvent validated from an OpenAI-shaped part without bytes still parses, which tests/unit/types/llms/test_types_llms_openai.py already checked

Review follow-ups (3588a1e and d9b67d8)

Runtime changes from the Greptile and Veria rounds:

  • response.completed is rebuilt by stream_chunk_builder, which drops chat logprobs, so the snapshot's message text part now takes the tokens the stream carried. Only the message item takes them, a reasoning item built from reasoning_content keeps none
  • The per-chunk accumulator appends and flattens once instead of recopying every earlier token
  • A guardrail rewrite of output_text (content_part, output_text.delta, output_text.done) also clears logprobs, which would otherwise spell out the original text token by token. A guardrail that returns the text unchanged keeps them
  • CarriesOutputTextLogprobs carries a frozen-ok reason, guardrails rewrite these objects in place
  • The wrap model_serializer has no return annotation anymore. With one, pydantic replaced the OutputText, OutputTextDeltaEvent and OutputTextDoneEvent OpenAPI schemas with a bare object, so the regenerated schema.d.ts lost type, text and annotations. Now it keeps them plus logprobs

Live at d9b67d8, two proxies on one config (base 9312a25, head d9b67d8), real Postgres, real OpenAI and Anthropic, no mocks

openai-bridge is openai/gpt-5.4-mini with use_chat_completions_api: true, input Reply with exactly the word Hi

Case Base 9312a25 Head d9b67d8
openai-bridge non-stream top_logprobs: 1 400 'top_logprobs' parameter is only allowed when 'logprobs' is enabled 200, output[0].content[0].logprobs = [{"token": "Hi", "logprob": -0.00069, "bytes": [72, 105], "top_logprobs": [...]}]
openai-bridge stream top_logprobs: 1 400 output_text.delta, output_text.done, content_part.done and the response.completed snapshot all carry the Hi token
openai-bridge no top_logprobs, both modes 200, no logprobs key 200, no logprobs key (unchanged)
openai/gpt-5.4-mini native Responses 200, logprobs: [] 200, logprobs: [] (unchanged)
anthropic/claude-haiku-5-5 bridge top_logprobs: 1 400 does not support parameters: ['top_logprobs'] 400 does not support parameters: ['logprobs', 'top_logprobs']
same with drop_params: true 200, no logprobs key 200, no logprobs key (unchanged)
post_call custom guardrail that masks Hi, non-stream top_logprobs: 1 400 (never reaches the guardrail) 200, text [MASKED], no logprobs key
post_call custom guardrail that returns text unchanged, non-stream top_logprobs: 1 400 200, text Hi, logprobs kept (1 token)
masking guardrail, stream top_logprobs: 1 (streaming_transform_mode: incremental_diff, end of stream) 400 response.completed text [MASKED], no logprobs key. Earlier delta/done events were already on the wire before the end-of-stream rewrite, same as for any text rewrite

Unit tests at d9b67d8

  1. pytest tests/unit/llms/openai/responses/test_openai_responses_guardrail_handler.py tests/unit/responses/litellm_completion_transformation/test_streaming_iterator_transformation.py tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py tests/unit/types/llms/test_types_llms_openai.py
  2. 803 passed. New since 0e0b8bd: completed-snapshot logprobs, reasoning item keeps none, masked text drops logprobs (non-stream and the three stream events), unchanged text keeps them

Review follow-up (9686000): include without top_logprobs

OpenAI's native Responses API returns output_text.logprobs for "include": ["message.output_text.logprobs"] with no top_logprobs (checked live on gpt-4.1-mini, nine tokens, each with "top_logprobs": []). The bridge only turned on chat logprobs for top_logprobs, so that request came back without probabilities. _requests_output_text_logprobs now treats either field as the ask

Live, two proxies on one config (base 9312a25 on 4101, head 9686000 on 4102), real OpenAI and Vertex, no mocks

Request body: {"model": "<deployment>", "instructions": "You are a terse assistant.", "input": "Say hello.", "max_output_tokens": 1024, "include": ["message.output_text.logprobs"]}

  1. openai-chat/gpt-4.1-mini on 4101: 200, text Hello., output[0].content[0] has no logprobs key
  2. openai-chat/gpt-4.1-mini on 4102: 200, text Hello., output[0].content[0].logprobs is [{"token": "Hello", "logprob": 0.0, "bytes": [72, 101, 108, 108, 111], "top_logprobs": []}, {"token": ".", "logprob": -2.288818359375e-05, "bytes": [46], "top_logprobs": []}]
  3. vertex_ai/gemini-2.5-flash on 4101 and 4102: 200 with no logprobs key on both, see the Gemini caveat below
  4. Regression check, same two proxies with "top_logprobs": 2 instead: 4101 400 logprobs is not supported without setting response_logprobs to be true, 4102 200 with two alternatives per token, unchanged from the earlier proof

Fake-provider translation case

  1. python tests/integration/run.py providers tests/integration/translation/responses/sampling/test_responses_sampling_vertex_ai.py
  2. Rig proxy on the previous tip: include_output_text_logprobs FAILED, Gemini got "generationConfig": {"max_output_tokens": 1024} with no responseLogprobs
  3. Rig proxy on 9686000: 2 passed, the body carries "responseLogprobs": true, plus 13 passed in basic/test_responses_basic_vertex_ai.py

Unit tests at 9686000

uv run pytest tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py -k logprobs, 5 passed, including the new test_responses_include_output_text_logprobs_bridges_to_chat_logprobs

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • Vertex Gemini returns no logprobsResult when responseLogprobs is true without a logprobs count (checked with a direct generateContent call, responseLogprobs alone gives no logprobsResult, responseLogprobs plus logprobs: 1 does), so include: ["message.output_text.logprobs"] alone on Vertex Gemini still answers without output_text.logprobs. Add top_logprobs there. Gemini's own behavior, outside this bridge
  • Vertex Gemini rejects response_logprobs on streamGenerateContent (the model does not support the response_logprobs parameter), so a streamed Responses call with top_logprobs on Vertex Gemini 400s before and after. Gemini's own limit, proven live on gemini-2.5-flash

Low

  • output_text.logprobs is omitted when the chat choice sent none. OpenAI's native Responses API sends "logprobs": [] there. Emitting [] would change every bridged response, so this PR keeps the field absent instead
  • Gemini gives no bytes per token, so bytes is null on Gemini logprobs. OpenAI chat fills it

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Link to Devin session: https://app.devin.ai/sessions/09cdd950360c40bfa37dbf9e3bd4d9c8
Open in Devin Desktop: https://app.devin.ai/desktop/session/09cdd950360c40bfa37dbf9e3bd4d9c8?variant=devin
Requested by: @mateo-berri


Note

Medium Risk
Touches the widely used Responses↔chat bridge, streaming assembly, and in-place guardrail stream rewrites; incorrect logprob handling could change API shapes or leak pre-redaction tokens via logprobs.

Overview
Responses requests that ask for token probabilities (top_logprobs or include: ["message.output_text.logprobs"]) now turn on chat logprobs on the bridged completion call, fixing provider 400s that required both flags. Chat choices[].logprobs are mapped into output_text.logprobs on non-stream responses and on stream events (output_text.delta, output_text.done, content_part.done), with accumulated tokens attached to the response.completed snapshot when the chunk builder drops them.

New OutputTextLogprob types and OpenAPI/schema updates document the field; logprobs is omitted from JSON when absent (not an empty array). Guardrail rewrites of output text clear logprobs so token lists cannot leak the pre-masked text; unchanged text keeps them.

Reviewed by Cursor Bugbot for commit 1bc94cb. Bugbot is set up for automated code reviews on this repo. Configure here.

mateo-berri and others added 4 commits October 31, 2026 23:35
A /v1/responses request with top_logprobs on a chat-bridged deployment sent top_logprobs without logprobs, which OpenAI-style chat APIs and Vertex Gemini reject, and the bridge dropped the chat choice's logprobs on the way back. The bridge now sends logprobs with top_logprobs and copies choice.logprobs into output_text.logprobs, on the response and on the output_text.delta, output_text.done and content_part.done stream events

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
… lint gates

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…e type gate

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…the type gate

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@veria-ai

veria-ai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Security review

No open security issues remain on this pull request.

Fixed/addressed: 1 · PR risk: 0/10

mateo-berri and others added 2 commits October 31, 2026 23:56
…put_text logprobs

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@greptile-apps

greptile-apps Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with the remaining previous finding addressed and no new issues found

Summary

The PR carries chat token probabilities into Responses text and stream events. It also clears those probabilities when guardrails replace the text

  • Responses calls through the chat bridge can request and return token probabilities.
  • Streamed Responses text carries token probabilities through to the completed message.
  • Guardrail rewrites remove token probabilities for text they replace.

Reviews (6) · Last reviewed commit: "refactor(responses): drop docstrings tha..." · Reviewed by Greptile

Comment thread litellm/types/llms/base.py Resolved
@codecov

codecov Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.65217% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...nses/litellm_completion_transformation/logprobs.py 82.60% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

mateo-berri and others added 3 commits November 1, 2026 00:07
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…ional logprob bytes

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…nd drop them on guardrail rewrites

Greptile review follow-ups on the bridge logprobs change. The response.completed
snapshot is rebuilt through stream_chunk_builder, which drops chat logprobs, so the
text part now takes the logprobs the stream carried. The per-chunk accumulator appends
and flattens once instead of recopying every earlier token. CarriesOutputTextLogprobs
carries a frozen-ok reason since guardrails rewrite text parts and stream events in
place. A guardrail rewrite of output_text (part, delta, done) now also clears the
logprobs that would spell out the original text.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

mateo-berri and others added 3 commits November 1, 2026 00:24
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…ing items, and in the OpenAPI schema

Second Greptile round on the bridge logprobs change. A guardrail that writes the
text back unchanged keeps the logprobs, only a real rewrite clears them. The
response.completed snapshot attaches the streamed answer tokens to the message
item only, so a reasoning item built from reasoning_content carries none. The
wrap model_serializer has no return annotation anymore, since pydantic used it as
the serialization schema and collapsed OutputText and the output_text events to a
bare object in the OpenAPI snapshot and schema.d.ts, which are regenerated here.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor 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.

Stale Bugbot comment from a previous run.

@codspeed

codspeed Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 43 untouched benchmarks


Comparing litellm_responses_bridge_logprobs (1bc94cb) with main (6be98d8)

Open in CodSpeed

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@mateo-berri

Copy link
Copy Markdown
Contributor Author

In a live check, include with message.output_text.logprobs alone returns no logprobs through this bridge, while OpenAI's own Responses API returns them

…t_text.logprobs

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Confirmed and fixed in 9686000. include alone now turns on chat logprobs, live OpenAI chat proof in the body. Gemini itself needs a logprobs count, noted as a caveat

@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor 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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 1bc94cb. Configure here.

@CLAassistant

CLAassistant commented Nov 5, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

This branch has not been deployed

No deployments
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