Skip to content

Build and run agents you can see, understand and trust.

fix(agent): emit ThinkingBlockEnd before TextBlockStart at reasoning→answer boundary (#1887)

qbc989a9ed61ff1
diff --git a/src/agentscope/agent/_agent.py b/src/agentscope/agent/_agent.py
index 8c7acc90e..b16d154a9 100644
--- a/src/agentscope/agent/_agent.py
+++ b/src/agentscope/agent/_agent.py
@@ -2473,39 +2473,11 @@ class Agent:
elif isinstance(block, DataBlock):
data_blocks.append(block)
- # Handle the text blocks. We only auto-close the open text block
- # when the current chunk has neither text NOR data — a chunk that
- # carries only data (e.g. an omni-style audio PCM delta arriving
- # between two text deltas) must keep the text stream alive so the
- # frontend doesn't fragment one logical text stream into many
- # separate bubbles. A chunk with tool calls (and no text/data)
- # still closes text, which preserves text → tool → text render
- # order via distinct text blocks.
- if text_blocks:
- # If the current chunk has text blocks but no text block id,
- # start with a start event
- if not block_ids.get("text"):
- block_ids["text"] = _generate_id()
- yield TextBlockStartEvent(
- reply_id=self.state.reply_id,
- block_id=block_ids["text"],
- )
- # Go on using the existing text block id to generate delta events
- yield TextBlockDeltaEvent(
- reply_id=self.state.reply_id,
- block_id=block_ids["text"],
- delta="".join([_.text for _ in text_blocks]),
- )
-
- elif block_ids.get("text") and not data_blocks:
- yield TextBlockEndEvent(
- reply_id=self.state.reply_id,
- block_id=block_ids["text"],
- )
- block_ids["text"] = None
-
- # Same reasoning as the text block above — keep the thinking
- # stream open across data-only chunks.
+ # Handle the thinking stream: continue/open or close.
+ # We only auto-close when the chunk also carries no data blocks;
+ # a data-only chunk (e.g. an omni-style audio PCM delta) must keep
+ # both text and thinking streams alive so the frontend doesn't
+ # fragment one logical stream into many separate bubbles.
if thinking_blocks:
# Generate a new thinking block id and start event
if not block_ids.get("thinking"):
@@ -2528,6 +2500,28 @@ class Agent:
)
block_ids["thinking"] = None
+ # Handle the text stream: continue/open or close. Placed after
+ # thinking so that a chunk carrying both ThinkingBlock and TextBlock
+ # emits thinking events first.
+ if text_blocks:
+ if not block_ids.get("text"):
+ block_ids["text"] = _generate_id()
+ yield TextBlockStartEvent(
+ reply_id=self.state.reply_id,
+ block_id=block_ids["text"],
+ )
+ yield TextBlockDeltaEvent(
+ reply_id=self.state.reply_id,
+ block_id=block_ids["text"],
+ delta="".join([_.text for _ in text_blocks]),
+ )
+ elif block_ids.get("text") and not data_blocks:
+ yield TextBlockEndEvent(
+ reply_id=self.state.reply_id,
+ block_id=block_ids["text"],
+ )
+ block_ids["text"] = None
+
# Handle the tool calls that exist in the current chunk
for tool_call in tool_call_blocks:
# Not in previous chunk, start with a start event