sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[BUG] `streamAggregated` drops whitespace-only text blocks that Bedrock (Claude Sonnet 4.6) accepts

Open 0Itsuki0 opened this issue 2 months ago • 1 comments

Checks

  • [x] I have updated to the lastest minor and patch version of Strands
  • [x] I have checked the documentation and this is not expected behavior
  • [x] I have searched ./issues and there are no duplicates of my issue

SDK Language

TypeScript

Strands Version

1.19

Language Runtime Version

Node.js 26.3.1

Operating System

macOS 27.2 beta 2

Installation Method

npm

Steps to Reproduce

streamAggregated in src/models/model.ts removes every TextBlock whose text is empty or whitespace-only from the aggregated message:

const filtered = contentBlocks.filter(
  (block) => !(block instanceof TextBlock && block.text.trim() === '')
)

So a model response streamed as three text blocks, "hey", "\n", "what's up", is stored as [TextBlock('hey'), TextBlock("what's up")], and the "\n" block is lost.

To check whether Bedrock needs the filter, seed an Agent with an assistant message and call invoke. The SDK appends the user message, so the assistant message is sent back mid-history:

import { Agent, Message, TextBlock } from '@strands-agents/sdk'

const agent = new Agent({
  model: bedrockModel,
  messages: [
    new Message({ role: 'assistant', content: [new TextBlock('hey'), new TextBlock('\n'), new TextBlock("what's up")] }),
  ],
})

await agent.invoke('hey')

I repeated this with the assistant content replaced by each case below.

Results (Bedrock Converse, Claude Sonnet 4.6)

Assistant message content Result
[TextBlock('')] ❌ rejected
[TextBlock('hey'), TextBlock(''), TextBlock("what's up")] ❌ rejected
[TextBlock('\n')] ❌ rejected
[TextBlock('hey'), TextBlock('\n'), TextBlock("what's up")] ✅ accepted
[TextBlock('hey'), TextBlock('\n')] ✅ accepted
[TextBlock('\n'), TextBlock('hey')] ✅ accepted

Blocks other than TextBlock behave the same, such as CitationsBlock: a "\n" text block next to them is also accepted.

Expected Behavior

Whitespace-only text blocks that Bedrock accepts are kept in the aggregated message. Only blocks that would get the next request rejected are dropped.

Actual Behavior

Every whitespace-only TextBlock is removed from result.message.content, even where Bedrock accepts it. The block is still yielded as a stream event, so the streamed output and the stored message disagree.

Additional Context

The filter was added in #3029 ("fix(model): drop blank text content blocks before send") to avoid Bedrock rejecting blank text on the next turn. That concern holds for "", and for a message whose only content is whitespace. The results above show that trim() === '' drops more than it needs to.

The Python SDK has equivalent handling in strands/event_loop/streaming.py (remove_blank_messages_content_text), so any change affects cross-SDK parity.

Not tested:

  • Models other than Claude Sonnet 4.6, even on Bedrock. Validation can differ by model.
  • Providers other than Bedrock. The filter is in provider-agnostic model.ts, so the rule has to hold for the other providers too.

Possible Solution

Narrow the filter in streamAggregated:

  • Always drop "" text blocks.
  • Drop whitespace-only text blocks only when the message has no other content.

Related Issues

#3029

0Itsuki0 avatar Sep 25 '26 02:09 0Itsuki0

+1. The same filter also breaks Anthropic extended thinking. When a whitespace-only text block sits between two thinking blocks, it's dropped from the aggregated message, so any resend of that turn (e.g. the structured-output retry) is rejected with 400 "thinking or redacted_thinking blocks in the latest assistant message cannot be modified". Seen on 1.11.2; same code in 1.19.0. #4592's rule would cover this case.

securityvoid avatar Oct 06 '26 14:10 securityvoid