[BUG] Strands TS SDK: streamed Bedrock citations are assembled incorrectly
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 v26.3.1
Operating System
macOS 27.2 Beta 2
Installation Method
npm
Steps to Reproduce
-
Use
BedrockModelin its default streaming mode with a Claude model that supports citations (observed with Claude Sonnet 4.6 via an application inference profile, ap-northeast-1). -
Send a citation-enabled document and ask something the model will answer from it:
import { Agent, DocumentBlock, TextBlock } from '@strands-agents/sdk' import { BedrockModel } from '@strands-agents/sdk/models/bedrock' const model = new BedrockModel({ modelId: '<claude model / inference profile>', region: '<region>' }) // stream: default (true) const agent = new Agent({ model, printer: false }) const doc = new DocumentBlock({ name: 'doc1', format: 'txt', source: { content: [ { text: 'Project Atlas is an internal initiative to automate invoice processing.' }, { text: 'In phase 1 of Atlas, paper invoices are scanned with OCR and registered in the accounting system automatically.', }, ], }, citations: { enabled: true }, }) const result = await agent.invoke([new TextBlock('What is Project Atlas?'), doc]) for (const block of result.lastMessage.content) console.log(JSON.stringify(block)) -
Inspect
result.lastMessage.content(orresult.toString()).
The same happens when the document is returned from a tool (DocumentBlock in a tool result).
Expected Behavior
Each content block holds only its own text, and each CitationsBlock holds only the text it cites. This is what the
same request produces with new BedrockModel({ …, stream: false }):
blocks: textBlock, citationsBlock, citationsBlock
[text] "Based on the documents, "
[citations] content="Project Atlas is an internal initiative to automate invoice processing." citations=[documentChunk {start:0,end:1}]
[citations] content="In phase 1 of Atlas, paper invoices are scanned with OCR (Optical Character Recognition) and registered in the accounting system automatically." citations=[documentChunk {start:1,end:2}]
Actual Behavior
With streaming (the default), every block after the first repeats all text before it, including the citation
blocks' content:
blocks: textBlock, citationsBlock, textBlock, citationsBlock
[text] "Based on the documents provided, "
[citations] content="Based on the documents provided, Project Atlas is an internal initiative to automate invoice processing." citations=[documentChunk {start:0,end:1}]
[text] "Based on the documents provided, Project Atlas is an internal initiative to automate invoice processing. "
[citations] content="Based on the documents provided, Project Atlas is an internal initiative to automate invoice processing. In phase 1 of Atlas, paper invoices are scanned with OCR (Optical Character Recognition) and registered in the accounting system automatically." citations=[documentChunk {start:1,end:2}]
result.toString() therefore repeats the answer several times. The citations themselves (location,
sourceContent, title) are correct; only the text placement is wrong.
On 1.16.0 the symptom differs: every CitationsBlock has an empty content and the cited text leaks into the
following TextBlock (which is also cumulative).
Additional Context
Bedrock's stream is correct. The same document and question sent directly with ConverseStream (no Strands; document name shortened to doc1 here):
{"contentBlockDelta":{"delta":{"text":"Based"},"contentBlockIndex":0}}
{"contentBlockDelta":{"delta":{"text":" on the documents"},"contentBlockIndex":0}}
…
{"contentBlockStop":{"contentBlockIndex":0}}
{"contentBlockDelta":{"delta":{"citation":{"title":"doc1","sourceContent":[{"text":"Project Atlas is an internal initiative to automate invoice processing."}],"location":{"documentChunk":{"documentIndex":0,"start":0,"end":1}}}},"contentBlockIndex":1}}
{"contentBlockDelta":{"delta":{"text":"**"},"contentBlockIndex":1}}
{"contentBlockDelta":{"delta":{"text":"Project Atlas** is an internal initiative to"},"contentBlockIndex":1}}
{"contentBlockDelta":{"delta":{"text":" automate invoice processing."},"contentBlockIndex":1}}
{"contentBlockStop":{"contentBlockIndex":1}}
{"contentBlockDelta":{"delta":{"text":"\n- "},"contentBlockIndex":2}}
{"contentBlockStop":{"contentBlockIndex":2}}
{"contentBlockDelta":{"delta":{"citation":{…,"location":{"documentChunk":{"documentIndex":0,"start":1,"end":2}}}},"contentBlockIndex":3}}
{"contentBlockDelta":{"delta":{"text":"In"},"contentBlockIndex":3}}
…
{"contentBlockStop":{"contentBlockIndex":3}}
Two properties of the Bedrock stream matter:
- No
contentBlockStartfor text blocks. Text blocks (and cited-text blocks) start directly withcontentBlockDelta; only tool-use blocks get acontentBlockStart. - A cited block = one
citationdelta followed by ordinarytextdeltas in the samecontentBlockIndex. The cited text arrives asdelta.text, not inside the citation delta.
Cause. The text buffer is only reset on a block start event (src/models/model.ts, streamAggregated):
case 'modelContentBlockStartEvent':
…
accumulatedToolInput = ''
accumulatedText = '' // ← only reset here
accumulatedReasoning = {}
accumulatedCitations.reset()
break
src/models/bedrock.ts emits modelContentBlockStartEvent only when Bedrock sends contentBlockStart, which it does
not for text blocks. So accumulatedText is never reset between blocks:
- each
TextBlockbuilt onmodelContentBlockStopEvent(new TextBlock(accumulatedText)) holds all text so far; - a stream
citationdelta is mapped tocitationsDeltawithcontent: [], and the cited text arrives as ordinarytextdeltas intoaccumulatedText. On stop, the citations block falls back toaccumulatedTextfor its content (the fallback added in #3568), which is also all text so far.
(Without citations this is invisible: a plain answer is a single text block.)
On 1.16.0 (before the #3568 fallback) the citations block took only accumulatedCitations.content (empty), so its
content was empty and the cited text leaked into the next text block.
The non-streaming path is correct because bedrock.ts converts a Converse response into synthetic events that
include a modelContentBlockStartEvent for every block and a citationsDelta whose content is the cited text
(citationsContent handler).
Impact: any consumer of result.lastMessage.content / result.toString() gets wrong content whenever the model
cites documents while streaming. Consumers that only read textDelta stream events still see the right text (the
deltas themselves are correct). Workaround: new BedrockModel({ …, stream: false }).
Possible Solution
In src/models/model.ts (streamAggregated), provider-agnostic: reset per-block state when a block finishes,
not only when one starts (some providers, like Bedrock, send no start event for text blocks):
case 'modelContentBlockStopEvent': {
…build and push the block…
accumulatedText = ''
accumulatedToolInput = ''
accumulatedReasoning = {}
accumulatedCitations.reset()
break
}
With that, the existing fallback (content: accumulatedCitations.content.length > 0 ? … : [{ text: accumulatedText }])
gives each citations block exactly the text streamed within it.
(Alternative: emit a modelContentBlockStartEvent from src/models/bedrock.ts on the first delta of each new
contentBlockIndex, as proposed for Python in #1464. The aggregator fix covers any provider that omits start events.)
Test to add: a stream of [text deltas, stop, citation delta + text deltas, stop, text deltas, stop] with no
start events should aggregate to TextBlock, CitationsBlock (with only its own cited text as content),
TextBlock, each holding only its own text.
Related Issues
#3568 (added the citations-content fallback in model.ts), #1464 (Python: emit contentBlockStart for Bedrock text / citations; not merged), #1460 (no contentBlockStart for the first block, Python), #3534 (TS aggregator: message start event omitted by a provider), #4004 / #1394 (content lost at content-block stop, Python), #2452 / #3007 (native citations for RAG tool results; affected when streaming)
related : https://github.com/strands-agents/harness-sdk/issues/4975
Same thing happens without citations. Any two plain text blocks in a row get merged the same way, we see it with OpenAI models on Bedrock. Stubbing the client with Hello. / stop / How are you? / stop gives [{"text":"Hello."},{"text":"Hello.How are you?"}] on 1.21.0, so the first sentence ends up in history twice. #4590 fixes it for us, would be great to get it reviewed.