Skip to content

[BUG] strands-ts OpenAIModel (chat API): streamAggregated drops all but the last tool call of parallel tool_calls and drops assistant text preceding tool calls #4975

Description

@BlueX888

Checks

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

SDK Language

TypeScript

Strands Version

@strands-agents/sdk 0.0.1-development — checkout of main at 18becd79d70d131f5417631916ab154b099df0b9

Language Runtime Version

Node.js v22.23.2

Operating System

macOS (Darwin 25.6.0)

Installation Method

git clone

Steps to Reproduce

OpenAIModel with api: 'chat' mishandles two standard OpenAI Chat Completions streaming shapes: (1) parallel tool_calls (index 0 then index 1, ids+names in the first delta of each, arguments in later deltas) and (2) assistant content streamed before tool_calls. In both cases streamAggregated() silently drops all but the last-started block.

Minimal reproduction (no network; the OpenAI client is mocked the same way the existing chat.test.ts does). Run with vitest inside strands-ts/:

import { describe, it, expect, vi } from 'vitest'
import OpenAI from 'openai'
import { OpenAIModel } from '../index.js'
import { Message, TextBlock, ToolUseBlock } from '../../../types/messages.js'

function createMockClient(streamGenerator: () => AsyncGenerator<any>): OpenAI {
  return {
    chat: { completions: { create: vi.fn(async () => streamGenerator()) } },
  } as any
}

vi.mock('openai', async (importOriginal) => {
  const actual = await importOriginal<typeof import('openai')>()
  return { ...actual, default: vi.fn(function (this: any) { return {} }) }
})

describe('parallel tool calls + text before tool calls (chat adapter)', () => {
  it('aggregates both tool calls of a parallel tool-call stream', async () => {
    const mockClient = createMockClient(async function* () {
      yield { choices: [{ delta: { role: 'assistant' }, index: 0 }] }
      yield {
        choices: [
          {
            delta: { tool_calls: [{ index: 0, id: 'call_1', type: 'function', function: { name: 'get_weather', arguments: '' } }] },
            index: 0,
          },
        ],
      }
      yield {
        choices: [{ delta: { tool_calls: [{ index: 0, function: { arguments: '{"city":"Seattle"}' } }] }, index: 0 }],
      }
      yield {
        choices: [
          {
            delta: { tool_calls: [{ index: 1, id: 'call_2', type: 'function', function: { name: 'get_time', arguments: '' } }] },
            index: 0,
          },
        ],
      }
      yield {
        choices: [{ delta: { tool_calls: [{ index: 1, function: { arguments: '{"tz":"PST"}' } }] }, index: 0 }],
      }
      yield { choices: [{ finish_reason: 'tool_calls', delta: {}, index: 0 }] }
      yield { choices: [], usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 } }
    })

    const provider = new OpenAIModel({ api: 'chat', client: mockClient, apiKey: 'sk-test' })
    const messages = [new Message({ role: 'user', content: [new TextBlock('weather and time in Seattle?')] })]

    const gen = provider.streamAggregated(messages)
    let last = await gen.next()
    while (!last.done) last = await gen.next()
    const result = last.value

    console.log(
      'PARALLEL CASE aggregated blocks:',
      JSON.stringify(
        result.message.content.map((b: any) => ({ type: b.type, name: b.name, toolUseId: b.toolUseId, input: b.input }))
      )
    )
    const toolUses = result.message.content.filter((b): b is ToolUseBlock => b.type === 'toolUseBlock')
    expect(toolUses.map((t) => t.name).sort()).toEqual(['get_time', 'get_weather'])
  })

  it('keeps assistant text that precedes a tool call', async () => {
    const mockClient = createMockClient(async function* () {
      yield { choices: [{ delta: { role: 'assistant' }, index: 0 }] }
      yield { choices: [{ delta: { content: 'Let me check the weather for you.' }, index: 0 }] }
      yield {
        choices: [
          {
            delta: { tool_calls: [{ index: 0, id: 'call_1', type: 'function', function: { name: 'get_weather', arguments: '' } }] },
            index: 0,
          },
        ],
      }
      yield {
        choices: [{ delta: { tool_calls: [{ index: 0, function: { arguments: '{"city":"Seattle"}' } }] }, index: 0 }],
      }
      yield { choices: [{ finish_reason: 'tool_calls', delta: {}, index: 0 }] }
      yield { choices: [], usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 } }
    })

    const provider = new OpenAIModel({ api: 'chat', client: mockClient, apiKey: 'sk-test' })
    const messages = [new Message({ role: 'user', content: [new TextBlock('weather in Seattle?')] })]

    const gen = provider.streamAggregated(messages)
    let last = await gen.next()
    while (!last.done) last = await gen.next()
    const result = last.value

    console.log(
      'TEXT+TOOL CASE aggregated blocks:',
      JSON.stringify(result.message.content.map((b: any) => ({ type: b.type, name: b.name, text: b.text, input: b.input })))
    )
    const text = result.message.content
      .filter((b): b is TextBlock => b.type === 'textBlock')
      .map((b) => b.text)
      .join('')
    console.log('TEXT+TOOL CASE text:', JSON.stringify(text))
    expect(text).toContain('Let me check the weather')
  })
})

Expected Behavior

For the parallel case, the aggregated message should contain both toolUseBlocks (get_weather/call_1 with input: {"city":"Seattle"} and get_time/call_2 with input: {"tz":"PST"}). For the text+tool case, the assistant text streamed before the tool call (Let me check the weather for you.) should be preserved alongside the toolUseBlock.

Basis — the aggregator contract and the sibling adapters all require sequential start → deltas → stop per block:

  • streamAggregated resets its single accumulation buffer on every modelContentBlockStartEvent (strands-ts/src/models/model.ts:491-497: accumulatedToolInput = ''; accumulatedText = ''; ...), and a block is only finalized when a modelContentBlockStopEvent arrives (model.ts:525-556). So a block's content survives only if it is stopped before the next block starts.
  • The Python sibling does exactly that: strands-py/src/strands/models/openai.py:744-800 buffers tool_calls per index (tool_calls.setdefault(tool_call.index, []).append(tool_call)) and, at finish, closes the text block and emits a paired content_start/content_delta/content_stop for each call.
  • The TS Responses adapter does the same: strands-ts/src/models/openai/responses-adapter.ts:567-598 (finalizeResponsesStream) buffers tool calls and flushes them as paired start/delta/stop events.
  • The Google adapter closes any open text/reasoning block before a functionCall part (strands-ts/src/models/google/adapters.ts:414-422), giving each function call its own start→delta→stop sequence.

Actual Behavior

Verbatim output of the reproduction above, on main @ 18becd79d70d131f5417631916ab154b099df0b9 (observed identically by two independent verification runs):

PARALLEL CASE aggregated blocks: [{"type":"toolUseBlock","name":"get_time","toolUseId":"call_2","input":{"tz":"PST"}}]
AssertionError: expected [ 'get_time' ] to deeply equal [ 'get_time', 'get_weather' ]
TEXT+TOOL CASE aggregated blocks: [{"type":"toolUseBlock","name":"get_weather","input":{"city":"Seattle"}}]
TEXT+TOOL CASE text: ""
AssertionError: expected '' to contain 'Let me check the weather'

Only the last-started tool call (get_time/call_2) survives; get_weather/call_1 is silently discarded and never executed. In the text+tool case the aggregated text is "". Since the agent loop drives every model call through model.streamAggregated() (strands-ts/src/agent/agent.ts:2377), any agent on the chat API whose model issues parallel tool calls loses all but the last call — and any assistant text preceding tool calls is dropped.

Possible Solution

Root cause is in mapChatChunkToEvents (strands-ts/src/models/openai/chat-adapter.ts:423-429): a modelContentBlockStartEvent is emitted for each tool call without emitting a modelContentBlockStopEvent for the block that is currently open (the text block is never closed when tool calls begin, and consecutive tool-call starts are not separated by stops). Stops are only emitted at finish_reason, in the order text-stop-then-tool-stops (chat-adapter.ts:440-449). Because the aggregator resets its buffer on every start (model.ts:491-497) and only flushes on stop (model.ts:525-556), each new start erases the previous block's accumulated text/arguments.

Fix: when a tool-call start is about to be emitted, first close any open text block (emit modelContentBlockStopEvent and clear state.textContentBlockStarted); for multiple tool calls, either emit a stop for the previous tool-call index before starting the next, or — matching the Python sibling (openai.py:744-800) and the TS Responses adapter (responses-adapter.ts:567-598) — buffer per-index tool-call deltas and flush them at finish_reason as paired start/delta/stop sequences after closing the text block, adjusting the finish handler to stop only still-open blocks.

The existing test handles multiple tool calls (strands-ts/src/models/openai/__tests__/chat.test.ts:909-958) only counts stop events from provider.stream() and never runs streamAggregated, which is why the suite currently passes.

I'm happy to open a PR with the buffered per-index approach plus streamAggregated regression tests if that works for the maintainers.

Additional Context

AI usage disclosure

This issue was drafted with AI assistance; the reproduction output quoted under "Actual Behavior" is the verbatim console output of two independent automated verification runs of the script above, and I verified the quoted source lines against main @ 18becd79d70d131f5417631916ab154b099df0b9. Per the template, "Human Overview" is intentionally left blank for a human to fill in.

Related Issues

None found — a search of this repository's issues and PRs for this symptom (mapChatChunkToEvents / parallel tool calls dropped) returned no duplicates, and there are no open PRs touching strands-ts/src/models/openai/chat-adapter.ts.

Activity

  1. added
    typescriptPull requests that update typescript code
    area-modelRelated to models or model providers
    bugSomething isn't working
    on Oct 7, 2026
  2. BlueX888 commented on Oct 7, 2026

    @BlueX888
    ContributorAuthor

    Reproduced this. Root cause: strands-ts/src/models/openai/chat-adapter.ts:423-429 (main @18becd7): mapChatChunkToEvents emits a content-block start for every tool_calls delta without stopping the block already open, while streamAggregated resets its single accumulation buffer on each start and only flushes on stop (src/models/model.ts:491-497), so only the last-started block survives.

    Minimal reproduction:

    In strands-ts/, run with vitest the issue's test: OpenAIModel({api:'chat', client: mockClient}) where the mocked chat stream yields role-delta -> tool_calls idx0 (id call_1, name get_weather) -> idx0 args {"city":"Seattle"} -> tool_calls idx1 (id call_2, name get_time) -> idx1 args {"tz":"PST"} -> finish_reason 'tool_calls' (plus the text-before-tool_calls variant), then drain provider.streamAggregated(messages)
    

    Observed output:

    PARALLEL CASE aggregated blocks: [{"type":"toolUseBlock","name":"get_time","toolUseId":"call_2","input":{"tz":"PST"}}]
    AssertionError: expected [ 'get_time' ] to deeply equal [ 'get_time', 'get_weather' ]
    TEXT+TOOL CASE aggregated blocks: [{"type":"toolUseBlock","name":"get_weather","input":{"city":"Seattle"}}]
    TEXT+TOOL CASE text: ""
    AssertionError: expected '' to contain 'Let me check the weather'
    

    Happy to open a PR with this fix, or to be assigned if you prefer to take it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-bidirectional-streamingRelated to bidirectional streamingarea-modelRelated to models or model providersbugSomething isn't workingtypescriptPull requests that update typescript code

    Type

    Fields

    Language

    TypeScript

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions