fix: handle empty Bedrock stream response (messageStop without messageStart)#749
Open
pfolegmalkov wants to merge 2 commits intostrands-agents:mainfrom
Open
fix: handle empty Bedrock stream response (messageStop without messageStart)#749pfolegmalkov wants to merge 2 commits intostrands-agents:mainfrom
pfolegmalkov wants to merge 2 commits intostrands-agents:mainfrom
Conversation
…eStart) Bedrock Converse API may return modelMessageStopEvent without a preceding modelMessageStartEvent when the model produces an empty response (e.g., after certain tool results like ask_clarifying_questions). Previously this threw 'Stream ended without completing a message'. Now defaults to assistant role with empty content array, matching Bedrock's behavior.
4 new test cases in model.test.ts: - messageStop without messageStart creates assistant message with empty content - content blocks accumulated before messageStop without messageStart are preserved - toolUse stopReason without messageStart handled correctly - completely empty stream (no events) still throws ModelError Covers the fix in model.ts where modelMessageStopEvent now defaults to 'assistant' role when modelMessageStartEvent was not received.
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.
Problem
When using AWS Bedrock Converse API, the model may return a
modelMessageStopEventwithout a precedingmodelMessageStartEvent. This happens when the model produces an empty response — for example, after processing certain tool results (likeask_clarifying_questions) where the model determines it has nothing to add.The current code throws
ModelError('Stream ended without completing a message')becausestoppedMessageis null (set only whenmessageRoleis truthy, which requiresmodelMessageStartEvent).This is valid Bedrock behavior — the Converse API spec allows
messageStopevents with a stop reason even when no content blocks were produced.Fix
In
src/models/model.ts, themodelMessageStopEventhandler now defaults to'assistant'role whenmodelMessageStartEventwas not received:The
contentBlocksarray will be empty in this case, which is the correct representation of an empty model response.Test plan
Added 4 new test cases in
src/models/__tests__/model.test.ts:ModelError(no events at all = real error)All 1617 tests pass. Coverage on
model.tsbranch coverage improved (87.09% → 90.32%).Reproduction
This issue was discovered in a production conversational search application using Bedrock Converse API with Claude Sonnet 4.6 and 13 tools. The model consistently returns empty responses after
ask_clarifying_questionstool results, causing the agent loop to crash.Checklist