Skip to content

fix(chatgpt): preserve streamed output in chat bridge - #43412

Closed
danielphang wants to merge 7 commits into
BerriAI:mainfrom
danielphang:bugfix/chatgpt-stream-output-bridge
Closed

danielphang wants to merge 7 commits into
BerriAI:mainfrom
danielphang:bugfix/chatgpt-stream-output-bridge

Conversation

@danielphang

@danielphang danielphang commented Sep 27, 2026 •

Copy link
Copy Markdown

TLDR

Problem this solves:

  • Forced-SSE ChatGPT answers can end with empty output
  • Non-streaming chat calls then return HTTP 500

How it solves it:

  • Preserve recoverable events while consuming the response stream
  • Rebuild empty output, preserving text and citation positions

User Flow

Before: a developer's non-streaming ChatGPT request loses the completed answer and fails

  1. They send POST http://localhost:4000/v1/chat/completions with model gpt-5.6-sol using the default non-streaming behavior
  2. The proxy returns HTTP 500 with Unknown items in responses API response: []
  3. Their application receives no assistant message

After: the same request returns the completed assistant answer

  1. They send POST http://localhost:4000/v1/chat/completions with model gpt-5.6-sol using the default non-streaming behavior
  2. The proxy returns HTTP 200 with the Chat Completions response
  3. Their application receives the assistant message normally

Relevant issues

Relationship to #41014

Both PRs fix the same bridge failure by restoring streamed data when the terminal response.output is empty

Aspect PR #41014 This PR
Collection point Shared Responses streaming iterator Responses-to-Chat bridge
Recovered events response.output_item.done response.output_item.done and response.output_text.done
Stored state New mutable accumulator and getter Bridge-local tuple of recoverable events
Output reconstruction Assign accumulated output items Reuse the raw SSE recovery helper

This route keeps the state at the conversion boundary where a non-streaming Chat Completions response is assembled. It also gives raw SSE and bridge-collected events one recovery implementation, and can synthesize output from response.output_text.done when a provider omits the complete response.output_item.done item

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
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Focused validation: 140 passed across the two Responses transformation test files

Local patch line coverage: all 55 changed executable source lines covered (100%). Recovery tests exercise SDK event objects, full and partial dictionary terminals, unchanged input and headers, complete-output bypass, and empty-output preservation. Hosted codecov/patch passed on e9c0037be3 at 100.00% after all coverage uploads completed (required: 82.39%)

The citation regression covers flat and nested annotations in both raw-SSE and bridge recovery, including out-of-order events and input preservation. Restoring the previous converter makes all four cases fail on incorrect citation positions; the fixed converter passes all four

The sync and async recovery regressions consume serialized SSE through the real Responses iterator, covering both output-item and text-only completion events

Repository validation: make lint passed

Greptile reviewed e9c0037be3 at 5/5 with no remaining findings. Veria passed on the same tip. codecov/patch passed on the same tip. All hosted CI checks completed without failures. No Bugbot review is available, so it is not counted as a pass. The CLA bot still reports unsigned; the author must complete that separately

Screenshots / Proof of Fix

Shared setup: gpt-5.6-sol maps to chatgpt/gpt-5.6-sol; the request sets disable_fallbacks: true

Before (303434d)

  1. Started the proxy from the merge base on http://127.0.0.1:49890
  2. Sent POST http://127.0.0.1:49890/v1/chat/completions with {"model":"gpt-5.6-sol","messages":[{"role":"user","content":"respond with HELLO WORLD"}],"max_tokens":8,"temperature":0,"disable_fallbacks":true}
  3. Observed HTTP 500 with {"error":{"message":"litellm.APIConnectionError: APIConnectionError: ChatgptException - Unknown items in responses API response: []","type":"internal_server_error","param":null,"code":"500"}}

After (e9c0037be3)

  1. Started the proxy from the PR tip on http://127.0.0.1:49889
  2. Sent POST http://127.0.0.1:49889/v1/chat/completions with the same payload
  3. Observed HTTP 200, x-litellm-model-id: chatgpt-gpt-5-6-sol, x-litellm-attempted-fallbacks: 0, and {"model":"gpt-5.6-sol","content":"HELLO WORLD"}

Type

Bug Fix

Caveats (if any)

Low

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

@danielphang
danielphang requested a review from a team September 27, 2026 04:05
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@greptile-apps

greptile-apps Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Fixes streamed output recovery in chat response transformation.

The PR appears safe to merge based on the reviewed changes

Summary

The PR recovers completed output from streamed Responses events when a non-streaming chat call receives an empty terminal output.

  • Reuses recovery logic for raw SSE and bridge-collected events
  • Joins recovered text parts and adjusts citation positions
  • Adds sync, async, and citation regression coverage

Reviews (7) · Last reviewed commit: "test(responses): complete recovery regre..."

@codspeed

codspeed Bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing danielphang:bugfix/chatgpt-stream-output-bridge (cfc7d48) with main (2c9b0e0)

Open in CodSpeed

@codecov

codecov Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@danielphang

Copy link
Copy Markdown
Author

@greptileai Please review commit d0b0247; it addresses all three findings, adds multi-part recovery coverage, and passes the full lint/type gate

@danielphang

Copy link
Copy Markdown
Author

@greptileai Please review 76e5741: citation offsets and immutable construction are fixed, with 134 passing tests, lint checks, and refreshed live proof

@danielphang

Copy link
Copy Markdown
Author

Added recovery compatibility tests in 4f3973c. Local patch coverage is 53/53 lines, with 138 tests and lint passing. @greptileai please re-review

@danielphang

Copy link
Copy Markdown
Author

@greptileai Please review b512de16dd: stream recovery tests now use the real iterator, and citation offset handling no longer uses recursion

@danielphang

Copy link
Copy Markdown
Author

@greptileai Please review 9889aec4e1: removed redundant docstrings and typed the stream fixtures and iterator arguments. Focused tests, lint, and live proof pass

@danielphang

Copy link
Copy Markdown
Author

@greptileai Please review e9c0037be3: all newly added test parameters and return types are annotated. All 140 tests, lint, and refreshed live proof pass

@danielphang

Copy link
Copy Markdown
Author

Updated the stream tests, simplified citation offset handling, and addressed typing feedback. All review threads are resolved; Greptile is 5/5 and CI passes

…am iterator

The code-quality recursive_detector gate rejected _offset_annotation because it
called itself to reach the nested url_citation form. The nesting is only one
level deep, so a flat _offset_citation_indices helper replaces the recursion

The bridge recovery tests used handwritten stand-ins for LiteLLM's own Responses
streaming iterators, which tests/unit/AGENTS.md forbids. They also skipped the
part that matters most: whether the typed events the real iterator yields pass
_is_recoverable_stream_event and dump into payloads that recovery can read. The
tests now run SyncResponsesAPIStreamingIterator and ResponsesAPIStreamingIterator
over an httpx SSE body for both output_item.done and output_text.done events,
and all four cases fail when recovery is disabled
@danielphang
danielphang force-pushed the bugfix/chatgpt-stream-output-bridge branch from e9c0037 to cfc7d48 Compare September 28, 2026 15:08
@danielphang

Copy link
Copy Markdown
Author

Closing as superseded by #41235, which fixes the forced SSE aggregation at the root and covers this exact case

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