Skip to content

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

fix(formatter): revert xAI assistant media promotion (#3965)

qbc04b82b246bd5
diff --git a/src/agentscope/formatter/_xai_formatter.py b/src/agentscope/formatter/_xai_formatter.py
index 20721743b..627a64906 100644
--- a/src/agentscope/formatter/_xai_formatter.py
+++ b/src/agentscope/formatter/_xai_formatter.py
@@ -162,22 +162,9 @@ class XAIChatFormatter(FormatterBase):
def flush_assistant(
text_blocks: list[TextBlock],
tool_calls: list[ToolCallBlock],
- media: list[DataBlock],
) -> None:
"""Emit one assistant segment before its tool results or hint."""
text = "\n".join(block.text for block in text_blocks)
- media_args = _xai_user_args_from_blocks(
- media,
- image,
- self.supported_input_media_types,
- )
- if media_args:
- if text:
- xai_messages.append(assistant(text))
- text = ""
- # Keep promoted media ahead of the tool call/result sequence.
- xai_messages.append(user(*media_args))
-
if tool_calls:
msg_proto = chat_pb2.Message()
msg_proto.role = chat_pb2.MessageRole.Value("ROLE_ASSISTANT")
@@ -197,7 +184,6 @@ class XAIChatFormatter(FormatterBase):
text_blocks.clear()
tool_calls.clear()
- media.clear()
for msg in msgs:
blocks = msg.get_content_blocks()
@@ -251,7 +237,6 @@ class XAIChatFormatter(FormatterBase):
pending_text: list[TextBlock] = []
pending_tool_calls: list[ToolCallBlock] = []
pending_media: list[Any] = []
- assistant_media: list[DataBlock] = []
for block in blocks:
if pending_media and not isinstance(
@@ -265,7 +250,6 @@ class XAIChatFormatter(FormatterBase):
flush_assistant(
pending_text,
pending_tool_calls,
- assistant_media,
)
(
@@ -300,15 +284,10 @@ class XAIChatFormatter(FormatterBase):
elif isinstance(block, ThinkingBlock):
pass
- elif isinstance(block, DataBlock):
- # xAI requires media in user messages.
- assistant_media.append(block)
-
elif isinstance(block, HintBlock):
flush_assistant(
pending_text,
pending_tool_calls,
- assistant_media,
)
if isinstance(block.hint, str):
xai_messages.append(user(block.hint))
@@ -325,7 +304,6 @@ class XAIChatFormatter(FormatterBase):
flush_assistant(
pending_text,
pending_tool_calls,
- assistant_media,
)
else:
diff --git a/tests/formatter_xai_test.py b/tests/formatter_xai_test.py
index d45dd31cc..c5e057da4 100644
--- a/tests/formatter_xai_test.py
+++ b/tests/formatter_xai_test.py
@@ -290,8 +290,8 @@ class TestXAIFormatter( # pylint: disable=too-many-public-methods
res,
)
- async def test_chat_formatter_assistant_media(self) -> None:
- """Assistant images follow their text and precede the next user."""
+ async def test_chat_formatter_assistant_media_skipped(self) -> None:
+ """Skip assistant images without adding user turns."""
picture = DataBlock(
source=Base64Source(
data="SU1HX0RBVEE=",
@@ -314,17 +314,7 @@ class TestXAIFormatter( # pylint: disable=too-many-public-methods
expected = [user("Draw a chart.")]
if text:
expected.append(assistant(text))
- expected.extend(
- [
- user(
- image(
- "data:image/png;base64,SU1HX0RBVEE=",
- detail="high",
- ),
- ),
- user("Explain the chart."),
- ],
- )
+ expected.append(user("Explain the chart."))
self.assertListEqual(expected, res)
async def test_chat_formatter_system_message(self) -> None:
@@ -556,7 +546,7 @@ class TestXAIFormatter( # pylint: disable=too-many-public-methods
async def test_chat_formatter_parallel_tool_media_after_tool_msgs(
self,
) -> None:
- """Assistant media precedes tools; result media follows all results."""
+ """Skip assistant media; keep result media after all tool results."""
fmt = XAIChatFormatter()
picture = DataBlock(
id="image_1",
@@ -629,11 +619,9 @@ class TestXAIFormatter( # pylint: disable=too-many-public-methods
self.assertListEqual(
normalized_res,
[
- assistant("Original chart."),
- user(image("https://example.com/original.png")),
(
2,
- [],
+ ["Original chart."],
[
(
"call_shot",
@@ -671,10 +659,10 @@ class TestXAIFormatter( # pylint: disable=too-many-public-methods
],
)
- async def test_chat_formatter_media_before_separate_tool_result(
+ async def test_chat_formatter_media_skipped_before_tool_result(
self,
) -> None:
- """Media must not separate a tool call from a later result message."""
+ """Skipping media preserves calls paired with separate results."""
res = await XAIChatFormatter().format(
[
AssistantMsg(
@@ -723,7 +711,6 @@ class TestXAIFormatter( # pylint: disable=too-many-public-methods
self.assertListEqual(
normalized_res,
[
- user(image("https://example.com/original.png")),
(2, [], [("call_1", 1, "edit", "{}")]),
tool_result("Done.", tool_call_id="call_1"),
],
@@ -1139,7 +1126,7 @@ class TestXAIFormatter( # pylint: disable=too-many-public-methods
)
async def test_chat_formatter_assistant_media_around_hint(self) -> None:
- """Assistant images stay with their segment on either side of hints."""
+ """Skip assistant images while preserving text and hint order."""
before = DataBlock(
source=URLSource(
url="https://example.com/before.png",
@@ -1176,10 +1163,8 @@ class TestXAIFormatter( # pylint: disable=too-many-public-methods
expected = [assistant(text)] if text else []
expected.extend(
[
- user(image("https://example.com/before.png")),
user("Revise the chart."),
assistant("Revised chart."),
- user(image("https://example.com/after.png")),
],
)
self.assertListEqual(expected, res)