fix(streaming): preserve custom attributes on final stream chunk#23530
Merged
Sameerlite merged 1 commit intoBerriAI:mainfrom Mar 16, 2026
Merged
Conversation
Ensure final finish_reason chunks retain non-OpenAI attributes from original provider chunks, including the holding_chunk flush path where delta is non-empty. Add regression tests for both final-chunk branches. Made-with: Cursor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Greptile SummaryThis PR fixes a bug where custom (non-OpenAI) attributes on final Key changes:
Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| litellm/litellm_core_utils/streaming_handler.py | Adds preserve_upstream_non_openai_attributes call in the received_finish_reason path before _is_delta_empty branching; also renames import from ChatCompletionChunk to OpenAIChatCompletionChunk. Logic is sound and placement is correct for both code paths. |
| tests/test_litellm/litellm_core_utils/test_streaming_handler.py | Adds two regression tests covering the empty-delta and holding-chunk-flush code paths. Tests are fully mocked with no real network calls, consistent with the folder's policy. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[return_processed_chunk_logic called] --> B{is_chunk_non_empty?}
B -- Yes --> C[Non-empty delta path]
C --> D{original_chunk in response_obj?}
D -- Yes --> E[preserve_upstream_non_openai_attributes]
E --> F[Return model_response]
D -- No --> F
B -- No --> G{received_finish_reason is not None?}
G -- No --> H[Other paths / return None]
G -- Yes --> I{sent_last_chunk is True?}
I -- Yes --> J[Raise StopIteration]
I -- No --> K{holding_chunk not empty?}
K -- Yes --> L[Flush holding_chunk into delta.content]
K -- No --> M[Compute _is_delta_empty]
L --> M
M --> N["✨ NEW: preserve_upstream_non_openai_attributes\n(before branching - applies to both paths)"]
N --> O{_is_delta_empty?}
O -- Yes --> P[Set delta=Delta content=None\nSet finish_reason\nSet sent_last_chunk=True]
O -- No --> Q[Non-empty delta returned\nwith holding chunk content]
P --> R[Return model_response]
Q --> R
Last reviewed commit: a012486
Harshit28j
approved these changes
Mar 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
finish_reasonchunks inCustomStreamWrapper.return_processed_chunk_logic_is_delta_emptybranching so both empty-delta and holding-chunk-flush paths keep custom fieldstest_streaming_handler.pyTest plan
poetry run pytest tests/test_litellm/litellm_core_utils/test_streaming_handler.py::test_finish_reason_chunk_preserves_non_openai_attributes -vpoetry run pytest tests/test_litellm/litellm_core_utils/test_streaming_handler.py::test_finish_reason_with_holding_chunk_preserves_non_openai_attributes -vFixes #23444