Skip to content

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

fix(formatter): skip empty thinking blocks in the moonshot formatter (#2954)

ANIRUDDHA ADAKc10ad6f033a0
diff --git a/src/agentscope/formatter/_moonshot_formatter.py b/src/agentscope/formatter/_moonshot_formatter.py
index 1ce387916..9ed223183 100644
--- a/src/agentscope/formatter/_moonshot_formatter.py
+++ b/src/agentscope/formatter/_moonshot_formatter.py
@@ -139,8 +139,13 @@ class MoonshotChatFormatter(_OpenAIFormatterBase):
if isinstance(block, ThinkingBlock):
# Preserve reasoning_content for multi-turn
- # Preserved Thinking (kimi-k2.6 / kimi-k2-thinking)
- reasoning_parts.append(block.thinking)
+ # Preserved Thinking (kimi-k2.6 / kimi-k2-thinking).
+ # Skip empty thinking: a redacted ThinkingBlock is
+ # stored with ``thinking=""``, and emitting it
+ # alone yields an assistant turn whose content is
+ # null and which the API rejects.
+ if block.thinking:
+ reasoning_parts.append(block.thinking)
elif isinstance(block, TextBlock):
content_blocks.append({"type": "text", "text": block.text})
diff --git a/tests/formatter_moonshot_test.py b/tests/formatter_moonshot_test.py
index 1ee827524..55441c793 100644
--- a/tests/formatter_moonshot_test.py
+++ b/tests/formatter_moonshot_test.py
@@ -359,6 +359,13 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
res,
)
+ async def test_empty_thinking_only_yields_no_message(self) -> None:
+ """An empty ThinkingBlock alone must not be sent."""
+ res = await MoonshotChatFormatter().format(
+ [AssistantMsg("a", [ThinkingBlock(thinking="")])],
+ )
+ self.assertListEqual(res, [])
+
async def test_chat_formatter_assistant_always_has_reasoning_content(
self,
) -> None: