fix(stream_consumer): handle overflow when no message exists yet - #6828
Merged
Conversation
The overflow split loop required _message_id to be set, but on the first streamed message (or after a segment break) _message_id is None. Oversized text fell through to _send_or_edit → adapter.send(), which split internally — but subsequent edits hit Telegram's 'message too long' and were silently truncated with '…', cutting off the response. Add a new code path for the _message_id is None case that uses truncate_message() (same as the non-streaming path) to split with proper word/code-fence boundaries and chunk indicators. Each chunk is sent as a new message via _send_new_chunk(). Properly handles got_done (returns immediately after sending chunks instead of continuing into an infinite loop) and got_segment_break. Original cherry-picked from PR #6816 by dangelo352. Fixes silent message truncation on Telegram for long streamed responses.
This was referenced Jul 30, 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
Salvage of #6816 by @dangelo352 — fixes silent message truncation on Telegram for long streamed responses.
Root cause
The overflow split loop in
stream_consumer.pyrequiredself._message_id is not Noneto enter, but_message_idstarts asNoneon the first streamed message (and after segment breaks). When the first accumulated text exceeded ~3993 chars:_send_or_edit()→adapter.send()truncate_message(), but returned only chunk 1's message_idsuccess=TrueFix
Add a new code path before the existing while loop for the
_message_id is Nonecase:truncate_message()(same helper the non-streaming path uses) for proper word/code-fence boundary splitting with chunk indicators like(1/2)_send_new_chunk()helpergot_done(returns immediately) andgot_segment_breakThe existing while loop for the
_message_id is not Nonecase is preserved untouched.Changes from original PR #6816
(1/\2\)instead of(1/3))continuethat would skipgot_donehandling (causing infinite loop when final message overflows)_safe_limitinstead of_raw_limitfor truncate_message to account for formatting expansionTest