Skip to content

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

fix(formatter): keep parallel Ollama tool calls on one assistant message (#3910)

Max Freedom6d95d575df93
diff --git a/src/agentscope/formatter/_ollama_formatter.py b/src/agentscope/formatter/_ollama_formatter.py
index 6827d59fd..6a1c3d1fd 100644
--- a/src/agentscope/formatter/_ollama_formatter.py
+++ b/src/agentscope/formatter/_ollama_formatter.py
@@ -137,6 +137,9 @@ class OllamaChatFormatter(_OllamaFormatterBase):
images = []
# Hold the promoted media until this turn's tool messages are out.
pending_media: list[dict] = []
+ # The message carrying this turn's tool calls, which later
+ # parallel tool calls join.
+ tool_call_msg: dict[str, Any] | None = None
for block in msg.get_content_blocks():
if pending_media and not isinstance(block, ToolResultBlock):
@@ -194,14 +197,26 @@ class OllamaChatFormatter(_OllamaFormatterBase):
pass
elif isinstance(block, ToolCallBlock):
- messages.append(
- self._format_tool_call_message(
- msg.role,
- content_parts,
- images,
- block,
- ),
+ formatted_call = self._format_tool_call_message(
+ msg.role,
+ content_parts,
+ images,
+ block,
)
+ if (
+ tool_call_msg is not None
+ and messages[-1] is tool_call_msg
+ ):
+ # A parallel tool call. Ollama would merge a second
+ # assistant message into this one by appending only
+ # its content, dropping the call, so it joins here.
+ self._merge_tool_call_message(
+ tool_call_msg,
+ formatted_call,
+ )
+ else:
+ tool_call_msg = formatted_call
+ messages.append(tool_call_msg)
content_parts = []
images = []
@@ -338,6 +353,37 @@ class OllamaChatFormatter(_OllamaFormatterBase):
tool_call_msg["images"] = images
return tool_call_msg
+ @staticmethod
+ def _merge_tool_call_message(
+ tool_call_msg: dict[str, Any],
+ formatted_call: dict[str, Any],
+ ) -> None:
+ """Merge the message of a parallel tool call into the message that
+ carries the earlier tool calls of the same turn.
+
+ Args:
+ tool_call_msg (`dict[str, Any]`):
+ The tool call message to extend in place.
+ formatted_call (`dict[str, Any]`):
+ The message built by :meth:`_format_tool_call_message` for
+ the next tool call, with the text and images that came
+ between the two calls.
+ """
+ if formatted_call["content"]:
+ tool_call_msg["content"] = "\n".join(
+ part
+ for part in (
+ tool_call_msg["content"],
+ formatted_call["content"],
+ )
+ if part
+ )
+ if formatted_call.get("images"):
+ tool_call_msg.setdefault("images", []).extend(
+ formatted_call["images"],
+ )
+ tool_call_msg["tool_calls"].extend(formatted_call["tool_calls"])
+
class OllamaMultiAgentFormatter(_OllamaFormatterBase):
"""
diff --git a/tests/formatter_ollama_test.py b/tests/formatter_ollama_test.py
index 4e070fd82..e9817afa9 100644
--- a/tests/formatter_ollama_test.py
+++ b/tests/formatter_ollama_test.py
@@ -271,6 +271,137 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
res,
)
+ async def test_chat_formatter_parallel_tool_calls_share_one_message(
+ self,
+ ) -> None:
+ """Parallel tool calls stay on one assistant message. Ollama merges
+ consecutive same-role messages by appending only their content, so
+ a tool call on a second assistant message would be dropped."""
+ fmt = OllamaChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ TextBlock(text="Checking both cities."),
+ ToolCallBlock(
+ id="call_paris",
+ name="get_weather",
+ input='{"city": "Paris"}',
+ ),
+ ToolCallBlock(
+ id="call_london",
+ name="get_weather",
+ input='{"city": "London"}',
+ ),
+ ToolResultBlock(
+ id="call_paris",
+ name="get_weather",
+ output="Paris: 18C",
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolResultBlock(
+ id="call_london",
+ name="get_weather",
+ output="London: 12C",
+ state=ToolResultState.SUCCESS,
+ ),
+ TextBlock(text="Paris is warmer."),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": "Checking both cities.",
+ "tool_calls": [
+ {
+ "function": {
+ "name": "get_weather",
+ "arguments": {"city": "Paris"},
+ },
+ },
+ {
+ "function": {
+ "name": "get_weather",
+ "arguments": {"city": "London"},
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_name": "get_weather",
+ "content": "Paris: 18C",
+ },
+ {
+ "role": "tool",
+ "tool_name": "get_weather",
+ "content": "London: 12C",
+ },
+ {"role": "assistant", "content": "Paris is warmer."},
+ ],
+ res,
+ )
+
+ async def test_chat_formatter_content_between_parallel_tool_calls(
+ self,
+ ) -> None:
+ """Text and images between two tool calls of one turn join the
+ message that carries the turn's tool calls."""
+ fmt = OllamaChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ TextBlock(text="First the forecast."),
+ ToolCallBlock(
+ id="c1",
+ name="forecast",
+ input='{"city": "Paris"}',
+ ),
+ TextBlock(text="Then the map."),
+ DataBlock(
+ source=Base64Source(
+ data=self.image_b64,
+ media_type="image/png",
+ ),
+ ),
+ ToolCallBlock(
+ id="c2",
+ name="radar",
+ input='{"city": "Paris"}',
+ ),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": "First the forecast.\nThen the map.",
+ "images": [self.image_b64],
+ "tool_calls": [
+ {
+ "function": {
+ "name": "forecast",
+ "arguments": {"city": "Paris"},
+ },
+ },
+ {
+ "function": {
+ "name": "radar",
+ "arguments": {"city": "Paris"},
+ },
+ },
+ ],
+ },
+ ],
+ res,
+ )
+
async def test_chat_formatter_base64_image(self) -> None:
"""Base64 image is placed in the 'images' list as a raw base64
string."""
@@ -526,12 +657,6 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
"content": "",
"tool_calls": [
{"function": {"name": "screenshot", "arguments": {}}},
- ],
- },
- {
- "role": "assistant",
- "content": "",
- "tool_calls": [
{"function": {"name": "get_title", "arguments": {}}},
],
},
@@ -823,12 +948,6 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
"arguments": {"arg": "value1"},
},
},
- ],
- },
- {
- "role": "assistant",
- "content": "",
- "tool_calls": [
{
"function": {
"name": "func_2",