Skip to content

[FEATURE] Stream helper utility for extracting text deltas without 3 levels of nesting #2479

@ErikCH

Description

@ErikCH

Problem Statement

Getting a text delta from agent.stream() requires checking three levels of nesting:

for await (const event of agent.stream(message)) {
  if (
    event.type === 'modelStreamUpdateEvent' &&
    event.event.type === 'modelContentBlockDeltaEvent' &&
    event.event.delta.type === 'textDelta'
  ) {
    res.write(`data: ${JSON.stringify({ type: 'text', content: event.event.delta.text })}\n\n`);
  }
}

Every consumer building a chat UI or SSE endpoint writes this same boilerplate. The Python SDK has a much simpler pattern — "data" in event gives you the text chunk directly.

The official TypeScript streaming docs example (/docs/user-guide/concepts/streaming/async-iterators/) shows JSON.stringify(event) dumping the entire event to the client, which doesn't demonstrate how to selectively extract text for a real chat UI.

Proposed Solution

One or more of:

  1. A flattened event typeevent.type === 'textDelta' with event.text directly accessible at the top level
  2. A utility helper — something like streamTextDeltas(agent.stream(msg)) that yields just the text strings
  3. A filter pattern — e.g. agent.stream(msg, { filter: 'text' }) or a .forText() method on the stream

For comparison, the Vercel AI SDK's streamText yields flat events. The Python Strands SDK gives you event["data"] directly.

Use Case

Building an Express.js SSE endpoint (or any HTTP streaming response) for a chat UI where you need to:

  • Stream text deltas token-by-token to the frontend
  • Show tool call start/end indicators
  • Capture metrics from the final agentResultEvent

Currently requires ~15 lines of nested conditionals per event type. A helper could reduce this to 2-3 lines.

Additional Context

Metadata

Metadata

Assignees

No one assigned

    Labels

    typescriptPull requests that update typescript code

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions