Repository navigation
fix(responses): carry logprobs across the chat completions bridge - #49536
mateo-berri wants to merge 16 commits into
Conversation
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>
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
PR overviewAll previously flagged issues have been addressed. No open security concerns remain on this pull request. Security reviewNo open security issues remain on this pull request. Fixed/addressed: 1 · PR risk: 0/10 |
…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>
[Medium impact] The PR appears safe to merge, with the remaining previous finding addressed and no new issues found SummaryThe PR carries chat token probabilities into Responses text and stream events. It also clears those probabilities when guardrails replace the text
Reviews (6) · Last reviewed commit: "refactor(responses): drop docstrings tha..." · Reviewed by Greptile |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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>
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>
|
bugbot run |
Merging this PR will not alter performance1 warning
Comparing |
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
In a live check, |
…t_text.logprobs Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
Confirmed and fixed in 9686000. |
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>
|
bugbot run |
There was a problem hiding this comment.
✅ 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.
Handwritten 1-3 sentence description
Details
TLDR
Problem this solves:
top_logprobson a chat-bridged deployment 400s on OpenAI chat and Vertex Geminichoices[].logprobsnever reached Responsesoutput_text.logprobs, non-stream or streamHow it solves it:
logprobs: truealongsidetop_logprobschoices[].logprobsbecomesoutput_text.logprobson the responseoutput_text.delta,output_text.done,content_part.donecarry the same tokenslogprobsis only present when the chat choice sent some, so other bridged replies are unchangedinclude: ["message.output_text.logprobs"]withouttop_logprobsalso turns on chatlogprobs, the way OpenAI's native Responses API returns token probabilities for itUser Flow
Before: a developer asking a chat-only deployment for token probabilities through the Responses API gets a 400
"model": "vertex_ai/gemini-2.5-flash","input": "Say hello.","top_logprobs": 2logprobs is not supported without setting response_logprobs to be true(OpenAI chat deployments say'top_logprobs' is only allowed when 'logprobs' is enabled)"stream": truethe same request opens the stream and the first event isresponse.failedwith that 400After: the same request answers with the probabilities
"top_logprobs": 2output[0].content[0].logprobslists each token with itslogproband twotop_logprobsalternatives"stream": trueeveryresponse.output_text.deltacarries that chunk'slogprobs, andresponse.output_text.doneandresponse.content_part.donecarry the whole listPre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
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 moreDelays 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) andopenai-chat/gpt-5.4-mini(openai/gpt-5.4-miniwithuse_chat_completions_api: true), no database, no mocks. Request body for every case:Before (9312a25)
Vertex Gemini, non-stream
curl -s http://127.0.0.1:4101/v1/responses -H "Authorization: Bearer $KEY" -H "content-type: application/json" -d '<body>'Vertex_aiException BadRequestError - "Unable to submit request because logprobs is not supported without setting response_logprobs to be true"OpenAI chat, non-stream
"model": "openai-chat/gpt-5.4-mini"OpenAIException - The 'top_logprobs' parameter is only allowed when 'logprobs' is enabled.OpenAI chat, stream
"stream": trueFake-provider translation case
python tests/integration/run.py providers tests/integration/translation/responses/sampling/test_responses_sampling_vertex_ai.py"generationConfig": {"max_output_tokens": 1024, "logprobs": 2}with noresponseLogprobsAfter (0e0b8bd)
Vertex Gemini, non-stream
status: completed, textHello., andoutput[0].content[0].logprobs:OpenAI chat, non-stream
"model": "openai-chat/gpt-5.4-mini"Hello.,output[0].content[0].logprobs:OpenAI chat, stream
"stream": trueresponse.output_text.deltaforHellocarries"logprobs": [{"token": "Hello", "logprob": -3.814697265625e-06, "bytes": [72, 101, 108, 108, 111], "top_logprobs": [...]}]response.output_text.doneandresponse.content_part.donecarry both tokens inlogprobsFake-provider translation cases
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]"logprobsResultcaptured with this exact request body and checks the request now carries"responseLogprobs": true, "logprobs": 2and the response carries the mappedoutput_text.logprobsUnit tests
pytest tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py tests/unit/responses/litellm_completion_transformation/test_streaming_iterator_transformation.pyCommits after 0e0b8bd regenerate
litellm/proxy/_lazy_openapi_snapshot.jsonandui/litellm-dashboard/src/lib/http/schema.d.tsfor the newOutputTextLogprobtypes (040f5cb, cd3c338, required by the ui-api-types job) and makebytesNotRequiredon the two logprob TypedDicts (1988c24, with the snapshot and schema.d.ts regenerated for it in 4580538) so aContentPartDoneEventvalidated from an OpenAI-shaped part withoutbytesstill parses, whichtests/unit/types/llms/test_types_llms_openai.pyalready checkedReview follow-ups (3588a1e and d9b67d8)
Runtime changes from the Greptile and Veria rounds:
response.completedis rebuilt bystream_chunk_builder, which drops chatlogprobs, so the snapshot's message text part now takes the tokens the stream carried. Only themessageitem takes them, areasoningitem built fromreasoning_contentkeeps noneoutput_text(content_part,output_text.delta,output_text.done) also clearslogprobs, which would otherwise spell out the original text token by token. A guardrail that returns the text unchanged keeps themCarriesOutputTextLogprobscarries afrozen-okreason, guardrails rewrite these objects in placemodel_serializerhas no return annotation anymore. With one, pydantic replaced theOutputText,OutputTextDeltaEventandOutputTextDoneEventOpenAPI schemas with a bare object, so the regeneratedschema.d.tslosttype,textandannotations. Now it keeps them pluslogprobsLive at d9b67d8, two proxies on one config (base 9312a25, head d9b67d8), real Postgres, real OpenAI and Anthropic, no mocks
openai-bridgeisopenai/gpt-5.4-miniwithuse_chat_completions_api: true, inputReply with exactly the word Hiopenai-bridgenon-streamtop_logprobs: 1'top_logprobs' parameter is only allowed when 'logprobs' is enabledoutput[0].content[0].logprobs=[{"token": "Hi", "logprob": -0.00069, "bytes": [72, 105], "top_logprobs": [...]}]openai-bridgestreamtop_logprobs: 1output_text.delta,output_text.done,content_part.doneand theresponse.completedsnapshot all carry theHitokenopenai-bridgenotop_logprobs, both modeslogprobskeylogprobskey (unchanged)openai/gpt-5.4-mininative Responseslogprobs: []logprobs: [](unchanged)anthropic/claude-haiku-5-5bridgetop_logprobs: 1does not support parameters: ['top_logprobs']does not support parameters: ['logprobs', 'top_logprobs']drop_params: truelogprobskeylogprobskey (unchanged)Hi, non-streamtop_logprobs: 1[MASKED], nologprobskeytop_logprobs: 1Hi,logprobskept (1 token)top_logprobs: 1(streaming_transform_mode: incremental_diff, end of stream)response.completedtext[MASKED], nologprobskey. Earlierdelta/doneevents were already on the wire before the end-of-stream rewrite, same as for any text rewriteUnit tests at d9b67d8
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.pyReview follow-up (9686000):
includewithouttop_logprobsOpenAI's native Responses API returns
output_text.logprobsfor"include": ["message.output_text.logprobs"]with notop_logprobs(checked live ongpt-4.1-mini, nine tokens, each with"top_logprobs": []). The bridge only turned on chatlogprobsfortop_logprobs, so that request came back without probabilities._requests_output_text_logprobsnow treats either field as the askLive, 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"]}openai-chat/gpt-4.1-minion 4101: 200, textHello.,output[0].content[0]has nologprobskeyopenai-chat/gpt-4.1-minion 4102: 200, textHello.,output[0].content[0].logprobsis[{"token": "Hello", "logprob": 0.0, "bytes": [72, 101, 108, 108, 111], "top_logprobs": []}, {"token": ".", "logprob": -2.288818359375e-05, "bytes": [46], "top_logprobs": []}]vertex_ai/gemini-2.5-flashon 4101 and 4102: 200 with nologprobskey on both, see the Gemini caveat below"top_logprobs": 2instead: 4101 400logprobs is not supported without setting response_logprobs to be true, 4102 200 with two alternatives per token, unchanged from the earlier proofFake-provider translation case
python tests/integration/run.py providers tests/integration/translation/responses/sampling/test_responses_sampling_vertex_ai.pyinclude_output_text_logprobsFAILED, Gemini got"generationConfig": {"max_output_tokens": 1024}with noresponseLogprobs"responseLogprobs": true, plus 13 passed inbasic/test_responses_basic_vertex_ai.pyUnit tests at 9686000
uv run pytest tests/unit/responses/litellm_completion_transformation/test_litellm_completion_responses.py -k logprobs, 5 passed, including the newtest_responses_include_output_text_logprobs_bridges_to_chat_logprobsType
🐛 Bug Fix
Caveats (if any)
Medium
logprobsResultwhenresponseLogprobsis true without alogprobscount (checked with a directgenerateContentcall,responseLogprobsalone gives nologprobsResult,responseLogprobspluslogprobs: 1does), soinclude: ["message.output_text.logprobs"]alone on Vertex Gemini still answers withoutoutput_text.logprobs. Addtop_logprobsthere. Gemini's own behavior, outside this bridgeresponse_logprobsonstreamGenerateContent(the model does not support the response_logprobs parameter), so a streamed Responses call withtop_logprobson Vertex Gemini 400s before and after. Gemini's own limit, proven live ongemini-2.5-flashLow
output_text.logprobsis 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 insteadbytesper token, sobytesisnullon Gemini logprobs. OpenAI chat fills itFinal Attestation
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_logprobsorinclude: ["message.output_text.logprobs"]) now turn on chatlogprobson the bridged completion call, fixing provider 400s that required both flags. Chatchoices[].logprobsare mapped intooutput_text.logprobson non-stream responses and on stream events (output_text.delta,output_text.done,content_part.done), with accumulated tokens attached to theresponse.completedsnapshot when the chunk builder drops them.New
OutputTextLogprobtypes and OpenAPI/schema updates document the field;logprobsis omitted from JSON when absent (not an empty array). Guardrail rewrites of output text clearlogprobsso 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.