fix: emit complete signal to callback handler when text block stops#1890
Open
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Open
fix: emit complete signal to callback handler when text block stops#1890giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
Conversation
… stops
When PrintingCallbackHandler checks kwargs.get('complete', False) to
determine whether to print a trailing newline, no event in the streaming
pipeline ever sets complete=True. This causes text output to never have
proper line termination.
Root cause: TextStreamEvent emits {data, delta} but never signals
completion. contentBlockStop triggers handle_content_block_stop() which
clears the text state, but no event is yielded to notify the callback.
Fix: Before calling handle_content_block_stop(), check if text was being
accumulated (state['text'] is truthy). If so, yield a
ModelStreamEvent({'complete': True}) after the stop handler runs. This
event reaches the callback handler via is_callback_event (non-empty dict)
and enables PrintingCallbackHandler to print the trailing newline.
The complete signal is only emitted for text content blocks, not for
toolUse or reasoningContent blocks, matching the callback handler's
expected behavior.
Closes strands-agents#826
5a078e2 to
57bf2b1
Compare
Contributor
Author
|
Friendly ping — emits the |
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.
Issue
Closes #826
Problem
PrintingCallbackHandlercheckskwargs.get('complete', False)to determine whether to print a trailing newline after text output, but no event in the streaming pipeline ever setscomplete=True. This causes text output to never have proper line termination.Root Cause
The streaming pipeline emits
TextStreamEvent({'data': text, 'delta': delta})for each text chunk but never signals completion. WhencontentBlockStoparrives,handle_content_block_stop()clears the accumulated text state—but yields no event to notify the callback handler that text streaming is done.Meanwhile,
EventLoopStopEventandModelStopReasonboth haveis_callback_event = False, so they never reach the callback handler either.Solution
Before calling
handle_content_block_stop(), check if text was being accumulated (state['text']is truthy). If so, yield aModelStreamEvent({'complete': True})after the stop handler runs.This event:
is_callback_event = True(non-empty dict →len(self.keys()) > 0)prepare()invocation_state merge (no'delta'key in the dict)toolUseorreasoningContentblocksThe fix is minimal (3 lines) and does not alter any existing event payloads or control flow.
Testing
test_complete_event_emitted_for_text_block— verifies exactly 1 complete event after text stoptest_no_complete_event_for_tool_use_block— verifies no complete event for tool_usetest_no_complete_event_for_reasoning_block— verifies no complete event for reasoningtest_complete_event_with_multiple_text_blocks— verifies 1 complete event per text blocktest_process_stream,test_stream_messages,test_agent__call__callback,test_stream_e2e_*Changes
src/strands/event_loop/streaming.pyhad_text, yieldModelStreamEvent({'complete': True})tests/strands/event_loop/test_streaming.pytests/strands/agent/test_agent.pytests/strands/agent/hooks/test_agent_events.pyBy issuing a ticket, you agree to abide by the Contributing Guidelines.