Skip to content

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

fix(formatter): repair truncated tool-call input instead of crashing (#2012)

nolanchicad97aec313ef
diff --git a/src/agentscope/formatter/_anthropic_formatter.py b/src/agentscope/formatter/_anthropic_formatter.py
index 83ea1dbdc..f3741cbdd 100644
--- a/src/agentscope/formatter/_anthropic_formatter.py
+++ b/src/agentscope/formatter/_anthropic_formatter.py
@@ -2,7 +2,6 @@
"""The Anthropic formatter module."""
import base64
import fnmatch
-import json
from abc import ABC
from typing import Any
@@ -11,6 +10,7 @@ from pydantic import Field
from ._formatter_base import FormatterBase
from .._logging import logger
+from .._utils._common import _json_loads_with_repair
from ..message import (
Msg,
TextBlock,
@@ -145,8 +145,13 @@ class _AnthropicFormatterBase(FormatterBase, ABC):
"id": block.id,
"name": block.name,
# Anthropic API expects input as a dict, not a
- # JSON string.
- "input": json.loads(block.input or "{}"),
+ # JSON string. Use the repair helper so a
+ # truncated input (from interrupted streaming or
+ # context compression) degrades to {} instead of
+ # raising JSONDecodeError.
+ "input": _json_loads_with_repair(
+ block.input or "{}",
+ ),
},
)
diff --git a/src/agentscope/formatter/_gemini_formatter.py b/src/agentscope/formatter/_gemini_formatter.py
index e495d38ee..bf77638c8 100644
--- a/src/agentscope/formatter/_gemini_formatter.py
+++ b/src/agentscope/formatter/_gemini_formatter.py
@@ -2,7 +2,6 @@
"""Google Gemini API formatter in agentscope."""
import base64
import fnmatch
-import json
from abc import ABC
from typing import Any
@@ -11,6 +10,7 @@ from pydantic import Field
from ._formatter_base import FormatterBase
from .._logging import logger
+from .._utils._common import _json_loads_with_repair
from ..message import (
Msg,
TextBlock,
@@ -206,7 +206,13 @@ class GeminiChatFormatter(_GeminiFormatterBase):
"function_call": {
"id": block.id,
"name": block.name,
- "args": json.loads(block.input or "{}"),
+ # Use the repair helper so a truncated input
+ # (from interrupted streaming or context
+ # compression) degrades to {} instead of
+ # raising JSONDecodeError.
+ "args": _json_loads_with_repair(
+ block.input or "{}",
+ ),
},
},
)
diff --git a/src/agentscope/formatter/_ollama_formatter.py b/src/agentscope/formatter/_ollama_formatter.py
index c239e869e..785d4e7c5 100644
--- a/src/agentscope/formatter/_ollama_formatter.py
+++ b/src/agentscope/formatter/_ollama_formatter.py
@@ -2,7 +2,6 @@
"""The Ollama formatter module."""
import base64
import fnmatch
-import json
from abc import ABC
from typing import Any
@@ -11,6 +10,7 @@ from pydantic import Field
from ._formatter_base import FormatterBase
from .._logging import logger
+from .._utils._common import _json_loads_with_repair
from ..message import (
Msg,
TextBlock,
@@ -197,8 +197,12 @@ class OllamaChatFormatter(_OllamaFormatterBase):
"function": {
"name": block.name,
# Ollama SDK expects a dict, not a
- # JSON string.
- "arguments": json.loads(
+ # JSON string. Use the repair helper
+ # so a truncated input (from
+ # interrupted streaming or context
+ # compression) degrades to {} instead
+ # of raising JSONDecodeError.
+ "arguments": _json_loads_with_repair(
block.input or "{}",
),
},
diff --git a/tests/formatter_anthropic_test.py b/tests/formatter_anthropic_test.py
index 088161537..d02a08bb1 100644
--- a/tests/formatter_anthropic_test.py
+++ b/tests/formatter_anthropic_test.py
@@ -956,3 +956,35 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
],
res,
)
+
+ async def test_tool_call_with_incomplete_input_does_not_crash(
+ self,
+ ) -> None:
+ """A truncated ``block.input`` must not crash the formatter.
+
+ Context compression or interrupted streaming can leave a
+ ``ToolCallBlock.input`` as an incomplete JSON fragment (e.g.
+ ``'{"key'``). The formatter must repair it into a dict (here
+ ``{}``) instead of raising ``JSONDecodeError``.
+ """
+ fmt = AnthropicChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ToolCallBlock(
+ id="call_1",
+ name="get_weather",
+ input='{"city": "Tok',
+ ),
+ ],
+ ),
+ ]
+
+ res = await fmt.format(msgs)
+
+ tool_use = [
+ b for b in res[0]["content"] if b.get("type") == "tool_use"
+ ][0]
+ # Repaired to a dict rather than crashing.
+ self.assertIsInstance(tool_use["input"], dict)
diff --git a/tests/formatter_gemini_test.py b/tests/formatter_gemini_test.py
index b43fabbd6..21cd23da8 100644
--- a/tests/formatter_gemini_test.py
+++ b/tests/formatter_gemini_test.py
@@ -746,3 +746,37 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):
],
res,
)
+
+ async def test_tool_call_with_incomplete_input_does_not_crash(
+ self,
+ ) -> None:
+ """A truncated ``block.input`` must not crash the formatter.
+
+ Context compression or interrupted streaming can leave a
+ ``ToolCallBlock.input`` as an incomplete JSON fragment. The
+ formatter must repair it into a dict instead of raising
+ ``JSONDecodeError``.
+ """
+ fmt = GeminiChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ToolCallBlock(
+ id="call_1",
+ name="get_weather",
+ input='{"city": "Tok',
+ ),
+ ],
+ ),
+ ]
+
+ res = await fmt.format(msgs)
+
+ function_call = [
+ part
+ for m in res
+ for part in m.get("parts", [])
+ if "function_call" in part
+ ][0]["function_call"]
+ self.assertIsInstance(function_call["args"], dict)
diff --git a/tests/formatter_ollama_test.py b/tests/formatter_ollama_test.py
index 9cfd6bd12..bba8eeb74 100644
--- a/tests/formatter_ollama_test.py
+++ b/tests/formatter_ollama_test.py
@@ -574,3 +574,32 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
],
res,
)
+
+ async def test_tool_call_with_incomplete_input_does_not_crash(
+ self,
+ ) -> None:
+ """A truncated ``block.input`` must not crash the formatter.
+
+ Context compression or interrupted streaming can leave a
+ ``ToolCallBlock.input`` as an incomplete JSON fragment. Ollama
+ expects ``arguments`` as a dict, so the formatter must repair the
+ fragment instead of raising ``JSONDecodeError``.
+ """
+ fmt = OllamaChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ToolCallBlock(
+ id="call_1",
+ name="get_weather",
+ input='{"city": "Tok',
+ ),
+ ],
+ ),
+ ]
+
+ res = await fmt.format(msgs)
+
+ args = res[0]["tool_calls"][0]["function"]["arguments"]
+ self.assertIsInstance(args, dict)