Skip to content

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

fix(formatter): preserve first rendered history marker (#2643)

wangtao20228d5c4bc9
diff --git a/src/agentscope/formatter/_anthropic_formatter.py b/src/agentscope/formatter/_anthropic_formatter.py
index 987e8c4af..956eb88e8 100644
--- a/src/agentscope/formatter/_anthropic_formatter.py
+++ b/src/agentscope/formatter/_anthropic_formatter.py
@@ -436,13 +436,13 @@ class AnthropicMultiAgentFormatter(_AnthropicFormatterBase):
await self._format_messages(group),
)
case "agent_message":
- formatted_msgs.extend(
- await self._format_agent_message(
- group,
- is_first_agent_message,
- ),
+ formatted_group = await self._format_agent_message(
+ group,
+ is_first_agent_message,
)
- is_first_agent_message = False
+ formatted_msgs.extend(formatted_group)
+ if formatted_group:
+ is_first_agent_message = False
return formatted_msgs
diff --git a/src/agentscope/formatter/_dashscope_formatter.py b/src/agentscope/formatter/_dashscope_formatter.py
index 9544746ed..cf17e3d42 100644
--- a/src/agentscope/formatter/_dashscope_formatter.py
+++ b/src/agentscope/formatter/_dashscope_formatter.py
@@ -475,13 +475,13 @@ class DashScopeMultiAgentFormatter(_DashScopeFormatterBase):
await self._format_tool_sequence(group),
)
case "agent_message":
- formatted_msgs.extend(
- await self._format_agent_message(
- group,
- is_first_agent_message,
- ),
+ formatted_group = await self._format_agent_message(
+ group,
+ is_first_agent_message,
)
- is_first_agent_message = False
+ formatted_msgs.extend(formatted_group)
+ if formatted_group:
+ is_first_agent_message = False
return formatted_msgs
diff --git a/src/agentscope/formatter/_deepseek_formatter.py b/src/agentscope/formatter/_deepseek_formatter.py
index fba0eb0a0..7f09c1eb9 100644
--- a/src/agentscope/formatter/_deepseek_formatter.py
+++ b/src/agentscope/formatter/_deepseek_formatter.py
@@ -242,13 +242,13 @@ class DeepSeekMultiAgentFormatter(FormatterBase):
await self._format_tool_sequence(group),
)
case "agent_message":
- formatted_msgs.extend(
- await self._format_agent_message(
- group,
- is_first_agent_message,
- ),
+ formatted_group = await self._format_agent_message(
+ group,
+ is_first_agent_message,
)
- is_first_agent_message = False
+ formatted_msgs.extend(formatted_group)
+ if formatted_group:
+ is_first_agent_message = False
return formatted_msgs
diff --git a/src/agentscope/formatter/_gemini_formatter.py b/src/agentscope/formatter/_gemini_formatter.py
index b4c9d895e..1cee40df4 100644
--- a/src/agentscope/formatter/_gemini_formatter.py
+++ b/src/agentscope/formatter/_gemini_formatter.py
@@ -348,13 +348,13 @@ class GeminiMultiAgentFormatter(_GeminiFormatterBase):
await self._format_tool_sequence(group),
)
case "agent_message":
- formatted_msgs.extend(
- await self._format_agent_message(
- group,
- is_first_agent_message,
- ),
+ formatted_group = await self._format_agent_message(
+ group,
+ is_first_agent_message,
)
- is_first_agent_message = False
+ formatted_msgs.extend(formatted_group)
+ if formatted_group:
+ is_first_agent_message = False
return formatted_msgs
diff --git a/src/agentscope/formatter/_moonshot_formatter.py b/src/agentscope/formatter/_moonshot_formatter.py
index f640e81c4..6814ef8ff 100644
--- a/src/agentscope/formatter/_moonshot_formatter.py
+++ b/src/agentscope/formatter/_moonshot_formatter.py
@@ -355,13 +355,13 @@ class MoonshotMultiAgentFormatter(_OpenAIFormatterBase):
).format(group),
)
elif typ == "agent_message":
- formatted_msgs.extend(
- await self._format_agent_message(
- group,
- is_first_agent_message,
- ),
+ formatted_group = await self._format_agent_message(
+ group,
+ is_first_agent_message,
)
- is_first_agent_message = False
+ formatted_msgs.extend(formatted_group)
+ if formatted_group:
+ is_first_agent_message = False
return formatted_msgs
diff --git a/src/agentscope/formatter/_ollama_formatter.py b/src/agentscope/formatter/_ollama_formatter.py
index 50b6015c8..56ed65ad8 100644
--- a/src/agentscope/formatter/_ollama_formatter.py
+++ b/src/agentscope/formatter/_ollama_formatter.py
@@ -382,13 +382,13 @@ class OllamaMultiAgentFormatter(_OllamaFormatterBase):
await self._format_tool_sequence(group),
)
case "agent_message":
- formatted_msgs.extend(
- await self._format_agent_message(
- group,
- is_first_agent_message,
- ),
+ formatted_group = await self._format_agent_message(
+ group,
+ is_first_agent_message,
)
- is_first_agent_message = False
+ formatted_msgs.extend(formatted_group)
+ if formatted_group:
+ is_first_agent_message = False
return formatted_msgs
diff --git a/src/agentscope/formatter/_openai_formatter.py b/src/agentscope/formatter/_openai_formatter.py
index c40311db3..f0f70282a 100644
--- a/src/agentscope/formatter/_openai_formatter.py
+++ b/src/agentscope/formatter/_openai_formatter.py
@@ -472,13 +472,13 @@ class OpenAIMultiAgentFormatter(_OpenAIFormatterBase):
await self._format_tool_sequence(group),
)
case "agent_message":
- formatted_msgs.extend(
- await self._format_agent_message(
- group,
- is_first_agent_message,
- ),
+ formatted_group = await self._format_agent_message(
+ group,
+ is_first_agent_message,
)
- is_first_agent_message = False
+ formatted_msgs.extend(formatted_group)
+ if formatted_group:
+ is_first_agent_message = False
return formatted_msgs
diff --git a/src/agentscope/formatter/_openai_response_formatter.py b/src/agentscope/formatter/_openai_response_formatter.py
index 322751b10..bd55b7da9 100644
--- a/src/agentscope/formatter/_openai_response_formatter.py
+++ b/src/agentscope/formatter/_openai_response_formatter.py
@@ -447,13 +447,13 @@ class OpenAIResponseMultiAgentFormatter(_OpenAIResponseFormatterBase):
await self._format_tool_sequence(group),
)
elif typ == "agent_message":
- formatted_msgs.extend(
- await self._format_agent_message(
- group,
- is_first_agent_message,
- ),
+ formatted_group = await self._format_agent_message(
+ group,
+ is_first_agent_message,
)
- is_first_agent_message = False
+ formatted_msgs.extend(formatted_group)
+ if formatted_group:
+ is_first_agent_message = False
return formatted_msgs
diff --git a/src/agentscope/formatter/_volcengine_formatter.py b/src/agentscope/formatter/_volcengine_formatter.py
index e29d52383..9d0e9ba1f 100644
--- a/src/agentscope/formatter/_volcengine_formatter.py
+++ b/src/agentscope/formatter/_volcengine_formatter.py
@@ -339,13 +339,13 @@ class VolcengineMultiAgentFormatter(_VolcengineFormatterBase):
await self._format_tool_sequence(group),
)
case "agent_message":
- formatted_msgs.extend(
- await self._format_agent_message(
- group,
- is_first_agent_message,
- ),
+ formatted_group = await self._format_agent_message(
+ group,
+ is_first_agent_message,
)
- is_first_agent_message = False
+ formatted_msgs.extend(formatted_group)
+ if formatted_group:
+ is_first_agent_message = False
return formatted_msgs
diff --git a/src/agentscope/formatter/_xai_formatter.py b/src/agentscope/formatter/_xai_formatter.py
index 7f557382e..325a93eee 100644
--- a/src/agentscope/formatter/_xai_formatter.py
+++ b/src/agentscope/formatter/_xai_formatter.py
@@ -448,7 +448,7 @@ class XAIMultiAgentFormatter(FormatterBase):
)
if history_text:
xai_messages.append(user(history_text))
- is_first_agent_message = False
+ is_first_agent_message = False
return xai_messages
diff --git a/tests/formatter_anthropic_test.py b/tests/formatter_anthropic_test.py
index a846bbbeb..cc3655985 100644
--- a/tests/formatter_anthropic_test.py
+++ b/tests/formatter_anthropic_test.py
@@ -652,6 +652,61 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
res = await fmt.format([])
self.assertListEqual([], res)
+ async def test_multiagent_empty_group_keeps_first_history_marker(
+ self,
+ ) -> None:
+ """A skipped group must not consume the first-history marker."""
+ fmt = AnthropicMultiAgentFormatter()
+ res = await fmt.format(
+ [
+ AssistantMsg(
+ name="assistant",
+ content=[ThinkingBlock(thinking="unsigned")],
+ ),
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ToolCallBlock(
+ id="call_1",
+ name="get_capital",
+ input='{"country": "Japan"}',
+ ),
+ ToolResultBlock(
+ id="call_1",
+ name="get_capital",
+ output=[
+ TextBlock(
+ text="The capital of Japan is Tokyo.",
+ ),
+ ],
+ state=ToolResultState.SUCCESS,
+ ),
+ ],
+ ),
+ UserMsg(name="user", content=[TextBlock(text="hello")]),
+ ],
+ )
+
+ self.assertListEqual(
+ [
+ self._gt_tool_call,
+ self._gt_tool_result,
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "text",
+ "text": (
+ fmt.conversation_history_prompt
+ + "<history>\nuser: hello\n</history>"
+ ),
+ },
+ ],
+ },
+ ],
+ res,
+ )
+
async def test_chat_formatter_complex_multi_step(self) -> None:
"""Complex multi-step sequence with interleaved thinking, text,
tool calls, and tool results."""
diff --git a/tests/formatter_gemini_test.py b/tests/formatter_gemini_test.py
index 71a4ad9b8..4fe08c226 100644
--- a/tests/formatter_gemini_test.py
+++ b/tests/formatter_gemini_test.py
@@ -565,6 +565,60 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):
res = await fmt.format([])
self.assertListEqual([], res)
+ async def test_multiagent_empty_group_keeps_first_history_marker(
+ self,
+ ) -> None:
+ """A skipped group must not consume the first-history marker."""
+ fmt = GeminiMultiAgentFormatter()
+ res = await fmt.format(
+ [
+ AssistantMsg(
+ name="assistant",
+ content=[ThinkingBlock(thinking="")],
+ ),
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ToolCallBlock(
+ id="call_1",
+ name="get_capital",
+ input='{"country": "Japan"}',
+ ),
+ ToolResultBlock(
+ id="call_1",
+ name="get_capital",
+ output=[
+ TextBlock(
+ text="The capital of Japan is Tokyo.",
+ ),
+ ],
+ state=ToolResultState.SUCCESS,
+ ),
+ ],
+ ),
+ UserMsg(name="user", content=[TextBlock(text="hello")]),
+ ],
+ )
+
+ self.assertListEqual(
+ [
+ self._gt_tool_call,
+ self._gt_tool_result,
+ {
+ "role": "user",
+ "parts": [
+ {
+ "text": (
+ fmt.conversation_history_prompt
+ + "<history>\nuser: hello\n</history>"
+ ),
+ },
+ ],
+ },
+ ],
+ res,
+ )
+
async def test_chat_formatter_complex_multi_step(self) -> None:
"""Complex multi-step sequence with interleaved thinking, text,
tool calls, and tool results."""