Skip to content

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

fix(formatter): derive multimodal tool-result identifier from the block's own stable id (#2165)

Amir Fathia25656689b6c
diff --git a/src/agentscope/formatter/_formatter_base.py b/src/agentscope/formatter/_formatter_base.py
index 50fff0969..ff34e331a 100644
--- a/src/agentscope/formatter/_formatter_base.py
+++ b/src/agentscope/formatter/_formatter_base.py
@@ -7,7 +7,6 @@ from abc import abstractmethod
from fnmatch import fnmatch
from typing import Any, List, AsyncGenerator
-import shortuuid
from pydantic import BaseModel, Field
from ..message import (
@@ -145,9 +144,11 @@ class FormatterBase(BaseModel):
):
# If supported, promote the block
- # Create an identifier for such multimodal data for
- # accurate reference (in terms of order, position, etc.)
- identifier = shortuuid.uuid()
+ # Reuse the block's own stable id as the identifier so
+ # repeated formatting of the same unchanged Msg history
+ # (e.g. on every LLM call) produces the same text, rather
+ # than a fresh random id each time.
+ identifier = block.id
textual_output.append(
f"<system-reminder>A(n) {main_type} file is returned "
diff --git a/tests/formatter_dashscope_test.py b/tests/formatter_dashscope_test.py
index 5fe6dbeca..74584ef2b 100644
--- a/tests/formatter_dashscope_test.py
+++ b/tests/formatter_dashscope_test.py
@@ -489,18 +489,14 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
)
mocked_open.assert_called_once_with("/tmp/audio.wav", "rb")
- @patch(
- "agentscope.formatter._formatter_base.shortuuid.uuid",
- return_value=_FIXED_ID,
- )
async def test_chat_formatter_url_image_in_tool_result(
self,
- _mock_uuid: object,
) -> None:
"""URL images in tool results are promoted to a follow-up user message.
The textual part of the tool result contains a system-reminder with a
- unique identifier; the identifier is mocked to be deterministic.
+ unique identifier; the identifier comes from the block's own stable
+ id.
"""
fmt = DashScopeChatFormatter()
msgs = [
@@ -518,6 +514,7 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
output=[
TextBlock(text="Here is the map."),
DataBlock(
+ id=_FIXED_ID,
source=URLSource(
url=self.image_url,
media_type="image/png",
diff --git a/tests/formatter_gemini_test.py b/tests/formatter_gemini_test.py
index 1430e3bfd..4d723b161 100644
--- a/tests/formatter_gemini_test.py
+++ b/tests/formatter_gemini_test.py
@@ -4,7 +4,6 @@ GeminiMultiAgentFormatter, following the reference test style with exact
ground-truth comparisons.
"""
from unittest import IsolatedAsyncioTestCase
-from unittest.mock import patch
from agentscope.formatter import (
GeminiChatFormatter,
@@ -373,13 +372,8 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):
self.assertEqual(thought_parts, [])
self.assertEqual(res[0]["parts"], [{"text": "reply"}])
- @patch(
- "agentscope.formatter._formatter_base.shortuuid.uuid",
- return_value=_FIXED_ID,
- )
async def test_chat_formatter_base64_image_in_tool_result(
self,
- _mock_uuid: object,
) -> None:
"""Base64 images in tool results are promoted to a follow-up user
message."""
@@ -399,6 +393,7 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):
output=[
TextBlock(text="Here is the map."),
DataBlock(
+ id=_FIXED_ID,
source=Base64Source(
data=self.image_b64,
media_type="image/png",
diff --git a/tests/formatter_moonshot_test.py b/tests/formatter_moonshot_test.py
index 2e3680372..de81e0ca7 100644
--- a/tests/formatter_moonshot_test.py
+++ b/tests/formatter_moonshot_test.py
@@ -419,13 +419,8 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
res,
)
- @patch(
- "agentscope.formatter._formatter_base.shortuuid.uuid",
- return_value=_FIXED_ID,
- )
async def test_chat_formatter_url_image_in_tool_result(
self,
- _mock_uuid: object,
) -> None:
"""URL images in tool results are promoted to a follow-up user
message."""
@@ -445,6 +440,7 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
output=[
TextBlock(text="Here is the map."),
DataBlock(
+ id=_FIXED_ID,
source=URLSource(
url=self.image_url,
media_type="image/png",
diff --git a/tests/formatter_ollama_test.py b/tests/formatter_ollama_test.py
index b97bd67db..a1b599838 100644
--- a/tests/formatter_ollama_test.py
+++ b/tests/formatter_ollama_test.py
@@ -3,7 +3,6 @@
OllamaMultiAgentFormatter, with exact ground-truth comparisons.
"""
from unittest import IsolatedAsyncioTestCase
-from unittest.mock import patch
from agentscope.formatter import OllamaChatFormatter, OllamaMultiAgentFormatter
from agentscope.message import (
@@ -297,13 +296,8 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
res,
)
- @patch(
- "agentscope.formatter._formatter_base.shortuuid.uuid",
- return_value=_FIXED_ID,
- )
async def test_chat_formatter_base64_image_in_tool_result(
self,
- _mock_uuid: object,
) -> None:
"""Base64 images in tool results are promoted to a follow-up user
message with images list."""
@@ -323,6 +317,7 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
output=[
TextBlock(text="Here is the map."),
DataBlock(
+ id=_FIXED_ID,
source=Base64Source(
data=self.image_b64,
media_type="image/png",
diff --git a/tests/formatter_openai_chat_test.py b/tests/formatter_openai_chat_test.py
index 87d6a8147..e1e58e7de 100644
--- a/tests/formatter_openai_chat_test.py
+++ b/tests/formatter_openai_chat_test.py
@@ -475,13 +475,8 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
res,
)
- @patch(
- "agentscope.formatter._formatter_base.shortuuid.uuid",
- return_value=_FIXED_ID,
- )
async def test_chat_formatter_url_image_in_tool_result(
self,
- _mock_uuid: object,
) -> None:
"""URL images in tool results are promoted to a follow-up user
message."""
@@ -501,6 +496,7 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
output=[
TextBlock(text="Here is the map."),
DataBlock(
+ id=_FIXED_ID,
source=URLSource(
url=self.image_url,
media_type="image/png",
@@ -584,6 +580,45 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
res,
)
+ async def test_chat_formatter_tool_result_image_id_is_deterministic(
+ self,
+ ) -> None:
+ """Formatting the same unchanged Msg history twice must produce the
+ same multimodal identifier both times (issue #2161): the id is
+ derived from the DataBlock's own stable id, not freshly generated
+ on every call."""
+ fmt = OpenAIChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ToolCallBlock(
+ id="call_img",
+ name="get_map",
+ input='{"city": "Tokyo"}',
+ ),
+ ToolResultBlock(
+ id="call_img",
+ name="get_map",
+ output=[
+ TextBlock(text="Here is the map."),
+ DataBlock(
+ source=URLSource(
+ url=self.image_url,
+ media_type="image/png",
+ ),
+ ),
+ ],
+ state=ToolResultState.SUCCESS,
+ ),
+ TextBlock(text="Here is the map of Tokyo."),
+ ],
+ ),
+ ]
+ first = await fmt.format(msgs)
+ second = await fmt.format(msgs)
+ self.assertListEqual(first, second)
+
# -------------------------------------------------------------------
# OpenAIMultiAgentFormatter tests
# -------------------------------------------------------------------