Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(formatter): drop empty hint text (#2656)
wangtao3ae8a1f4eca2
diff --git a/src/agentscope/formatter/_anthropic_formatter.py b/src/agentscope/formatter/_anthropic_formatter.pyindex dd4d26fa9..987e8c4af 100644--- a/src/agentscope/formatter/_anthropic_formatter.py+++ b/src/agentscope/formatter/_anthropic_formatter.py@@ -122,40 +122,38 @@ class _AnthropicFormatterBase(FormatterBase, ABC):)elif isinstance(block, HintBlock):- if content_blocks:- role = "user" if has_tool_result else msg.role- messages.append(- {"role": role, "content": content_blocks},- )- content_blocks = []- has_tool_result = False-if isinstance(block.hint, str):- messages.append(- {- "role": "user",- "content": [- {"type": "text", "text": block.hint},- ],- },+ hint_parts = (+ [{"type": "text", "text": block.hint}]+ if block.hint+ else [])else:- hint_parts: list[dict] = []+ hint_parts = []for sub in block.hint:if isinstance(sub, TextBlock):- hint_parts.append(- {"type": "text", "text": sub.text},- )+ if sub.text:+ hint_parts.append(+ {"type": "text", "text": sub.text},+ )elif isinstance(sub, DataBlock):formatted_sub = (self._format_anthropic_data_block(sub))if formatted_sub:hint_parts.append(formatted_sub)- if hint_parts:++ if hint_parts:+ if content_blocks:+ role = "user" if has_tool_result else msg.rolemessages.append(- {"role": "user", "content": hint_parts},+ {"role": role, "content": content_blocks},)+ content_blocks = []+ has_tool_result = False+ messages.append(+ {"role": "user", "content": hint_parts},+ )elif isinstance(block, DataBlock):formatted_block = self._format_anthropic_data_block(block)diff --git a/src/agentscope/formatter/_gemini_formatter.py b/src/agentscope/formatter/_gemini_formatter.pyindex 9b1b0910f..b4c9d895e 100644--- a/src/agentscope/formatter/_gemini_formatter.py+++ b/src/agentscope/formatter/_gemini_formatter.py@@ -169,33 +169,33 @@ class GeminiChatFormatter(_GeminiFormatterBase):)elif isinstance(block, HintBlock):- if parts:- role = "model" if msg.role == "assistant" else "user"- messages.append({"role": role, "parts": parts})- parts = []-if isinstance(block.hint, str):- messages.append(- {- "role": "user",- "parts": [{"text": block.hint}],- },+ hint_parts = (+ [{"text": block.hint}] if block.hint else [])else:- hint_parts: list[dict] = []+ hint_parts = []for sub in block.hint:if isinstance(sub, TextBlock):- hint_parts.append({"text": sub.text})+ if sub.text:+ hint_parts.append({"text": sub.text})elif isinstance(sub, DataBlock):formatted_sub = self._format_gemini_data_block(sub,)if formatted_sub:hint_parts.append(formatted_sub)- if hint_parts:- messages.append(- {"role": "user", "parts": hint_parts},++ if hint_parts:+ if parts:+ role = (+ "model" if msg.role == "assistant" else "user")+ messages.append({"role": role, "parts": parts})+ parts = []+ messages.append(+ {"role": "user", "parts": hint_parts},+ )elif isinstance(block, DataBlock):formatted = self._format_gemini_data_block(block)diff --git a/tests/formatter_anthropic_test.py b/tests/formatter_anthropic_test.pyindex 440c9d98e..a846bbbeb 100644--- a/tests/formatter_anthropic_test.py+++ b/tests/formatter_anthropic_test.py@@ -863,6 +863,53 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):res,)+ async def test_chat_formatter_drops_empty_hint_text(self) -> None:+ """Empty hint text is ignored without splitting adjacent content."""+ fmt = AnthropicChatFormatter()+ res = await fmt.format(+ [+ AssistantMsg(+ name="assistant",+ content=[+ TextBlock(text="before"),+ HintBlock(hint=""),+ TextBlock(text="after"),+ HintBlock(hint=[TextBlock(text="")]),+ HintBlock(+ hint=[+ TextBlock(text=""),+ TextBlock(text="valid hint"),+ ],+ ),+ TextBlock(text="done"),+ ],+ ),+ ],+ )++ self.assertListEqual(+ [+ {+ "role": "assistant",+ "content": [+ {"type": "text", "text": "before"},+ {"type": "text", "text": "after"},+ ],+ },+ {+ "role": "user",+ "content": [+ {"type": "text", "text": "valid hint"},+ ],+ },+ {+ "role": "assistant",+ "content": [{"type": "text", "text": "done"}],+ },+ ],+ res,+ )+async def test_chat_formatter_hint_block_multimodal(self) -> None:"""Multimodal HintBlock becomes a single user message with text+ image."""diff --git a/tests/formatter_gemini_test.py b/tests/formatter_gemini_test.pyindex 443a9233d..71a4ad9b8 100644--- a/tests/formatter_gemini_test.py+++ b/tests/formatter_gemini_test.py@@ -773,6 +773,51 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):res,)+ async def test_chat_formatter_drops_empty_hint_text(self) -> None:+ """Empty hint text is ignored without splitting adjacent content."""+ fmt = GeminiChatFormatter()+ res = await fmt.format(+ [+ AssistantMsg(+ name="assistant",+ content=[+ TextBlock(text="before"),+ HintBlock(hint=""),+ TextBlock(text="after"),+ HintBlock(hint=[TextBlock(text="")]),+ HintBlock(+ hint=[+ TextBlock(text=""),+ TextBlock(text="valid hint"),+ ],+ ),+ TextBlock(text="done"),+ ],+ ),+ ],+ )++ self.assertListEqual(+ [+ {+ "role": "model",+ "parts": [+ {"text": "before"},+ {"text": "after"},+ ],+ },+ {+ "role": "user",+ "parts": [{"text": "valid hint"}],+ },+ {+ "role": "model",+ "parts": [{"text": "done"}],+ },+ ],+ res,+ )+async def test_chat_formatter_hint_block_multimodal(self) -> None:"""Multimodal HintBlock becomes a single user message with text +image."""