Skip to content

[Bug]: reply_stream merges all text blocks of a non-streaming response into one block and moves tool calls after the text #3236

Open

Description

@BlueX888

Prerequisites

  • I have searched the existing issues and discussions, and this is not a duplicate.
  • This is a bug, not a usage question. (For questions, please use Discussions instead.)

Background / Description

For a non-streaming model response, the message rebuilt from reply_stream events diverges from what the agent itself records: agent.state.context and the Msg returned by reply().

Agent._convert_chat_response_to_event receives a non-streaming ChatResponse as a single chunk carrying the whole response, and it funnels every TextBlock of that chunk through one text slot:

  • all text blocks are joined into one delta with "".join(...) (no separator), and
  • tool calls are emitted after all the text, regardless of their position in response.content.

Meanwhile _save_to_context persists completed_response.content verbatim, and reply() returns those blocks unchanged — so the two documented entry points disagree for the same response.

This is reachable with real models: Gemini's non-streaming parser builds one TextBlock per part (src/agentscope/model/_gemini/_model.py), Anthropic's non-streaming parser preserves a [text, tool_use, text] order (src/agentscope/model/_anthropic/_model.py), and stream=False is a supported ChatModelBase config.

The streaming branch of the same function does not have the problem: with one block per chunk, a tool-call chunk closes the open text stream, so text → tool call → text is rebuilt as three separate blocks that match the context.

Observed with [TextBlock("Hello"), TextBlock("world")]:

  • event-rebuilt: one block TextBlock('Helloworld')
  • context / reply(): two blocks TextBlock('Hello'), TextBlock('world')

Observed with [TextBlock("Let me check."), ToolCallBlock, TextBlock("Done checking.")]:

  • event-rebuilt: TextBlock('Let me check.Done checking.') followed by the tool call
  • context / reply(): TextBlock('Let me check.'), tool call, TextBlock('Done checking.')

For SSE consumers that rebuild the reply message from events (the Msg.append_event path the chat service uses), the stored transcript therefore no longer matches the agent's own context.

Error Messages

No exception — the divergence is silent. Comparing the event-rebuilt message with agent.state.context for [TextBlock("Hello"), TextBlock("world")]:

event-rebuilt:  [TextBlock(type='text', text='Helloworld', ...)]
context:       [TextBlock(type='text', text='Hello', ...), TextBlock(type='text', text='world', ...)]

Steps to Reproduce

  1. Code (uses the repo's own MockModel so no API key is needed; the same shape of response is produced by Gemini/Anthropic with stream=False):
import asyncio, sys
sys.path.insert(0, "tests")

from utils import MockModel
from agentscope.agent import Agent
from agentscope.event import ReplyStartEvent
from agentscope.message import AssistantMsg, Msg, TextBlock, UserMsg
from agentscope.model import ChatResponse
from agentscope.tool import Toolkit

async def main() -> None:
    model = MockModel(context_size=100_000)
    # a plain list of ChatResponse -> non-streaming call
    model.set_responses([
        ChatResponse(
            content=[TextBlock(text="Hello"), TextBlock(text="world")],
            is_last=True,
        ),
    ])

    agent = Agent(
        name="assistant",
        system_prompt="You are a helpful assistant.",
        model=model,
        toolkit=Toolkit(),
    )

    rebuilt = None
    async for evt in agent.reply_stream(UserMsg(name="user", content="hi")):
        if isinstance(evt, ReplyStartEvent):
            rebuilt = AssistantMsg(id=evt.reply_id, name="assistant", content=[])
        elif rebuilt is not None and not isinstance(evt, Msg):
            rebuilt.append_event(evt)

    print("event-rebuilt:", rebuilt.content)
    print("context:      ", agent.state.context[-1].content)

asyncio.run(main())
  1. Run: python repro.py (from the repo root, editable install active)
  2. The event-rebuilt message contains one glued TextBlock('Helloworld'), while agent.state.context[-1] keeps the two text blocks separate.

Environment

  • AgentScope Version: 2.0.10dev (main)
  • Python Version: 3.12.14
  • OS: macOS 26

Activity

  1. added
    triage/verifyingA bot is verifying whether this bug is real
    triage/confirmedVerified: the reported defect exists
    and removed
    triage/verifyingA bot is verifying whether this bug is real
    on Oct 11, 2026
  2. ffffff55 commented on Oct 11, 2026

    @ffffff55
    Contributor

    I would like to work on this — I will follow up with a fix and a regression test shortly.

  3. github-actions commented on Oct 11, 2026

    @github-actions
    Contributor

    It's yours, @ffffff55. If there is no pull request and no word from you by 2026-10-25 the claim is released so nobody is blocked — a comment here renews it.

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

Metadata

Metadata

Assignees

Labels

triage/confirmedVerified: the reported defect exists

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions