Skip to content

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

fix(formatter): refine formatters and unittest (#1621)

qbc4b1228db666c
diff --git a/src/agentscope/formatter/_anthropic_formatter.py b/src/agentscope/formatter/_anthropic_formatter.py
index 84a09fc6c..b140d3967 100644
--- a/src/agentscope/formatter/_anthropic_formatter.py
+++ b/src/agentscope/formatter/_anthropic_formatter.py
@@ -51,11 +51,25 @@ class _AnthropicFormatterBase(FormatterBase, ABC):
self.assert_list_of_msgs(msgs)
messages: list[dict] = []
- for msg in msgs:
+ for msg in msgs: # pylint: disable=too-many-nested-blocks
content_blocks: list = []
has_tool_result = False
for block in msg.get_content_blocks():
+ if (
+ has_tool_result
+ and content_blocks
+ and not isinstance(
+ block,
+ ToolResultBlock,
+ )
+ ):
+ messages.append(
+ {"role": "user", "content": content_blocks},
+ )
+ content_blocks = []
+ has_tool_result = False
+
if isinstance(block, TextBlock):
content_blocks.append(
{"type": "text", "text": block.text},
@@ -75,7 +89,22 @@ class _AnthropicFormatterBase(FormatterBase, ABC):
)
elif isinstance(block, HintBlock):
- pass # Anthropic does not support hint blocks
+ if content_blocks:
+ role = "user" if has_tool_result else msg.role
+ messages.append(
+ {"role": role, "content": content_blocks},
+ )
+ content_blocks = []
+ has_tool_result = False
+
+ messages.append(
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": block.hint},
+ ],
+ },
+ )
elif isinstance(block, DataBlock):
formatted_block = self._format_anthropic_data_block(block)
@@ -95,25 +124,47 @@ class _AnthropicFormatterBase(FormatterBase, ABC):
)
elif isinstance(block, ToolResultBlock):
- (
- textual_output,
- multimodal_data,
- ) = self.convert_tool_result_to_string(block.output)
-
- tool_result_content = []
- if textual_output:
- tool_result_content.append(
- {"type": "text", "text": textual_output},
+ if content_blocks:
+ role = "user" if has_tool_result else msg.role
+ messages.append(
+ {"role": role, "content": content_blocks},
)
+ content_blocks = []
- for data_block in multimodal_data:
- if not isinstance(data_block, DataBlock):
- continue
- formatted_block = self._format_anthropic_data_block(
- data_block,
+ tool_result_content: list[dict] = []
+ output = block.output
+ if isinstance(output, str):
+ tool_result_content.append(
+ {"type": "text", "text": output},
)
- if formatted_block:
- tool_result_content.append(formatted_block)
+ else:
+ for out_block in output:
+ if isinstance(out_block, TextBlock):
+ tool_result_content.append(
+ {"type": "text", "text": out_block.text},
+ )
+ elif isinstance(out_block, DataBlock):
+ fmt_block = self._format_anthropic_data_block(
+ out_block,
+ )
+ if fmt_block:
+ tool_result_content.append(fmt_block)
+ else:
+ source = out_block.source
+ main_type = source.media_type.split("/")[0]
+ if isinstance(source, URLSource):
+ fallback = (
+ f"[{main_type} file returned, "
+ f"URL: {source.url}]"
+ )
+ else:
+ fallback = (
+ f"[{main_type} file returned, "
+ f"type: {source.media_type}]"
+ )
+ tool_result_content.append(
+ {"type": "text", "text": fallback},
+ )
content_blocks.append(
{
diff --git a/src/agentscope/formatter/_dashscope_formatter.py b/src/agentscope/formatter/_dashscope_formatter.py
index 67b5bf5b9..73959045c 100644
--- a/src/agentscope/formatter/_dashscope_formatter.py
+++ b/src/agentscope/formatter/_dashscope_formatter.py
@@ -19,7 +19,6 @@ from ..message import (
DataBlock,
ToolCallBlock,
Base64Source,
- UserMsg,
HintBlock,
)
@@ -269,16 +268,30 @@ class DashScopeChatFormatter(_DashScopeFormatterBase):
content_blocks.append(formatted_block)
elif isinstance(block, HintBlock):
- if content_blocks or tool_calls:
+ if content_blocks or tool_calls or thinking_parts:
msg_openai: dict[str, Any] = {
"role": msg.role,
"content": content_blocks or None,
}
if tool_calls:
msg_openai["tool_calls"] = tool_calls
+ if thinking_parts:
+ msg_openai["reasoning_content"] = "\n".join(
+ thinking_parts,
+ )
formatted_msgs.append(msg_openai)
content_blocks = []
tool_calls = []
+ thinking_parts = []
+
+ formatted_msgs.append(
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": block.hint},
+ ],
+ },
+ )
elif isinstance(block, ToolCallBlock):
tool_calls.append(
@@ -297,6 +310,22 @@ class DashScopeChatFormatter(_DashScopeFormatterBase):
thinking_parts.append(block.thinking)
elif isinstance(block, ToolResultBlock):
+ if content_blocks or tool_calls or thinking_parts:
+ msg_flush: dict[str, Any] = {
+ "role": msg.role,
+ "content": content_blocks or None,
+ }
+ if tool_calls:
+ msg_flush["tool_calls"] = tool_calls
+ if thinking_parts:
+ msg_flush["reasoning_content"] = "\n".join(
+ thinking_parts,
+ )
+ formatted_msgs.append(msg_flush)
+ content_blocks = []
+ tool_calls = []
+ thinking_parts = []
+
(
textual_output,
multimodal_data,
@@ -312,13 +341,26 @@ class DashScopeChatFormatter(_DashScopeFormatterBase):
)
if multimodal_data:
- msgs.insert(
- i + 1,
- UserMsg(
- name="system-reminder",
- content=multimodal_data,
- ),
- )
+ promo_content = []
+ for item in multimodal_data:
+ if isinstance(item, TextBlock):
+ promo_content.append(
+ {"type": "text", "text": item.text},
+ )
+ elif isinstance(item, DataBlock):
+ fmt_item = self._format_dashscope_data_block(
+ item,
+ role="user",
+ )
+ if fmt_item is not None:
+ promo_content.append(fmt_item)
+ if promo_content:
+ formatted_msgs.append(
+ {
+ "role": "user",
+ "content": promo_content,
+ },
+ )
else:
logger.warning(
@@ -337,7 +379,11 @@ class DashScopeChatFormatter(_DashScopeFormatterBase):
if thinking_parts:
msg_dashscope["reasoning_content"] = "\n".join(thinking_parts)
- if msg_dashscope["content"] or msg_dashscope.get("tool_calls"):
+ if (
+ msg_dashscope["content"]
+ or msg_dashscope.get("tool_calls")
+ or msg_dashscope.get("reasoning_content")
+ ):
formatted_msgs.append(msg_dashscope)
i += 1
diff --git a/src/agentscope/formatter/_deepseek_formatter.py b/src/agentscope/formatter/_deepseek_formatter.py
index 4302eef49..3b26741b1 100644
--- a/src/agentscope/formatter/_deepseek_formatter.py
+++ b/src/agentscope/formatter/_deepseek_formatter.py
@@ -60,7 +60,35 @@ class DeepSeekChatFormatter(FormatterBase):
reasoning_content_blocks.append(block.thinking)
elif isinstance(block, HintBlock):
- pass # DeepSeek does not support hint blocks
+ if (
+ content_blocks
+ or tool_calls
+ or reasoning_content_blocks
+ ):
+ content_text = "\n".join(
+ b.get("text", "") for b in content_blocks
+ )
+ msg_flush_hint: dict[str, Any] = {
+ "role": msg.role,
+ "content": content_text
+ or (None if tool_calls else ""),
+ }
+ if msg.role == "assistant":
+ msg_flush_hint["reasoning_content"] = (
+ "\n".join(reasoning_content_blocks)
+ if reasoning_content_blocks
+ else ""
+ )
+ if tool_calls:
+ msg_flush_hint["tool_calls"] = tool_calls
+ messages.append(msg_flush_hint)
+ content_blocks = []
+ reasoning_content_blocks = []
+ tool_calls = []
+
+ messages.append(
+ {"role": "user", "content": block.hint},
+ )
elif isinstance(block, ToolCallBlock):
tool_calls.append(
@@ -75,6 +103,32 @@ class DeepSeekChatFormatter(FormatterBase):
)
elif isinstance(block, ToolResultBlock):
+ if (
+ content_blocks
+ or tool_calls
+ or reasoning_content_blocks
+ ):
+ content_text = "\n".join(
+ b.get("text", "") for b in content_blocks
+ )
+ msg_flush: dict[str, Any] = {
+ "role": msg.role,
+ "content": content_text
+ or (None if tool_calls else ""),
+ }
+ if msg.role == "assistant":
+ msg_flush["reasoning_content"] = (
+ "\n".join(reasoning_content_blocks)
+ if reasoning_content_blocks
+ else ""
+ )
+ if tool_calls:
+ msg_flush["tool_calls"] = tool_calls
+ messages.append(msg_flush)
+ content_blocks = []
+ reasoning_content_blocks = []
+ tool_calls = []
+
textual_output, _ = self.convert_tool_result_to_string(
block.output,
)
@@ -115,7 +169,11 @@ class DeepSeekChatFormatter(FormatterBase):
if tool_calls:
msg_deepseek["tool_calls"] = tool_calls
- if msg_deepseek["content"] or msg_deepseek.get("tool_calls"):
+ if (
+ msg_deepseek["content"]
+ or msg_deepseek.get("tool_calls")
+ or reasoning_content_blocks
+ ):
messages.append(msg_deepseek)
return messages
diff --git a/src/agentscope/formatter/_gemini_formatter.py b/src/agentscope/formatter/_gemini_formatter.py
index 16a34d70d..1854b9323 100644
--- a/src/agentscope/formatter/_gemini_formatter.py
+++ b/src/agentscope/formatter/_gemini_formatter.py
@@ -19,7 +19,6 @@ from ..message import (
DataBlock,
ToolCallBlock,
ToolResultBlock,
- UserMsg,
URLSource,
Base64Source,
)
@@ -161,7 +160,17 @@ class GeminiChatFormatter(_GeminiFormatterBase):
parts.append({"thought": True, "text": block.thinking})
elif isinstance(block, HintBlock):
- pass # Gemini does not support hint blocks
+ if parts:
+ role = "model" if msg.role == "assistant" else "user"
+ messages.append({"role": role, "parts": parts})
+ parts = []
+
+ messages.append(
+ {
+ "role": "user",
+ "parts": [{"text": block.hint}],
+ },
+ )
elif isinstance(block, DataBlock):
formatted = self._format_gemini_data_block(block)
@@ -180,6 +189,11 @@ class GeminiChatFormatter(_GeminiFormatterBase):
)
elif isinstance(block, ToolResultBlock):
+ if parts:
+ role = "model" if msg.role == "assistant" else "user"
+ messages.append({"role": role, "parts": parts})
+ parts = []
+
(
textual_output,
multimodal_data,
@@ -203,13 +217,20 @@ class GeminiChatFormatter(_GeminiFormatterBase):
)
if multimodal_data:
- msgs.insert(
- i + 1,
- UserMsg(
- name="system-reminder",
- content=multimodal_data,
- ),
- )
+ promo_parts = []
+ for item in multimodal_data:
+ if isinstance(item, TextBlock):
+ promo_parts.append({"text": item.text})
+ elif isinstance(item, DataBlock):
+ fmt_item = self._format_gemini_data_block(
+ item,
+ )
+ if fmt_item is not None:
+ promo_parts.append(fmt_item)
+ if promo_parts:
+ messages.append(
+ {"role": "user", "parts": promo_parts},
+ )
else:
logger.warning(
diff --git a/src/agentscope/formatter/_moonshot_formatter.py b/src/agentscope/formatter/_moonshot_formatter.py
index 680c77bb7..b320c82e2 100644
--- a/src/agentscope/formatter/_moonshot_formatter.py
+++ b/src/agentscope/formatter/_moonshot_formatter.py
@@ -14,7 +14,6 @@ from ..message import (
HintBlock,
ToolCallBlock,
ToolResultBlock,
- UserMsg,
)
@@ -82,15 +81,17 @@ class MoonshotChatFormatter(_OpenAIFormatterBase):
content_blocks.append(formatted)
elif isinstance(block, HintBlock):
- if content_blocks or tool_calls:
+ if content_blocks or tool_calls or reasoning_parts:
msg_moonshot = {
"role": msg.role,
"name": msg.name,
"content": content_blocks or None,
}
- if reasoning_parts:
- msg_moonshot["reasoning_content"] = "\n".join(
- reasoning_parts,
+ if msg.role == "assistant":
+ msg_moonshot["reasoning_content"] = (
+ "\n".join(reasoning_parts)
+ if reasoning_parts
+ else ""
)
if tool_calls:
msg_moonshot["tool_calls"] = tool_calls
@@ -99,6 +100,15 @@ class MoonshotChatFormatter(_OpenAIFormatterBase):
reasoning_parts = []
tool_calls = []
+ messages.append(
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": block.hint},
+ ],
+ },
+ )
+
elif isinstance(block, ToolCallBlock):
tool_calls.append(
{
@@ -112,6 +122,25 @@ class MoonshotChatFormatter(_OpenAIFormatterBase):
)
elif isinstance(block, ToolResultBlock):
+ if content_blocks or tool_calls or reasoning_parts:
+ msg_flush = {
+ "role": msg.role,
+ "name": msg.name,
+ "content": content_blocks or None,
+ }
+ if msg.role == "assistant":
+ msg_flush["reasoning_content"] = (
+ "\n".join(reasoning_parts)
+ if reasoning_parts
+ else ""
+ )
+ if tool_calls:
+ msg_flush["tool_calls"] = tool_calls
+ messages.append(msg_flush)
+ content_blocks = []
+ reasoning_parts = []
+ tool_calls = []
+
(
textual_output,
multimodal_data,
@@ -127,13 +156,27 @@ class MoonshotChatFormatter(_OpenAIFormatterBase):
)
if multimodal_data:
- msgs.insert(
- i + 1,
- UserMsg(
- name="system-reminder",
- content=multimodal_data,
- ),
- )
+ promo_content = []
+ for item in multimodal_data:
+ if isinstance(item, TextBlock):
+ promo_content.append(
+ {"type": "text", "text": item.text},
+ )
+ elif isinstance(item, DataBlock):
+ fmt_item = self._format_openai_data_block(
+ item,
+ role="user",
+ )
+ if fmt_item is not None:
+ promo_content.append(fmt_item)
+ if promo_content:
+ messages.append(
+ {
+ "role": "user",
+ "name": "system-reminder",
+ "content": promo_content,
+ },
+ )
else:
logger.warning(
@@ -159,7 +202,11 @@ class MoonshotChatFormatter(_OpenAIFormatterBase):
if tool_calls:
msg_moonshot["tool_calls"] = tool_calls
- if msg_moonshot["content"] or msg_moonshot.get("tool_calls"):
+ if (
+ msg_moonshot["content"]
+ or msg_moonshot.get("tool_calls")
+ or reasoning_parts
+ ):
messages.append(msg_moonshot)
i += 1
diff --git a/src/agentscope/formatter/_ollama_formatter.py b/src/agentscope/formatter/_ollama_formatter.py
index c20196a3c..88e9b0231 100644
--- a/src/agentscope/formatter/_ollama_formatter.py
+++ b/src/agentscope/formatter/_ollama_formatter.py
@@ -139,7 +139,20 @@ class OllamaChatFormatter(_OllamaFormatterBase):
content_parts.append(block.text)
elif isinstance(block, HintBlock):
- pass # Ollama does not support hint blocks
+ if content_parts or images:
+ msg_flush = {
+ "role": msg.role,
+ "content": "\n".join(content_parts),
+ }
+ if images:
+ msg_flush["images"] = images
+ messages.append(msg_flush)
+ content_parts = []
+ images = []
+
+ messages.append(
+ {"role": "user", "content": block.hint},
+ )
elif isinstance(block, DataBlock):
formatted_image = self._format_ollama_data_block(block)
@@ -176,6 +189,17 @@ class OllamaChatFormatter(_OllamaFormatterBase):
images = []
elif isinstance(block, ToolResultBlock):
+ if content_parts or images:
+ msg_flush = {
+ "role": msg.role,
+ "content": "\n".join(content_parts),
+ }
+ if images:
+ msg_flush["images"] = images
+ messages.append(msg_flush)
+ content_parts = []
+ images = []
+
(
textual_output,
multimodal_data,
diff --git a/src/agentscope/formatter/_openai_formatter.py b/src/agentscope/formatter/_openai_formatter.py
index 1cdcb17ef..d1a9212b4 100644
--- a/src/agentscope/formatter/_openai_formatter.py
+++ b/src/agentscope/formatter/_openai_formatter.py
@@ -21,7 +21,6 @@ from ..message import (
ToolResultBlock,
HintBlock,
ThinkingBlock,
- UserMsg,
)
@@ -244,9 +243,6 @@ class OpenAIChatFormatter(_OpenAIFormatterBase):
content_blocks.append(formatted)
elif isinstance(block, HintBlock):
- # Insert a new user message with the hint content right
- # after the current message, and go on processing the
- # rest of the blocks in the current message
if content_blocks or tool_calls:
msg_openai = {
"role": msg.role,
@@ -259,6 +255,15 @@ class OpenAIChatFormatter(_OpenAIFormatterBase):
content_blocks = []
tool_calls = []
+ messages.append(
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": block.hint},
+ ],
+ },
+ )
+
elif isinstance(block, ToolCallBlock):
tool_calls.append(
{
@@ -272,6 +277,18 @@ class OpenAIChatFormatter(_OpenAIFormatterBase):
)
elif isinstance(block, ToolResultBlock):
+ if content_blocks or tool_calls:
+ msg_openai_flush = {
+ "role": msg.role,
+ "name": msg.name,
+ "content": content_blocks or None,
+ }
+ if tool_calls:
+ msg_openai_flush["tool_calls"] = tool_calls
+ messages.append(msg_openai_flush)
+ content_blocks = []
+ tool_calls = []
+
(
textual_output,
multimodal_data,
@@ -287,13 +304,27 @@ class OpenAIChatFormatter(_OpenAIFormatterBase):
)
if multimodal_data:
- msgs.insert(
- i + 1,
- UserMsg(
- name="system-reminder",
- content=multimodal_data,
- ),
- )
+ promo_content = []
+ for item in multimodal_data:
+ if isinstance(item, TextBlock):
+ promo_content.append(
+ {"type": "text", "text": item.text},
+ )
+ elif isinstance(item, DataBlock):
+ fmt_item = self._format_openai_data_block(
+ item,
+ role="user",
+ )
+ if fmt_item is not None:
+ promo_content.append(fmt_item)
+ if promo_content:
+ messages.append(
+ {
+ "role": "user",
+ "name": "system-reminder",
+ "content": promo_content,
+ },
+ )
elif isinstance(block, ThinkingBlock):
# OpenAI API does not accept reasoning/thinking content
diff --git a/src/agentscope/formatter/_openai_response_formatter.py b/src/agentscope/formatter/_openai_response_formatter.py
index 71a19c405..fee822f1a 100644
--- a/src/agentscope/formatter/_openai_response_formatter.py
+++ b/src/agentscope/formatter/_openai_response_formatter.py
@@ -15,7 +15,6 @@ from ..message import (
ToolResultBlock,
HintBlock,
ThinkingBlock,
- UserMsg,
)
@@ -119,7 +118,18 @@ class OpenAIResponseFormatter(_OpenAIResponseFormatterBase):
content_parts.append(formatted)
elif isinstance(block, HintBlock):
- if content_parts:
+ if function_calls:
+ if content_parts:
+ items.append(
+ {
+ "role": msg.role,
+ "content": content_parts,
+ },
+ )
+ content_parts = []
+ items.extend(function_calls)
+ function_calls = []
+ elif content_parts:
items.append(
{
"role": msg.role,
@@ -128,6 +138,18 @@ class OpenAIResponseFormatter(_OpenAIResponseFormatterBase):
)
content_parts = []
+ items.append(
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "input_text",
+ "text": block.hint,
+ },
+ ],
+ },
+ )
+
elif isinstance(block, ThinkingBlock):
# When reasoning_item_id is present the block originated
# from a Responses API "reasoning" output item. The API
@@ -186,6 +208,26 @@ class OpenAIResponseFormatter(_OpenAIResponseFormatterBase):
)
elif isinstance(block, ToolResultBlock):
+ if function_calls:
+ if content_parts:
+ items.append(
+ {
+ "role": msg.role,
+ "content": content_parts,
+ },
+ )
+ content_parts = []
+ items.extend(function_calls)
+ function_calls = []
+ elif content_parts:
+ items.append(
+ {
+ "role": msg.role,
+ "content": content_parts,
+ },
+ )
+ content_parts = []
+
(
textual_output,
multimodal_data,
@@ -200,13 +242,29 @@ class OpenAIResponseFormatter(_OpenAIResponseFormatterBase):
)
if multimodal_data:
- msgs.insert(
- i + 1,
- UserMsg(
- name="system-reminder",
- content=multimodal_data,
- ),
- )
+ promo_content = []
+ for item in multimodal_data:
+ if isinstance(item, TextBlock):
+ promo_content.append(
+ {
+ "type": "input_text",
+ "text": item.text,
+ },
+ )
+ elif isinstance(item, DataBlock):
+ fmt_item = self._format_response_data_block(
+ item,
+ role="user",
+ )
+ if fmt_item is not None:
+ promo_content.append(fmt_item)
+ if promo_content:
+ items.append(
+ {
+ "role": "user",
+ "content": promo_content,
+ },
+ )
else:
logger.warning(
diff --git a/src/agentscope/formatter/_xai_formatter.py b/src/agentscope/formatter/_xai_formatter.py
index bfb665951..cd89f280a 100644
--- a/src/agentscope/formatter/_xai_formatter.py
+++ b/src/agentscope/formatter/_xai_formatter.py
@@ -46,6 +46,7 @@ class XAIChatFormatter(FormatterBase):
),
)
+ # pylint: disable=too-many-statements, too-many-branches
async def format(
self,
msgs: list[Msg],
@@ -82,12 +83,6 @@ class XAIChatFormatter(FormatterBase):
blocks = msg.get_content_blocks()
text_blocks = [b for b in blocks if isinstance(b, TextBlock)]
- tool_call_blocks = [
- b for b in blocks if isinstance(b, ToolCallBlock)
- ]
- tool_result_blocks = [
- b for b in blocks if isinstance(b, ToolResultBlock)
- ]
if msg.role == "system":
text = "\n".join(b.text for b in text_blocks)
@@ -96,8 +91,13 @@ class XAIChatFormatter(FormatterBase):
elif msg.role == "user":
content_args: list = []
for block in blocks:
- if isinstance(block, (HintBlock, ThinkingBlock)):
+ if isinstance(block, ThinkingBlock):
pass
+ elif isinstance(block, HintBlock):
+ if content_args:
+ xai_messages.append(user(*content_args))
+ content_args = []
+ xai_messages.append(user(block.hint))
elif isinstance(block, TextBlock):
content_args.append(block.text)
elif isinstance(block, DataBlock):
@@ -146,29 +146,102 @@ class XAIChatFormatter(FormatterBase):
xai_messages.append(user(*content_args))
elif msg.role == "assistant":
- if tool_result_blocks:
- # Convert each ToolResultBlock to a tool_result message.
- for tr_block in tool_result_blocks:
+ pending_text: list[TextBlock] = []
+ pending_tool_calls: list[ToolCallBlock] = []
+
+ for block in blocks:
+ if isinstance(block, ToolResultBlock):
+ # Convert each ToolResultBlock to a tool_result
+ # message.
+ if pending_tool_calls:
+ msg_proto = chat_pb2.Message()
+ msg_proto.role = chat_pb2.MessageRole.Value(
+ "ROLE_ASSISTANT",
+ )
+ if pending_text:
+ c = msg_proto.content.add()
+ c.text = "\n".join(
+ b.text for b in pending_text
+ )
+ pending_text = []
+ for tc in pending_tool_calls:
+ proto_tc = msg_proto.tool_calls.add()
+ proto_tc.id = tc.id
+ proto_tc.type = chat_pb2.ToolCallType.Value(
+ "TOOL_CALL_TYPE_CLIENT_SIDE_TOOL",
+ )
+ proto_tc.function.name = tc.name
+ proto_tc.function.arguments = tc.input
+ xai_messages.append(msg_proto)
+ pending_tool_calls = []
+ elif pending_text:
+ text = "\n".join(b.text for b in pending_text)
+ if text:
+ xai_messages.append(assistant(text))
+ pending_text = []
+
output_text = self._extract_result_text(
- tr_block.output,
+ block.output,
)
xai_messages.append(
tool_result(
output_text,
- tool_call_id=tr_block.id,
+ tool_call_id=block.id,
),
)
- elif tool_call_blocks:
+ elif isinstance(block, ToolCallBlock):
+ pending_tool_calls.append(block)
+
+ elif isinstance(block, TextBlock):
+ pending_text.append(block)
+
+ elif isinstance(block, ThinkingBlock):
+ pass
+
+ elif isinstance(block, HintBlock):
+ if pending_tool_calls or pending_text:
+ if pending_tool_calls:
+ msg_proto = chat_pb2.Message()
+ msg_proto.role = chat_pb2.MessageRole.Value(
+ "ROLE_ASSISTANT",
+ )
+ if pending_text:
+ c = msg_proto.content.add()
+ c.text = "\n".join(
+ b.text for b in pending_text
+ )
+ pending_text = []
+ for tc in pending_tool_calls:
+ proto_tc = msg_proto.tool_calls.add()
+ proto_tc.id = tc.id
+ proto_tc.type = (
+ chat_pb2.ToolCallType.Value(
+ "TOOL_CALL_TYPE_CLIENT_SIDE_TOOL",
+ )
+ )
+ proto_tc.function.name = tc.name
+ proto_tc.function.arguments = tc.input
+ xai_messages.append(msg_proto)
+ pending_tool_calls = []
+ elif pending_text:
+ text = "\n".join(b.text for b in pending_text)
+ if text:
+ xai_messages.append(assistant(text))
+ pending_text = []
+ xai_messages.append(user(block.hint))
+
+ if pending_tool_calls:
# Assistant turn that triggered tool calls (history).
msg_proto = chat_pb2.Message()
msg_proto.role = chat_pb2.MessageRole.Value(
"ROLE_ASSISTANT",
)
- if text_blocks:
+ if pending_text:
c = msg_proto.content.add()
- c.text = "\n".join(b.text for b in text_blocks)
- for tc in tool_call_blocks:
+ c.text = "\n".join(b.text for b in pending_text)
+ pending_text = []
+ for tc in pending_tool_calls:
proto_tc = msg_proto.tool_calls.add()
proto_tc.id = tc.id
proto_tc.type = chat_pb2.ToolCallType.Value(
@@ -177,10 +250,9 @@ class XAIChatFormatter(FormatterBase):
proto_tc.function.name = tc.name
proto_tc.function.arguments = tc.input
xai_messages.append(msg_proto)
-
- else:
+ elif pending_text:
# Regular assistant text message.
- text = "\n".join(b.text for b in text_blocks)
+ text = "\n".join(b.text for b in pending_text)
if text:
xai_messages.append(assistant(text))
diff --git a/src/agentscope/model/_dashscope/_model.py b/src/agentscope/model/_dashscope/_model.py
index aa4a3c845..2d63dbf4a 100644
--- a/src/agentscope/model/_dashscope/_model.py
+++ b/src/agentscope/model/_dashscope/_model.py
@@ -299,13 +299,16 @@ class DashScopeChatModel(ChatModelBase):
acc_thinking.thinking += delta_thinking
acc_text.text += delta_text
+ delta_tool_call_blocks: List[ToolCallBlock] = []
for tool_call in getattr(delta, "tool_calls", None) or []:
idx = tool_call.index
+ args = (
+ tool_call.function.arguments
+ if tool_call.function
+ else ""
+ ) or ""
if idx in acc_tool_calls:
- if tool_call.function and tool_call.function.arguments:
- acc_tool_calls[idx][
- "input"
- ] += tool_call.function.arguments
+ acc_tool_calls[idx]["input"] += args
else:
acc_tool_calls[idx] = {
"id": tool_call.id or "",
@@ -314,13 +317,16 @@ class DashScopeChatModel(ChatModelBase):
if tool_call.function
else ""
),
- "input": (
- tool_call.function.arguments
- if tool_call.function
- else ""
- )
- or "",
+ "input": args,
}
+ tc = acc_tool_calls[idx]
+ delta_tool_call_blocks.append(
+ ToolCallBlock(
+ id=tc["id"],
+ name=tc["name"],
+ input=args,
+ ),
+ )
delta_contents: List[
TextBlock | ToolCallBlock | ThinkingBlock
@@ -336,6 +342,7 @@ class DashScopeChatModel(ChatModelBase):
delta_contents.append(
TextBlock(id=acc_text.id, text=delta_text),
)
+ delta_contents.extend(delta_tool_call_blocks)
if delta_contents:
_kwargs: dict[str, Any] = {
diff --git a/src/agentscope/model/_deepseek/_model.py b/src/agentscope/model/_deepseek/_model.py
index 5ff9de68c..74e7d6550 100644
--- a/src/agentscope/model/_deepseek/_model.py
+++ b/src/agentscope/model/_deepseek/_model.py
@@ -236,19 +236,26 @@ class DeepSeekChatModel(ChatModelBase):
acc_thinking.thinking += delta_thinking
acc_text.text += delta_text
+ delta_tool_call_blocks: List[ToolCallBlock] = []
for tool_call in getattr(delta, "tool_calls", None) or []:
idx = tool_call.index
+ args = tool_call.function.arguments or ""
if idx in acc_tool_calls:
- if tool_call.function.arguments is not None:
- acc_tool_calls[idx][
- "input"
- ] += tool_call.function.arguments
+ acc_tool_calls[idx]["input"] += args
else:
acc_tool_calls[idx] = {
"id": tool_call.id,
"name": tool_call.function.name,
- "input": tool_call.function.arguments or "",
+ "input": args,
}
+ tc = acc_tool_calls[idx]
+ delta_tool_call_blocks.append(
+ ToolCallBlock(
+ id=tc["id"],
+ name=tc["name"],
+ input=args,
+ ),
+ )
delta_contents: List[
TextBlock | ToolCallBlock | ThinkingBlock
@@ -264,6 +271,7 @@ class DeepSeekChatModel(ChatModelBase):
delta_contents.append(
TextBlock(id=acc_text.id, text=delta_text),
)
+ delta_contents.extend(delta_tool_call_blocks)
if delta_contents:
_kwargs: dict[str, Any] = {
diff --git a/src/agentscope/model/_moonshot/_model.py b/src/agentscope/model/_moonshot/_model.py
index de8c11ca9..9739614e0 100644
--- a/src/agentscope/model/_moonshot/_model.py
+++ b/src/agentscope/model/_moonshot/_model.py
@@ -243,25 +243,37 @@ class MoonshotChatModel(ChatModelBase):
delta_text = getattr(delta, "content", None) or ""
acc_text.text += delta_text
+ delta_tool_call_blocks: List[ToolCallBlock] = []
for tool_call in getattr(delta, "tool_calls", None) or []:
idx = tool_call.index
+ args = tool_call.function.arguments or ""
if idx in acc_tool_calls:
- if tool_call.function.arguments is not None:
- acc_tool_calls[idx][
- "input"
- ] += tool_call.function.arguments
+ acc_tool_calls[idx]["input"] += args
else:
acc_tool_calls[idx] = {
"id": tool_call.id,
"name": tool_call.function.name,
- "input": tool_call.function.arguments or "",
+ "input": args,
}
+ tc = acc_tool_calls[idx]
+ delta_tool_call_blocks.append(
+ ToolCallBlock(
+ id=tc["id"],
+ name=tc["name"],
+ input=args,
+ ),
+ )
+ delta_contents: List[TextBlock | ToolCallBlock] = []
if delta_text:
+ delta_contents.append(
+ TextBlock(id=acc_text.id, text=delta_text),
+ )
+ delta_contents.extend(delta_tool_call_blocks)
+
+ if delta_contents:
_text_kwargs: dict[str, Any] = {
- "content": [
- TextBlock(id=acc_text.id, text=delta_text),
- ],
+ "content": delta_contents,
"usage": usage,
"is_last": False,
}
diff --git a/src/agentscope/model/_openai_chat/_model.py b/src/agentscope/model/_openai_chat/_model.py
index 3d171f11f..62802e8d5 100644
--- a/src/agentscope/model/_openai_chat/_model.py
+++ b/src/agentscope/model/_openai_chat/_model.py
@@ -251,19 +251,26 @@ class OpenAIChatModel(ChatModelBase):
acc_thinking.thinking += delta_thinking
acc_text.text += delta_text
+ delta_tool_call_blocks: List[ToolCallBlock] = []
for tool_call in getattr(delta, "tool_calls", None) or []:
idx = tool_call.index
+ args = tool_call.function.arguments or ""
if idx in acc_tool_calls:
- if tool_call.function.arguments is not None:
- acc_tool_calls[idx][
- "input"
- ] += tool_call.function.arguments
+ acc_tool_calls[idx]["input"] += args
else:
acc_tool_calls[idx] = {
"id": tool_call.id,
"name": tool_call.function.name,
- "input": tool_call.function.arguments or "",
+ "input": args,
}
+ tc = acc_tool_calls[idx]
+ delta_tool_call_blocks.append(
+ ToolCallBlock(
+ id=tc["id"],
+ name=tc["name"],
+ input=args,
+ ),
+ )
delta_contents: List[
TextBlock | ToolCallBlock | ThinkingBlock
@@ -279,6 +286,7 @@ class OpenAIChatModel(ChatModelBase):
delta_contents.append(
TextBlock(id=acc_text.id, text=delta_text),
)
+ delta_contents.extend(delta_tool_call_blocks)
if delta_contents:
_kwargs: dict[str, Any] = {
diff --git a/src/agentscope/model/_openai_response/_model.py b/src/agentscope/model/_openai_response/_model.py
index 90b826d6f..8a7af8347 100644
--- a/src/agentscope/model/_openai_response/_model.py
+++ b/src/agentscope/model/_openai_response/_model.py
@@ -251,6 +251,15 @@ class OpenAIResponseModel(ChatModelBase):
item_id = event.item_id
if item_id in tool_calls:
tool_calls[item_id]["input"] += event.delta
+ tc = tool_calls[item_id]
+ delta_contents.append(
+ ToolCallBlock(
+ id=tc["id"],
+ call_id=tc.get("call_id"),
+ name=tc["name"],
+ input=event.delta,
+ ),
+ )
elif event_type == "response.completed":
resp = event.response
diff --git a/tests/formatter_anthropic_test.py b/tests/formatter_anthropic_test.py
index 88936831c..4b1b1cc99 100644
--- a/tests/formatter_anthropic_test.py
+++ b/tests/formatter_anthropic_test.py
@@ -10,15 +10,18 @@ from agentscope.formatter import (
AnthropicMultiAgentFormatter,
)
from agentscope.message import (
- Msg,
+ UserMsg,
+ AssistantMsg,
+ SystemMsg,
TextBlock,
DataBlock,
ToolCallBlock,
ToolResultBlock,
+ ToolResultState,
Base64Source,
ThinkingBlock,
+ HintBlock,
)
-from agentscope.message._block import ToolResultState
class TestAnthropicFormatter(IsolatedAsyncioTestCase):
@@ -33,43 +36,37 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
# (No URL images: Anthropic URL handling downloads from the network)
# ---------------------------------------------------------------
self.msgs_system = [
- Msg(
+ SystemMsg(
name="system",
content="You're a helpful assistant.",
- role="system",
),
]
self.msgs_conversation = [
- Msg(
+ UserMsg(
name="user",
content="What is the capital of France?",
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of France is Paris.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Germany?",
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of Germany is Berlin.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Japan?",
- role="user",
),
]
self.msgs_tools = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ToolCallBlock(
@@ -77,30 +74,16 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
name="get_capital",
input='{"country": "Japan"}',
),
- ],
- role="assistant",
- ),
- Msg(
- name="tool",
- content=[
ToolResultBlock(
id="call_1",
name="get_capital",
output=[
- TextBlock(
- type="text",
- text="The capital of Japan is Tokyo.",
- ),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
state=ToolResultState.SUCCESS,
),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
- role="assistant",
- ),
- Msg(
- name="assistant",
- content="The capital of Japan is Tokyo.",
- role="assistant",
),
]
@@ -219,27 +202,12 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
"user: What is the capital of Japan?"
)
- # is_first=False: no wrapping (Anthropic only wraps on first group)
- _gt_trailing_asst_nonfirst = {
- "role": "user",
- "content": [
- {
- "type": "text",
- "text": "assistant: The capital of Japan is Tokyo.",
- },
- ],
- }
- # is_first=True: full wrapping with hist_prompt
- self._gt_trailing_asst_first = {
- "role": "user",
+ self._gt_trailing_asst = {
+ "role": "assistant",
"content": [
{
"type": "text",
- "text": (
- _hist_prompt + "<history>\n"
- "assistant: The capital of Japan is Tokyo.\n"
- "</history>"
- ),
+ "text": "The capital of Japan is Tokyo.",
},
],
}
@@ -294,7 +262,7 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
},
self._gt_tool_call,
self._gt_tool_result,
- _gt_trailing_asst_nonfirst,
+ self._gt_trailing_asst,
]
# -------------------------------------------------------------------
@@ -304,7 +272,6 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
async def test_chat_formatter(self) -> None:
"""Chat formatter produces exact output for various subsets."""
fmt = AnthropicChatFormatter()
- self.maxDiff = None
# Full history
res = await fmt.format(
@@ -317,15 +284,16 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
self.assertListEqual(self.gt_chat[1:], res)
# Without conversation
+ n_tools_gt = len(self.gt_chat) - 1 - len(self.msgs_conversation)
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
- [self.gt_chat[0]] + self.gt_chat[-len(self.msgs_tools) :],
+ [self.gt_chat[0]] + self.gt_chat[-n_tools_gt:],
res,
)
# Without tools
res = await fmt.format([*self.msgs_system, *self.msgs_conversation])
- self.assertListEqual(self.gt_chat[: -len(self.msgs_tools)], res)
+ self.assertListEqual(self.gt_chat[:-n_tools_gt], res)
# Empty
res = await fmt.format([])
@@ -335,19 +303,17 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
"""Base64-encoded image is formatted as Anthropic image source."""
fmt = AnthropicChatFormatter()
msgs = [
- Msg(
+ UserMsg(
name="user",
content=[
- TextBlock(type="text", text="What's in this image?"),
+ TextBlock(text="What's in this image?"),
DataBlock(
source=Base64Source(
- type="base64",
data=self.image_b64,
media_type="image/png",
),
),
],
- role="user",
),
]
res = await fmt.format(msgs)
@@ -375,23 +341,31 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
"""ThinkingBlock is passed back as a thinking content block."""
fmt = AnthropicChatFormatter()
msgs = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ThinkingBlock(thinking="inner thoughts"),
- TextBlock(type="text", text="reply"),
+ TextBlock(text="reply"),
],
- role="assistant",
),
]
res = await fmt.format(msgs)
- self.assertEqual(len(res), 1)
- thinking_blocks = [
- b for b in res[0]["content"] if b.get("type") == "thinking"
- ]
- self.assertEqual(len(thinking_blocks), 1)
- self.assertEqual(thinking_blocks[0]["thinking"], "inner thoughts")
- self.assertEqual(thinking_blocks[0]["signature"], "")
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "thinking",
+ "thinking": "inner thoughts",
+ "signature": "",
+ },
+ {"type": "text", "text": "reply"},
+ ],
+ },
+ ],
+ res,
+ )
async def test_chat_formatter_tool_result_role_forced_to_user(
self,
@@ -401,24 +375,22 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
res = await fmt.format(
[*self.msgs_system, *self.msgs_conversation, *self.msgs_tools],
)
- tool_results = [
- m
+ tool_result_roles = [
+ m["role"]
for m in res
if any(
b.get("type") == "tool_result"
for b in (m.get("content") or [])
)
]
- self.assertTrue(len(tool_results) > 0)
- for tr in tool_results:
- self.assertEqual(tr["role"], "user")
+ self.assertListEqual(tool_result_roles, ["user"])
async def test_chat_formatter_tool_result_with_image(self) -> None:
"""Tool result containing an image DataBlock inlines the image in the
tool_result content without crashing on TextBlock system-reminders."""
fmt = AnthropicChatFormatter()
msgs = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ToolCallBlock(
@@ -426,17 +398,11 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
name="get_chart",
input="{}",
),
- ],
- role="assistant",
- ),
- Msg(
- name="tool",
- content=[
ToolResultBlock(
id="call_img",
name="get_chart",
output=[
- TextBlock(type="text", text="Here is the chart."),
+ TextBlock(text="Here is the chart."),
DataBlock(
source=Base64Source(
data=self.image_b64,
@@ -446,32 +412,59 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
],
state=ToolResultState.SUCCESS,
),
+ TextBlock(text="Here is the chart analysis."),
],
- role="assistant",
),
]
res = await fmt.format(msgs)
- # There should be 2 messages: the tool_call and the tool_result
- self.assertEqual(len(res), 2)
- # Find the tool_result message
- tool_result_msg = next(
- m
- for m in res
- if any(
- b.get("type") == "tool_result"
- for b in (m.get("content") or [])
- )
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "tool_use",
+ "id": "call_img",
+ "name": "get_chart",
+ "input": {},
+ },
+ ],
+ },
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "tool_result",
+ "tool_use_id": "call_img",
+ "content": [
+ {
+ "type": "text",
+ "text": "Here is the chart.",
+ },
+ {
+ "type": "image",
+ "source": {
+ "type": "base64",
+ "media_type": "image/png",
+ "data": self.image_b64,
+ },
+ },
+ ],
+ },
+ ],
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "Here is the chart analysis.",
+ },
+ ],
+ },
+ ],
+ res,
)
- self.assertEqual(tool_result_msg["role"], "user")
- tr_content = next(
- b
- for b in tool_result_msg["content"]
- if b.get("type") == "tool_result"
- )["content"]
- # The content should have a text part and an image part
- types = [b.get("type") for b in tr_content]
- self.assertIn("text", types)
- self.assertIn("image", types)
# -------------------------------------------------------------------
# AnthropicMultiAgentFormatter tests
@@ -480,7 +473,6 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
async def test_multiagent_formatter(self) -> None:
"""MultiAgent formatter produces exact output for various subsets."""
fmt = AnthropicMultiAgentFormatter()
- self.maxDiff = None
# Full history
res = await fmt.format(
@@ -504,25 +496,25 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
res = await fmt.format(self.msgs_conversation)
self.assertListEqual([self.gt_multiagent[1]], res)
- # Tools only — is_first=True for trailing assistant
+ # Tools only
res = await fmt.format(self.msgs_tools)
self.assertListEqual(
[
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
- # System + tools (no conversation) — same is_first=True
+ # System + tools (no conversation)
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
[
self.gt_multiagent[0],
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
@@ -530,3 +522,219 @@ class TestAnthropicFormatter(IsolatedAsyncioTestCase):
# Empty
res = await fmt.format([])
self.assertListEqual([], res)
+
+ async def test_chat_formatter_complex_multi_step(self) -> None:
+ """Complex multi-step sequence with interleaved thinking, text,
+ tool calls, and tool results."""
+ fmt = AnthropicChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ThinkingBlock(thinking="thinking_1"),
+ TextBlock(text="text_1"),
+ ToolCallBlock(
+ id="call_1",
+ name="func_1",
+ input='{"arg": "value1"}',
+ ),
+ ToolCallBlock(
+ id="call_2",
+ name="func_2",
+ input='{"arg": "value2"}',
+ ),
+ ToolResultBlock(
+ id="call_1",
+ name="func_1",
+ output=[TextBlock(text="result_1")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolResultBlock(
+ id="call_2",
+ name="func_2",
+ output=[TextBlock(text="result_2")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_2"),
+ TextBlock(text="text_2"),
+ ToolCallBlock(
+ id="call_3",
+ name="func_3",
+ input='{"arg": "value3"}',
+ ),
+ ToolResultBlock(
+ id="call_3",
+ name="func_3",
+ output=[TextBlock(text="result_3")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolCallBlock(
+ id="call_4",
+ name="func_4",
+ input='{"arg": "value4"}',
+ ),
+ ToolResultBlock(
+ id="call_4",
+ name="func_4",
+ output=[TextBlock(text="result_4")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_3"),
+ TextBlock(text="text_3"),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "thinking",
+ "thinking": "thinking_1",
+ "signature": "",
+ },
+ {"type": "text", "text": "text_1"},
+ {
+ "type": "tool_use",
+ "id": "call_1",
+ "name": "func_1",
+ "input": {"arg": "value1"},
+ },
+ {
+ "type": "tool_use",
+ "id": "call_2",
+ "name": "func_2",
+ "input": {"arg": "value2"},
+ },
+ ],
+ },
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "tool_result",
+ "tool_use_id": "call_1",
+ "content": [
+ {"type": "text", "text": "result_1"},
+ ],
+ },
+ ],
+ },
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "tool_result",
+ "tool_use_id": "call_2",
+ "content": [
+ {"type": "text", "text": "result_2"},
+ ],
+ },
+ ],
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "thinking",
+ "thinking": "thinking_2",
+ "signature": "",
+ },
+ {"type": "text", "text": "text_2"},
+ {
+ "type": "tool_use",
+ "id": "call_3",
+ "name": "func_3",
+ "input": {"arg": "value3"},
+ },
+ ],
+ },
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "tool_result",
+ "tool_use_id": "call_3",
+ "content": [
+ {"type": "text", "text": "result_3"},
+ ],
+ },
+ ],
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "tool_use",
+ "id": "call_4",
+ "name": "func_4",
+ "input": {"arg": "value4"},
+ },
+ ],
+ },
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "tool_result",
+ "tool_use_id": "call_4",
+ "content": [
+ {"type": "text", "text": "result_4"},
+ ],
+ },
+ ],
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "thinking",
+ "thinking": "thinking_3",
+ "signature": "",
+ },
+ {"type": "text", "text": "text_3"},
+ ],
+ },
+ ],
+ res,
+ )
+
+ async def test_chat_formatter_hint_block(self) -> None:
+ """HintBlock flushes preceding content and becomes a user message."""
+ fmt = AnthropicChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ TextBlock(text="Let me think about that."),
+ HintBlock(hint="Remember to be concise."),
+ TextBlock(text="Here is my answer."),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": [
+ {"type": "text", "text": "Let me think about that."},
+ ],
+ },
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "Remember to be concise."},
+ ],
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {"type": "text", "text": "Here is my answer."},
+ ],
+ },
+ ],
+ res,
+ )
diff --git a/tests/formatter_dashscope_test.py b/tests/formatter_dashscope_test.py
index 870b79847..9e67fe866 100644
--- a/tests/formatter_dashscope_test.py
+++ b/tests/formatter_dashscope_test.py
@@ -6,23 +6,24 @@ ground-truth comparisons.
from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch
-import shortuuid
-
from agentscope.formatter import (
DashScopeChatFormatter,
DashScopeMultiAgentFormatter,
)
from agentscope.message import (
- Msg,
+ UserMsg,
+ AssistantMsg,
+ SystemMsg,
TextBlock,
DataBlock,
ToolCallBlock,
ToolResultBlock,
+ ToolResultState,
Base64Source,
URLSource,
ThinkingBlock,
+ HintBlock,
)
-from agentscope.message._block import ToolResultState
# A fixed short-uuid used to make promote-to-multimodal tests deterministic.
@@ -56,21 +57,17 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
# Message fixtures
# ---------------------------------------------------------------
self.msgs_system = [
- Msg(
+ SystemMsg(
name="system",
content="You're a helpful assistant.",
- role="system",
),
]
self.msgs_conversation = [
- Msg(
+ UserMsg(
name="user",
content=[
- TextBlock(
- type="text",
- text="What is the capital of France?",
- ),
+ TextBlock(text="What is the capital of France?"),
DataBlock(
source=URLSource(
url=self.image_url,
@@ -78,20 +75,15 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
),
),
],
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of France is Paris.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content=[
- TextBlock(
- type="text",
- text="What is the capital of Germany?",
- ),
+ TextBlock(text="What is the capital of Germany?"),
DataBlock(
source=URLSource(
url=self.audio_url,
@@ -99,24 +91,21 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
),
),
],
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of Germany is Berlin.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Japan?",
- role="user",
),
]
# Messages with ToolResultBlock must use role="assistant" because the
# system-role validator rejects non-text blocks.
self.msgs_tools = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ToolCallBlock(
@@ -124,30 +113,16 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
name="get_capital",
input='{"country": "Japan"}',
),
- ],
- role="assistant",
- ),
- Msg(
- name="tool",
- content=[
ToolResultBlock(
id="call_1",
name="get_capital",
output=[
- TextBlock(
- type="text",
- text="The capital of Japan is Tokyo.",
- ),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
state=ToolResultState.SUCCESS,
),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
- role="assistant",
- ),
- Msg(
- name="assistant",
- content="The capital of Japan is Tokyo.",
- role="assistant",
),
]
@@ -261,29 +236,12 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
DashScopeMultiAgentFormatter().conversation_history_prompt
)
- _gt_trailing_asst_nonfirst = {
- "role": "user",
- "content": [
- {
- "type": "text",
- "text": (
- "<history>\n"
- "assistant: The capital of Japan is Tokyo.\n"
- "</history>"
- ),
- },
- ],
- }
- self._gt_trailing_asst_first = {
- "role": "user",
+ self._gt_trailing_asst = {
+ "role": "assistant",
"content": [
{
"type": "text",
- "text": (
- _hist_prompt + "<history>\n"
- "assistant: The capital of Japan is Tokyo.\n"
- "</history>"
- ),
+ "text": "The capital of Japan is Tokyo.",
},
],
}
@@ -344,7 +302,7 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
},
self._gt_tool_call,
self._gt_tool_result,
- _gt_trailing_asst_nonfirst,
+ self._gt_trailing_asst,
]
# -------------------------------------------------------------------
@@ -359,7 +317,6 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
res = await fmt.format(
[*self.msgs_system, *self.msgs_conversation, *self.msgs_tools],
)
- self.maxDiff = None
self.assertListEqual(self.gt_chat, res)
# Without system
@@ -367,15 +324,16 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
self.assertListEqual(self.gt_chat[1:], res)
# Without conversation
+ n_tools_gt = len(self.gt_chat) - 1 - len(self.msgs_conversation)
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
- [self.gt_chat[0]] + self.gt_chat[-len(self.msgs_tools) :],
+ [self.gt_chat[0]] + self.gt_chat[-n_tools_gt:],
res,
)
# Without tools
res = await fmt.format([*self.msgs_system, *self.msgs_conversation])
- self.assertListEqual(self.gt_chat[: -len(self.msgs_tools)], res)
+ self.assertListEqual(self.gt_chat[:-n_tools_gt], res)
# Empty
res = await fmt.format([])
@@ -385,19 +343,17 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
"""Base64-encoded image is inlined as a data URI."""
fmt = DashScopeChatFormatter()
msgs = [
- Msg(
+ UserMsg(
name="user",
content=[
- TextBlock(type="text", text="What's in this image?"),
+ TextBlock(text="What's in this image?"),
DataBlock(
source=Base64Source(
- type="base64",
data=self.image_b64,
media_type="image/png",
),
),
],
- role="user",
),
]
res = await fmt.format(msgs)
@@ -417,51 +373,48 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
res,
)
- async def test_chat_formatter_url_image_in_tool_result(self) -> None:
+ @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.
"""
- with patch.object(shortuuid, "uuid", return_value=_FIXED_ID):
- fmt = DashScopeChatFormatter()
- msgs = [
- Msg(
- name="assistant",
- content=[
- ToolCallBlock(
- id="call_img",
- name="get_map",
- input='{"city": "Tokyo"}',
- ),
- ],
- role="assistant",
- ),
- Msg(
- name="tool",
- content=[
- ToolResultBlock(
- id="call_img",
- name="get_map",
- output=[
- TextBlock(
- type="text",
- text="Here is the map.",
- ),
- DataBlock(
- source=URLSource(
- url=self.image_url,
- media_type="image/png",
- ),
+ fmt = DashScopeChatFormatter()
+ 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,
- ),
- ],
- role="assistant",
- ),
- ]
- res = await fmt.format(msgs)
+ ),
+ ],
+ state=ToolResultState.SUCCESS,
+ ),
+ TextBlock(text="Here is the map of Tokyo."),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
expected_tool_content = (
"Here is the map.\n"
@@ -469,37 +422,89 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
f"and will be presented to you with the identifier "
f"[{_FIXED_ID}].</system-reminder>"
)
- self.assertEqual(len(res), 3)
- self.assertEqual(res[0]["role"], "assistant")
- self.assertEqual(res[1]["role"], "tool")
- self.assertEqual(res[1]["content"], expected_tool_content)
- # The promoted multimodal user message
- self.assertEqual(res[2]["role"], "user")
- image_blocks = [
- b
- for b in res[2]["content"]
- if b.get("type") == "image_url"
- and b.get("image_url", {}).get("url") == self.image_url
- ]
- self.assertEqual(len(image_blocks), 1)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": [
+ {
+ "id": "call_img",
+ "type": "function",
+ "function": {
+ "name": "get_map",
+ "arguments": '{"city": "Tokyo"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_img",
+ "content": expected_tool_content,
+ "name": "get_map",
+ },
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "text",
+ "text": (
+ "<system-reminder>The multimodal data "
+ "and their identifiers are listed as "
+ "follows:"
+ ),
+ },
+ {
+ "type": "text",
+ "text": f"- {_FIXED_ID} (image file): ",
+ },
+ {
+ "type": "image_url",
+ "image_url": {"url": self.image_url},
+ },
+ {
+ "type": "text",
+ "text": "</system-reminder>",
+ },
+ ],
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "Here is the map of Tokyo.",
+ },
+ ],
+ },
+ ],
+ res,
+ )
async def test_chat_formatter_thinking_dropped_without_flag(self) -> None:
"""ThinkingBlock is silently dropped when application/x-thinking is
absent from input_types."""
fmt = DashScopeChatFormatter()
msgs = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ThinkingBlock(thinking="inner thoughts"),
- TextBlock(type="text", text="reply"),
+ TextBlock(text="reply"),
],
- role="assistant",
),
]
res = await fmt.format(msgs)
- self.assertEqual(len(res), 1)
- self.assertNotIn("reasoning_content", res[0])
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": [{"type": "text", "text": "reply"}],
+ },
+ ],
+ res,
+ )
async def test_chat_formatter_thinking_becomes_reasoning_content(
self,
@@ -510,18 +515,25 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
input_types=["text/plain", "application/x-thinking"],
)
msgs = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ThinkingBlock(thinking="inner thoughts"),
- TextBlock(type="text", text="reply"),
+ TextBlock(text="reply"),
],
- role="assistant",
),
]
res = await fmt.format(msgs)
- self.assertEqual(len(res), 1)
- self.assertEqual(res[0]["reasoning_content"], "inner thoughts")
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": [{"type": "text", "text": "reply"}],
+ "reasoning_content": "inner thoughts",
+ },
+ ],
+ res,
+ )
async def test_chat_formatter_multiple_thinking_blocks_joined(
self,
@@ -532,19 +544,26 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
input_types=["text/plain", "application/x-thinking"],
)
msgs = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ThinkingBlock(thinking="part one"),
ThinkingBlock(thinking="part two"),
- TextBlock(type="text", text="answer"),
+ TextBlock(text="answer"),
],
- role="assistant",
),
]
res = await fmt.format(msgs)
- self.assertIn("part one", res[0]["reasoning_content"])
- self.assertIn("part two", res[0]["reasoning_content"])
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": [{"type": "text", "text": "answer"}],
+ "reasoning_content": "part one\npart two",
+ },
+ ],
+ res,
+ )
# -------------------------------------------------------------------
# DashScopeMultiAgentFormatter tests
@@ -553,7 +572,6 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
async def test_multiagent_formatter(self) -> None:
"""MultiAgent formatter produces exact output for various subsets."""
fmt = DashScopeMultiAgentFormatter()
- self.maxDiff = None
# Full history
res = await fmt.format(
@@ -584,20 +602,19 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
[
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
- # System + tools (no conversation) — same is_first=True for the
- # trailing assistant message.
+ # System + tools (no conversation)
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
[
self.gt_multiagent[0],
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
@@ -622,18 +639,355 @@ class TestDashScopeFormatter(IsolatedAsyncioTestCase):
tr = ToolResultBlock(
id="call_1",
name="get_capital",
- output=[TextBlock(type="text", text="Tokyo")],
+ output=[TextBlock(text="Tokyo")],
state=ToolResultState.SUCCESS,
)
msgs = [
- Msg(
+ AssistantMsg(
name="assistant",
- content=[ThinkingBlock(thinking="Need to check"), tc],
- role="assistant",
+ content=[ThinkingBlock(thinking="Need to check"), tc, tr],
),
- Msg(name="tool", content=[tr], role="assistant"),
]
res = await fmt.format(msgs)
asst_msgs = [m for m in res if m.get("role") == "assistant"]
- self.assertTrue(len(asst_msgs) > 0)
- self.assertEqual(asst_msgs[0]["reasoning_content"], "Need to check")
+ self.assertListEqual(
+ [m["reasoning_content"] for m in asst_msgs],
+ ["Need to check"],
+ )
+
+ async def test_chat_formatter_complex_multi_step(self) -> None:
+ """Complex multi-step sequence with interleaved thinking, text,
+ tool calls, and tool results (thinking dropped without flag)."""
+ fmt = DashScopeChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ThinkingBlock(thinking="thinking_1"),
+ TextBlock(text="text_1"),
+ ToolCallBlock(
+ id="call_1",
+ name="func_1",
+ input='{"arg": "value1"}',
+ ),
+ ToolCallBlock(
+ id="call_2",
+ name="func_2",
+ input='{"arg": "value2"}',
+ ),
+ ToolResultBlock(
+ id="call_1",
+ name="func_1",
+ output=[TextBlock(text="result_1")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolResultBlock(
+ id="call_2",
+ name="func_2",
+ output=[TextBlock(text="result_2")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_2"),
+ TextBlock(text="text_2"),
+ ToolCallBlock(
+ id="call_3",
+ name="func_3",
+ input='{"arg": "value3"}',
+ ),
+ ToolResultBlock(
+ id="call_3",
+ name="func_3",
+ output=[TextBlock(text="result_3")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolCallBlock(
+ id="call_4",
+ name="func_4",
+ input='{"arg": "value4"}',
+ ),
+ ToolResultBlock(
+ id="call_4",
+ name="func_4",
+ output=[TextBlock(text="result_4")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_3"),
+ TextBlock(text="text_3"),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": [{"type": "text", "text": "text_1"}],
+ "tool_calls": [
+ {
+ "id": "call_1",
+ "type": "function",
+ "function": {
+ "name": "func_1",
+ "arguments": '{"arg": "value1"}',
+ },
+ },
+ {
+ "id": "call_2",
+ "type": "function",
+ "function": {
+ "name": "func_2",
+ "arguments": '{"arg": "value2"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_1",
+ "content": "result_1",
+ "name": "func_1",
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_2",
+ "content": "result_2",
+ "name": "func_2",
+ },
+ {
+ "role": "assistant",
+ "content": [{"type": "text", "text": "text_2"}],
+ "tool_calls": [
+ {
+ "id": "call_3",
+ "type": "function",
+ "function": {
+ "name": "func_3",
+ "arguments": '{"arg": "value3"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_3",
+ "content": "result_3",
+ "name": "func_3",
+ },
+ {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": [
+ {
+ "id": "call_4",
+ "type": "function",
+ "function": {
+ "name": "func_4",
+ "arguments": '{"arg": "value4"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_4",
+ "content": "result_4",
+ "name": "func_4",
+ },
+ {
+ "role": "assistant",
+ "content": [{"type": "text", "text": "text_3"}],
+ },
+ ],
+ res,
+ )
+
+ async def test_chat_formatter_complex_multi_step_with_thinking(
+ self,
+ ) -> None:
+ """Complex multi-step sequence with thinking preserved via
+ application/x-thinking flag."""
+ fmt = DashScopeChatFormatter(
+ input_types=["text/plain", "application/x-thinking"],
+ )
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ThinkingBlock(thinking="thinking_1"),
+ TextBlock(text="text_1"),
+ ToolCallBlock(
+ id="call_1",
+ name="func_1",
+ input='{"arg": "value1"}',
+ ),
+ ToolCallBlock(
+ id="call_2",
+ name="func_2",
+ input='{"arg": "value2"}',
+ ),
+ ToolResultBlock(
+ id="call_1",
+ name="func_1",
+ output=[TextBlock(text="result_1")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolResultBlock(
+ id="call_2",
+ name="func_2",
+ output=[TextBlock(text="result_2")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_2"),
+ TextBlock(text="text_2"),
+ ToolCallBlock(
+ id="call_3",
+ name="func_3",
+ input='{"arg": "value3"}',
+ ),
+ ToolResultBlock(
+ id="call_3",
+ name="func_3",
+ output=[TextBlock(text="result_3")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolCallBlock(
+ id="call_4",
+ name="func_4",
+ input='{"arg": "value4"}',
+ ),
+ ToolResultBlock(
+ id="call_4",
+ name="func_4",
+ output=[TextBlock(text="result_4")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_3"),
+ TextBlock(text="text_3"),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": [{"type": "text", "text": "text_1"}],
+ "tool_calls": [
+ {
+ "id": "call_1",
+ "type": "function",
+ "function": {
+ "name": "func_1",
+ "arguments": '{"arg": "value1"}',
+ },
+ },
+ {
+ "id": "call_2",
+ "type": "function",
+ "function": {
+ "name": "func_2",
+ "arguments": '{"arg": "value2"}',
+ },
+ },
+ ],
+ "reasoning_content": "thinking_1",
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_1",
+ "content": "result_1",
+ "name": "func_1",
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_2",
+ "content": "result_2",
+ "name": "func_2",
+ },
+ {
+ "role": "assistant",
+ "content": [{"type": "text", "text": "text_2"}],
+ "tool_calls": [
+ {
+ "id": "call_3",
+ "type": "function",
+ "function": {
+ "name": "func_3",
+ "arguments": '{"arg": "value3"}',
+ },
+ },
+ ],
+ "reasoning_content": "thinking_2",
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_3",
+ "content": "result_3",
+ "name": "func_3",
+ },
+ {
+ "role": "assistant",
+ "content": None,
+ "tool_calls": [
+ {
+ "id": "call_4",
+ "type": "function",
+ "function": {
+ "name": "func_4",
+ "arguments": '{"arg": "value4"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_4",
+ "content": "result_4",
+ "name": "func_4",
+ },
+ {
+ "role": "assistant",
+ "content": [{"type": "text", "text": "text_3"}],
+ "reasoning_content": "thinking_3",
+ },
+ ],
+ res,
+ )
+
+ async def test_chat_formatter_hint_block(self) -> None:
+ """HintBlock flushes preceding content and becomes a user message."""
+ fmt = DashScopeChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ TextBlock(text="Let me think about that."),
+ HintBlock(hint="Remember to be concise."),
+ TextBlock(text="Here is my answer."),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": [
+ {"type": "text", "text": "Let me think about that."},
+ ],
+ },
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "Remember to be concise."},
+ ],
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {"type": "text", "text": "Here is my answer."},
+ ],
+ },
+ ],
+ res,
+ )
diff --git a/tests/formatter_deepseek_test.py b/tests/formatter_deepseek_test.py
index d4f697646..9a764cb35 100644
--- a/tests/formatter_deepseek_test.py
+++ b/tests/formatter_deepseek_test.py
@@ -9,13 +9,16 @@ from agentscope.formatter import (
DeepSeekMultiAgentFormatter,
)
from agentscope.message import (
- Msg,
+ UserMsg,
+ AssistantMsg,
+ SystemMsg,
TextBlock,
ToolCallBlock,
ToolResultBlock,
+ ToolResultState,
ThinkingBlock,
+ HintBlock,
)
-from agentscope.message._block import ToolResultState
class TestDeepSeekFormatter(IsolatedAsyncioTestCase):
@@ -28,41 +31,35 @@ class TestDeepSeekFormatter(IsolatedAsyncioTestCase):
)
self.msgs_system = [
- Msg(
+ SystemMsg(
name="system",
content="You're a helpful assistant.",
- role="system",
),
]
self.msgs_conversation = [
- Msg(
+ UserMsg(
name="user",
content="What is the capital of France?",
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of France is Paris.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Germany?",
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of Germany is Berlin.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Japan?",
- role="user",
),
]
self.msgs_tools = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ToolCallBlock(
@@ -70,30 +67,16 @@ class TestDeepSeekFormatter(IsolatedAsyncioTestCase):
name="get_capital",
input='{"country": "Japan"}',
),
- ],
- role="assistant",
- ),
- Msg(
- name="tool",
- content=[
ToolResultBlock(
id="call_1",
name="get_capital",
output=[
- TextBlock(
- type="text",
- text="The capital of Japan is Tokyo.",
- ),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
state=ToolResultState.SUCCESS,
),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
- role="assistant",
- ),
- Msg(
- name="assistant",
- content="The capital of Japan is Tokyo.",
- role="assistant",
),
]
@@ -149,21 +132,10 @@ class TestDeepSeekFormatter(IsolatedAsyncioTestCase):
# History is a plain string (not a list) with <history> tags.
# The trailing assistant message (is_first=False) is wrapped in a
# minimal <history> block without the full prompt prefix.
- _gt_trailing_asst_nonfirst = {
- "role": "user",
- "content": (
- "<history>\n"
- "assistant: The capital of Japan is Tokyo.\n"
- "</history>"
- ),
- }
- self._gt_trailing_asst_first = {
- "role": "user",
- "content": (
- _hist_prompt + "<history>\n"
- "assistant: The capital of Japan is Tokyo.\n"
- "</history>"
- ),
+ self._gt_trailing_asst = {
+ "role": "assistant",
+ "content": "The capital of Japan is Tokyo.",
+ "reasoning_content": "",
}
self._gt_tool_call = {
"role": "assistant",
@@ -203,7 +175,7 @@ class TestDeepSeekFormatter(IsolatedAsyncioTestCase):
},
self._gt_tool_call,
self._gt_tool_result,
- _gt_trailing_asst_nonfirst,
+ self._gt_trailing_asst,
]
# ------------------------------------------------------------------
@@ -213,7 +185,6 @@ class TestDeepSeekFormatter(IsolatedAsyncioTestCase):
async def test_chat_formatter(self) -> None:
"""Chat formatter produces exact output for various subsets."""
fmt = DeepSeekChatFormatter()
- self.maxDiff = None
# Full history
res = await fmt.format(
@@ -226,15 +197,16 @@ class TestDeepSeekFormatter(IsolatedAsyncioTestCase):
self.assertListEqual(self.gt_chat[1:], res)
# Without conversation
+ n_tools_gt = len(self.gt_chat) - 1 - len(self.msgs_conversation)
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
- [self.gt_chat[0]] + self.gt_chat[-len(self.msgs_tools) :],
+ [self.gt_chat[0]] + self.gt_chat[-n_tools_gt:],
res,
)
# Without tools
res = await fmt.format([*self.msgs_system, *self.msgs_conversation])
- self.assertListEqual(self.gt_chat[: -len(self.msgs_tools)], res)
+ self.assertListEqual(self.gt_chat[:-n_tools_gt], res)
# Empty
self.assertListEqual([], await fmt.format([]))
@@ -244,27 +216,42 @@ class TestDeepSeekFormatter(IsolatedAsyncioTestCase):
) -> None:
"""Every assistant message always has a reasoning_content field."""
fmt = DeepSeekChatFormatter()
- msgs = [Msg(name="assistant", content="Answer", role="assistant")]
+ msgs = [AssistantMsg(name="assistant", content="Answer")]
res = await fmt.format(msgs)
- self.assertIn("reasoning_content", res[0])
- self.assertEqual(res[0]["reasoning_content"], "")
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": "Answer",
+ "reasoning_content": "",
+ },
+ ],
+ res,
+ )
async def test_chat_formatter_thinking_block(self) -> None:
"""ThinkingBlock is placed into reasoning_content."""
fmt = DeepSeekChatFormatter()
msgs = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ThinkingBlock(thinking="Let me think..."),
- TextBlock(type="text", text="Answer"),
+ TextBlock(text="Answer"),
],
- role="assistant",
),
]
res = await fmt.format(msgs)
- self.assertEqual(res[0]["reasoning_content"], "Let me think...")
- self.assertEqual(res[0]["content"], "Answer")
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": "Answer",
+ "reasoning_content": "Let me think...",
+ },
+ ],
+ res,
+ )
# ------------------------------------------------------------------
# DeepSeekMultiAgentFormatter tests
@@ -273,7 +260,6 @@ class TestDeepSeekFormatter(IsolatedAsyncioTestCase):
async def test_multiagent_formatter(self) -> None:
"""MultiAgent formatter produces exact output for various subsets."""
fmt = DeepSeekMultiAgentFormatter()
- self.maxDiff = None
# Full
res = await fmt.format(
@@ -297,28 +283,212 @@ class TestDeepSeekFormatter(IsolatedAsyncioTestCase):
res = await fmt.format(self.msgs_conversation)
self.assertListEqual([self.gt_multiagent[1]], res)
- # Tools only (is_first=True for trailing assistant)
+ # Tools only
res = await fmt.format(self.msgs_tools)
self.assertListEqual(
[
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
- # System + tools (is_first=True for trailing assistant)
+ # System + tools
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
[
self.gt_multiagent[0],
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
# Empty
self.assertListEqual([], await fmt.format([]))
+
+ async def test_chat_formatter_complex_multi_step(self) -> None:
+ """Complex multi-step sequence with interleaved thinking, text,
+ tool calls, and tool results."""
+ fmt = DeepSeekChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ThinkingBlock(thinking="thinking_1"),
+ TextBlock(text="text_1"),
+ ToolCallBlock(
+ id="call_1",
+ name="func_1",
+ input='{"arg": "value1"}',
+ ),
+ ToolCallBlock(
+ id="call_2",
+ name="func_2",
+ input='{"arg": "value2"}',
+ ),
+ ToolResultBlock(
+ id="call_1",
+ name="func_1",
+ output=[TextBlock(text="result_1")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolResultBlock(
+ id="call_2",
+ name="func_2",
+ output=[TextBlock(text="result_2")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_2"),
+ TextBlock(text="text_2"),
+ ToolCallBlock(
+ id="call_3",
+ name="func_3",
+ input='{"arg": "value3"}',
+ ),
+ ToolResultBlock(
+ id="call_3",
+ name="func_3",
+ output=[TextBlock(text="result_3")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolCallBlock(
+ id="call_4",
+ name="func_4",
+ input='{"arg": "value4"}',
+ ),
+ ToolResultBlock(
+ id="call_4",
+ name="func_4",
+ output=[TextBlock(text="result_4")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_3"),
+ TextBlock(text="text_3"),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": "text_1",
+ "reasoning_content": "thinking_1",
+ "tool_calls": [
+ {
+ "id": "call_1",
+ "type": "function",
+ "function": {
+ "name": "func_1",
+ "arguments": '{"arg": "value1"}',
+ },
+ },
+ {
+ "id": "call_2",
+ "type": "function",
+ "function": {
+ "name": "func_2",
+ "arguments": '{"arg": "value2"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_1",
+ "content": "result_1",
+ "name": "func_1",
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_2",
+ "content": "result_2",
+ "name": "func_2",
+ },
+ {
+ "role": "assistant",
+ "content": "text_2",
+ "reasoning_content": "thinking_2",
+ "tool_calls": [
+ {
+ "id": "call_3",
+ "type": "function",
+ "function": {
+ "name": "func_3",
+ "arguments": '{"arg": "value3"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_3",
+ "content": "result_3",
+ "name": "func_3",
+ },
+ {
+ "role": "assistant",
+ "content": None,
+ "reasoning_content": "",
+ "tool_calls": [
+ {
+ "id": "call_4",
+ "type": "function",
+ "function": {
+ "name": "func_4",
+ "arguments": '{"arg": "value4"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_4",
+ "content": "result_4",
+ "name": "func_4",
+ },
+ {
+ "role": "assistant",
+ "content": "text_3",
+ "reasoning_content": "thinking_3",
+ },
+ ],
+ res,
+ )
+
+ async def test_chat_formatter_hint_block(self) -> None:
+ """HintBlock flushes preceding content and becomes a user message."""
+ fmt = DeepSeekChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ TextBlock(text="Let me think about that."),
+ HintBlock(hint="Remember to be concise."),
+ TextBlock(text="Here is my answer."),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": "Let me think about that.",
+ "reasoning_content": "",
+ },
+ {
+ "role": "user",
+ "content": "Remember to be concise.",
+ },
+ {
+ "role": "assistant",
+ "content": "Here is my answer.",
+ "reasoning_content": "",
+ },
+ ],
+ res,
+ )
diff --git a/tests/formatter_gemini_test.py b/tests/formatter_gemini_test.py
index d2162f0aa..76151eb64 100644
--- a/tests/formatter_gemini_test.py
+++ b/tests/formatter_gemini_test.py
@@ -4,21 +4,28 @@ 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,
GeminiMultiAgentFormatter,
)
from agentscope.message import (
- Msg,
+ UserMsg,
+ AssistantMsg,
+ SystemMsg,
TextBlock,
DataBlock,
ToolCallBlock,
ToolResultBlock,
+ ToolResultState,
Base64Source,
ThinkingBlock,
+ HintBlock,
)
-from agentscope.message._block import ToolResultState
+
+
+_FIXED_ID = "TESTID1234567"
class TestGeminiFormatter(IsolatedAsyncioTestCase):
@@ -26,62 +33,52 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):
async def asyncSetUp(self) -> None:
"""Set up shared message fixtures and expected ground-truth dicts."""
- image_b64 = "ZmFrZSBpbWFnZSBkYXRh"
+ self.image_b64 = "ZmFrZSBpbWFnZSBkYXRh"
# ---------------------------------------------------------------
# Message fixtures
# (Use base64 images: Gemini URL handling downloads from the network)
# ---------------------------------------------------------------
self.msgs_system = [
- Msg(
+ SystemMsg(
name="system",
content="You're a helpful assistant.",
- role="system",
),
]
self.msgs_conversation = [
- Msg(
+ UserMsg(
name="user",
content=[
- TextBlock(
- type="text",
- text="What is the capital of France?",
- ),
+ TextBlock(text="What is the capital of France?"),
DataBlock(
source=Base64Source(
- type="base64",
- data=image_b64,
+ data=self.image_b64,
media_type="image/png",
),
),
],
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of France is Paris.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Germany?",
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of Germany is Berlin.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Japan?",
- role="user",
),
]
self.msgs_tools = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ToolCallBlock(
@@ -89,35 +86,21 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):
name="get_capital",
input='{"country": "Japan"}',
),
- ],
- role="assistant",
- ),
- Msg(
- name="tool",
- content=[
ToolResultBlock(
id="call_1",
name="get_capital",
output=[
- TextBlock(
- type="text",
- text="The capital of Japan is Tokyo.",
- ),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
state=ToolResultState.SUCCESS,
),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
- role="assistant",
- ),
- Msg(
- name="assistant",
- content="The capital of Japan is Tokyo.",
- role="assistant",
),
]
_inline_img = {
- "inline_data": {"data": image_b64, "mime_type": "image/png"},
+ "inline_data": {"data": self.image_b64, "mime_type": "image/png"},
}
# ---------------------------------------------------------------
@@ -199,28 +182,10 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):
# ---------------------------------------------------------------
_hist_prompt = GeminiMultiAgentFormatter().conversation_history_prompt
- _gt_trailing_asst_nonfirst = {
- "role": "user",
+ self._gt_trailing_asst = {
+ "role": "model",
"parts": [
- {
- "text": (
- "<history>\n"
- "assistant: The capital of Japan is Tokyo.\n"
- "</history>"
- ),
- },
- ],
- }
- self._gt_trailing_asst_first = {
- "role": "user",
- "parts": [
- {
- "text": (
- _hist_prompt + "<history>\n"
- "assistant: The capital of Japan is Tokyo.\n"
- "</history>"
- ),
- },
+ {"text": "The capital of Japan is Tokyo."},
],
}
@@ -279,7 +244,7 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):
},
self._gt_tool_call,
self._gt_tool_result,
- _gt_trailing_asst_nonfirst,
+ self._gt_trailing_asst,
]
# -------------------------------------------------------------------
@@ -289,7 +254,6 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):
async def test_chat_formatter(self) -> None:
"""Chat formatter produces exact output for various subsets."""
fmt = GeminiChatFormatter()
- self.maxDiff = None
# Full history
res = await fmt.format(
@@ -302,15 +266,16 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):
self.assertListEqual(self.gt_chat[1:], res)
# Without conversation
+ n_tools_gt = len(self.gt_chat) - 1 - len(self.msgs_conversation)
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
- [self.gt_chat[0]] + self.gt_chat[-len(self.msgs_tools) :],
+ [self.gt_chat[0]] + self.gt_chat[-n_tools_gt:],
res,
)
# Without tools
res = await fmt.format([*self.msgs_system, *self.msgs_conversation])
- self.assertListEqual(self.gt_chat[: -len(self.msgs_tools)], res)
+ self.assertListEqual(self.gt_chat[:-n_tools_gt], res)
# Empty
res = await fmt.format([])
@@ -320,22 +285,131 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):
"""ThinkingBlock becomes a part with thought=True in Gemini format."""
fmt = GeminiChatFormatter()
msgs = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ThinkingBlock(thinking="inner thoughts"),
- TextBlock(type="text", text="reply"),
+ TextBlock(text="reply"),
],
- role="assistant",
),
]
res = await fmt.format(msgs)
- self.assertEqual(len(res), 1)
- thinking_parts = [
- p for p in res[0]["parts"] if p.get("thought") is True
+ self.assertListEqual(
+ [
+ {
+ "role": "model",
+ "parts": [
+ {"thought": True, "text": "inner thoughts"},
+ {"text": "reply"},
+ ],
+ },
+ ],
+ 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."""
+ fmt = GeminiChatFormatter()
+ 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=Base64Source(
+ data=self.image_b64,
+ media_type="image/png",
+ ),
+ ),
+ ],
+ state=ToolResultState.SUCCESS,
+ ),
+ TextBlock(text="Here is the map of Tokyo."),
+ ],
+ ),
]
- self.assertEqual(len(thinking_parts), 1)
- self.assertEqual(thinking_parts[0]["text"], "inner thoughts")
+ res = await fmt.format(msgs)
+
+ expected_tool_output = (
+ "Here is the map.\n"
+ f"<system-reminder>A(n) image file is returned "
+ f"and will be presented to you with the identifier "
+ f"[{_FIXED_ID}].</system-reminder>"
+ )
+ self.assertListEqual(
+ [
+ {
+ "role": "model",
+ "parts": [
+ {
+ "function_call": {
+ "id": "call_img",
+ "name": "get_map",
+ "args": {"city": "Tokyo"},
+ },
+ },
+ ],
+ },
+ {
+ "role": "user",
+ "parts": [
+ {
+ "function_response": {
+ "id": "call_img",
+ "name": "get_map",
+ "response": {"output": expected_tool_output},
+ },
+ },
+ ],
+ },
+ {
+ "role": "user",
+ "parts": [
+ {
+ "text": (
+ "<system-reminder>The multimodal data "
+ "and their identifiers are listed as "
+ "follows:"
+ ),
+ },
+ {
+ "text": f"- {_FIXED_ID} (image file): ",
+ },
+ {
+ "inline_data": {
+ "data": self.image_b64,
+ "mime_type": "image/png",
+ },
+ },
+ {"text": "</system-reminder>"},
+ ],
+ },
+ {
+ "role": "model",
+ "parts": [
+ {"text": "Here is the map of Tokyo."},
+ ],
+ },
+ ],
+ res,
+ )
# -------------------------------------------------------------------
# GeminiMultiAgentFormatter tests
@@ -344,7 +418,6 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):
async def test_multiagent_formatter(self) -> None:
"""MultiAgent formatter produces exact output for various subsets."""
fmt = GeminiMultiAgentFormatter()
- self.maxDiff = None
# Full history
res = await fmt.format(
@@ -368,25 +441,25 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):
res = await fmt.format(self.msgs_conversation)
self.assertListEqual([self.gt_multiagent[1]], res)
- # Tools only — is_first=True for trailing assistant
+ # Tools only
res = await fmt.format(self.msgs_tools)
self.assertListEqual(
[
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
- # System + tools — same is_first=True for trailing assistant
+ # System + tools
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
[
self.gt_multiagent[0],
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
@@ -394,3 +467,211 @@ class TestGeminiFormatter(IsolatedAsyncioTestCase):
# Empty
res = await fmt.format([])
self.assertListEqual([], res)
+
+ async def test_chat_formatter_complex_multi_step(self) -> None:
+ """Complex multi-step sequence with interleaved thinking, text,
+ tool calls, and tool results."""
+ fmt = GeminiChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ThinkingBlock(thinking="thinking_1"),
+ TextBlock(text="text_1"),
+ ToolCallBlock(
+ id="call_1",
+ name="func_1",
+ input='{"arg": "value1"}',
+ ),
+ ToolCallBlock(
+ id="call_2",
+ name="func_2",
+ input='{"arg": "value2"}',
+ ),
+ ToolResultBlock(
+ id="call_1",
+ name="func_1",
+ output=[TextBlock(text="result_1")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolResultBlock(
+ id="call_2",
+ name="func_2",
+ output=[TextBlock(text="result_2")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_2"),
+ TextBlock(text="text_2"),
+ ToolCallBlock(
+ id="call_3",
+ name="func_3",
+ input='{"arg": "value3"}',
+ ),
+ ToolResultBlock(
+ id="call_3",
+ name="func_3",
+ output=[TextBlock(text="result_3")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolCallBlock(
+ id="call_4",
+ name="func_4",
+ input='{"arg": "value4"}',
+ ),
+ ToolResultBlock(
+ id="call_4",
+ name="func_4",
+ output=[TextBlock(text="result_4")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_3"),
+ TextBlock(text="text_3"),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "model",
+ "parts": [
+ {"thought": True, "text": "thinking_1"},
+ {"text": "text_1"},
+ {
+ "function_call": {
+ "id": "call_1",
+ "name": "func_1",
+ "args": {"arg": "value1"},
+ },
+ },
+ {
+ "function_call": {
+ "id": "call_2",
+ "name": "func_2",
+ "args": {"arg": "value2"},
+ },
+ },
+ ],
+ },
+ {
+ "role": "user",
+ "parts": [
+ {
+ "function_response": {
+ "id": "call_1",
+ "name": "func_1",
+ "response": {"output": "result_1"},
+ },
+ },
+ ],
+ },
+ {
+ "role": "user",
+ "parts": [
+ {
+ "function_response": {
+ "id": "call_2",
+ "name": "func_2",
+ "response": {"output": "result_2"},
+ },
+ },
+ ],
+ },
+ {
+ "role": "model",
+ "parts": [
+ {"thought": True, "text": "thinking_2"},
+ {"text": "text_2"},
+ {
+ "function_call": {
+ "id": "call_3",
+ "name": "func_3",
+ "args": {"arg": "value3"},
+ },
+ },
+ ],
+ },
+ {
+ "role": "user",
+ "parts": [
+ {
+ "function_response": {
+ "id": "call_3",
+ "name": "func_3",
+ "response": {"output": "result_3"},
+ },
+ },
+ ],
+ },
+ {
+ "role": "model",
+ "parts": [
+ {
+ "function_call": {
+ "id": "call_4",
+ "name": "func_4",
+ "args": {"arg": "value4"},
+ },
+ },
+ ],
+ },
+ {
+ "role": "user",
+ "parts": [
+ {
+ "function_response": {
+ "id": "call_4",
+ "name": "func_4",
+ "response": {"output": "result_4"},
+ },
+ },
+ ],
+ },
+ {
+ "role": "model",
+ "parts": [
+ {"thought": True, "text": "thinking_3"},
+ {"text": "text_3"},
+ ],
+ },
+ ],
+ res,
+ )
+
+ async def test_chat_formatter_hint_block(self) -> None:
+ """HintBlock flushes preceding content and becomes a user message."""
+ fmt = GeminiChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ TextBlock(text="Let me think about that."),
+ HintBlock(hint="Remember to be concise."),
+ TextBlock(text="Here is my answer."),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "model",
+ "parts": [
+ {"text": "Let me think about that."},
+ ],
+ },
+ {
+ "role": "user",
+ "parts": [
+ {"text": "Remember to be concise."},
+ ],
+ },
+ {
+ "role": "model",
+ "parts": [
+ {"text": "Here is my answer."},
+ ],
+ },
+ ],
+ res,
+ )
diff --git a/tests/formatter_moonshot_test.py b/tests/formatter_moonshot_test.py
index 5e32aeb9c..d2237bf01 100644
--- a/tests/formatter_moonshot_test.py
+++ b/tests/formatter_moonshot_test.py
@@ -4,22 +4,26 @@ MoonshotMultiAgentFormatter, following the reference test style with exact
ground-truth comparisons.
"""
from unittest import IsolatedAsyncioTestCase
+from unittest.mock import patch
from agentscope.formatter import (
MoonshotChatFormatter,
MoonshotMultiAgentFormatter,
)
from agentscope.message import (
- Msg,
+ UserMsg,
+ AssistantMsg,
+ SystemMsg,
TextBlock,
DataBlock,
ToolCallBlock,
ToolResultBlock,
+ ToolResultState,
Base64Source,
URLSource,
ThinkingBlock,
+ HintBlock,
)
-from agentscope.message._block import ToolResultState
_FIXED_ID = "TESTID1234567"
@@ -34,7 +38,7 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
url="https://example.com/image.png",
media_type="image/png",
)
- image_url = str(_img_src.url)
+ self.image_url = str(_img_src.url)
self.image_b64 = "ZmFrZSBpbWFnZSBkYXRh"
self.image_data_uri = f"data:image/png;base64,{self.image_b64}"
@@ -43,54 +47,45 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
# Message fixtures (no audio to avoid downloads)
# ---------------------------------------------------------------
self.msgs_system = [
- Msg(
+ SystemMsg(
name="system",
content="You're a helpful assistant.",
- role="system",
),
]
self.msgs_conversation = [
- Msg(
+ UserMsg(
name="user",
content=[
- TextBlock(
- type="text",
- text="What is the capital of France?",
- ),
+ TextBlock(text="What is the capital of France?"),
DataBlock(
source=URLSource(
- url=image_url,
+ url=self.image_url,
media_type="image/png",
),
),
],
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of France is Paris.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Germany?",
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of Germany is Berlin.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Japan?",
- role="user",
),
]
self.msgs_tools = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ToolCallBlock(
@@ -98,30 +93,16 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
name="get_capital",
input='{"country": "Japan"}',
),
- ],
- role="assistant",
- ),
- Msg(
- name="tool",
- content=[
ToolResultBlock(
id="call_1",
name="get_capital",
output=[
- TextBlock(
- type="text",
- text="The capital of Japan is Tokyo.",
- ),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
state=ToolResultState.SUCCESS,
),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
- role="assistant",
- ),
- Msg(
- name="assistant",
- content="The capital of Japan is Tokyo.",
- role="assistant",
),
]
@@ -145,7 +126,7 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
{"type": "text", "text": "What is the capital of France?"},
{
"type": "image_url",
- "image_url": {"url": image_url},
+ "image_url": {"url": self.image_url},
},
],
},
@@ -237,31 +218,16 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
"user: What is the capital of Japan?"
)
- _gt_trailing_asst_nonfirst = {
- "role": "user",
- "content": [
- {
- "type": "text",
- "text": (
- "<history>\n"
- "assistant: The capital of Japan is Tokyo.\n"
- "</history>"
- ),
- },
- ],
- }
- self._gt_trailing_asst_first = {
- "role": "user",
+ self._gt_trailing_asst = {
+ "role": "assistant",
+ "name": "assistant",
"content": [
{
"type": "text",
- "text": (
- _hist_prompt + "<history>\n"
- "assistant: The capital of Japan is Tokyo.\n"
- "</history>"
- ),
+ "text": "The capital of Japan is Tokyo.",
},
],
+ "reasoning_content": "",
}
self._gt_tool_call = {
@@ -306,13 +272,13 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
},
{
"type": "image_url",
- "image_url": {"url": image_url},
+ "image_url": {"url": self.image_url},
},
],
},
self._gt_tool_call,
self._gt_tool_result,
- _gt_trailing_asst_nonfirst,
+ self._gt_trailing_asst,
]
# -------------------------------------------------------------------
@@ -322,7 +288,6 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
async def test_chat_formatter(self) -> None:
"""Chat formatter produces exact output for various subsets."""
fmt = MoonshotChatFormatter()
- self.maxDiff = None
# Full history
res = await fmt.format(
@@ -335,15 +300,16 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
self.assertListEqual(self.gt_chat[1:], res)
# Without conversation
+ n_tools_gt = len(self.gt_chat) - 1 - len(self.msgs_conversation)
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
- [self.gt_chat[0]] + self.gt_chat[-len(self.msgs_tools) :],
+ [self.gt_chat[0]] + self.gt_chat[-n_tools_gt:],
res,
)
# Without tools
res = await fmt.format([*self.msgs_system, *self.msgs_conversation])
- self.assertListEqual(self.gt_chat[: -len(self.msgs_tools)], res)
+ self.assertListEqual(self.gt_chat[:-n_tools_gt], res)
# Empty
res = await fmt.format([])
@@ -356,21 +322,25 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
Thinking)."""
fmt = MoonshotChatFormatter()
msgs = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ThinkingBlock(thinking="inner thoughts"),
- TextBlock(type="text", text="reply"),
+ TextBlock(text="reply"),
],
- role="assistant",
),
]
res = await fmt.format(msgs)
- self.assertEqual(len(res), 1)
- self.assertEqual(res[0]["reasoning_content"], "inner thoughts")
- self.assertEqual(
- res[0]["content"],
- [{"type": "text", "text": "reply"}],
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": [{"type": "text", "text": "reply"}],
+ "reasoning_content": "inner thoughts",
+ },
+ ],
+ res,
)
async def test_chat_formatter_assistant_always_has_reasoning_content(
@@ -380,34 +350,39 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
empty)."""
fmt = MoonshotChatFormatter()
msgs = [
- Msg(
+ AssistantMsg(
name="assistant",
content="Hello!",
- role="assistant",
),
]
res = await fmt.format(msgs)
- self.assertEqual(len(res), 1)
- self.assertIn("reasoning_content", res[0])
- self.assertEqual(res[0]["reasoning_content"], "")
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": [{"type": "text", "text": "Hello!"}],
+ "reasoning_content": "",
+ },
+ ],
+ res,
+ )
async def test_chat_formatter_base64_image(self) -> None:
"""Base64-encoded image is inlined as a data URI."""
fmt = MoonshotChatFormatter()
msgs = [
- Msg(
+ UserMsg(
name="user",
content=[
- TextBlock(type="text", text="What's in this image?"),
+ TextBlock(text="What's in this image?"),
DataBlock(
source=Base64Source(
- type="base64",
data=self.image_b64,
media_type="image/png",
),
),
],
- role="user",
),
]
res = await fmt.format(msgs)
@@ -428,6 +403,117 @@ 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."""
+ fmt = MoonshotChatFormatter()
+ 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."),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+
+ expected_tool_content = (
+ "Here is the map.\n"
+ f"<system-reminder>A(n) image file is returned "
+ f"and will be presented to you with the identifier "
+ f"[{_FIXED_ID}].</system-reminder>"
+ )
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": None,
+ "reasoning_content": "",
+ "tool_calls": [
+ {
+ "id": "call_img",
+ "type": "function",
+ "function": {
+ "name": "get_map",
+ "arguments": '{"city": "Tokyo"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_img",
+ "content": expected_tool_content,
+ "name": "get_map",
+ },
+ {
+ "role": "user",
+ "name": "system-reminder",
+ "content": [
+ {
+ "type": "text",
+ "text": (
+ "<system-reminder>The multimodal data "
+ "and their identifiers are listed as "
+ "follows:"
+ ),
+ },
+ {
+ "type": "text",
+ "text": f"- {_FIXED_ID} (image file): ",
+ },
+ {
+ "type": "image_url",
+ "image_url": {"url": self.image_url},
+ },
+ {
+ "type": "text",
+ "text": "</system-reminder>",
+ },
+ ],
+ },
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "Here is the map of Tokyo.",
+ },
+ ],
+ "reasoning_content": "",
+ },
+ ],
+ res,
+ )
+
# -------------------------------------------------------------------
# MoonshotMultiAgentFormatter tests
# -------------------------------------------------------------------
@@ -435,7 +521,6 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
async def test_multiagent_formatter(self) -> None:
"""MultiAgent formatter produces exact output for various subsets."""
fmt = MoonshotMultiAgentFormatter()
- self.maxDiff = None
# Full history
res = await fmt.format(
@@ -459,25 +544,25 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
res = await fmt.format(self.msgs_conversation)
self.assertListEqual([self.gt_multiagent[1]], res)
- # Tools only — is_first=True for trailing assistant
+ # Tools only
res = await fmt.format(self.msgs_tools)
self.assertListEqual(
[
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
- # System + tools — same is_first=True
+ # System + tools
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
[
self.gt_multiagent[0],
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
@@ -485,3 +570,199 @@ class TestMoonshotFormatter(IsolatedAsyncioTestCase):
# Empty
res = await fmt.format([])
self.assertListEqual([], res)
+
+ async def test_chat_formatter_complex_multi_step(self) -> None:
+ """Complex multi-step sequence with interleaved thinking, text,
+ tool calls, and tool results."""
+ fmt = MoonshotChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ThinkingBlock(thinking="thinking_1"),
+ TextBlock(text="text_1"),
+ ToolCallBlock(
+ id="call_1",
+ name="func_1",
+ input='{"arg": "value1"}',
+ ),
+ ToolCallBlock(
+ id="call_2",
+ name="func_2",
+ input='{"arg": "value2"}',
+ ),
+ ToolResultBlock(
+ id="call_1",
+ name="func_1",
+ output=[TextBlock(text="result_1")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolResultBlock(
+ id="call_2",
+ name="func_2",
+ output=[TextBlock(text="result_2")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_2"),
+ TextBlock(text="text_2"),
+ ToolCallBlock(
+ id="call_3",
+ name="func_3",
+ input='{"arg": "value3"}',
+ ),
+ ToolResultBlock(
+ id="call_3",
+ name="func_3",
+ output=[TextBlock(text="result_3")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolCallBlock(
+ id="call_4",
+ name="func_4",
+ input='{"arg": "value4"}',
+ ),
+ ToolResultBlock(
+ id="call_4",
+ name="func_4",
+ output=[TextBlock(text="result_4")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_3"),
+ TextBlock(text="text_3"),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": [{"type": "text", "text": "text_1"}],
+ "reasoning_content": "thinking_1",
+ "tool_calls": [
+ {
+ "id": "call_1",
+ "type": "function",
+ "function": {
+ "name": "func_1",
+ "arguments": '{"arg": "value1"}',
+ },
+ },
+ {
+ "id": "call_2",
+ "type": "function",
+ "function": {
+ "name": "func_2",
+ "arguments": '{"arg": "value2"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_1",
+ "content": "result_1",
+ "name": "func_1",
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_2",
+ "content": "result_2",
+ "name": "func_2",
+ },
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": [{"type": "text", "text": "text_2"}],
+ "reasoning_content": "thinking_2",
+ "tool_calls": [
+ {
+ "id": "call_3",
+ "type": "function",
+ "function": {
+ "name": "func_3",
+ "arguments": '{"arg": "value3"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_3",
+ "content": "result_3",
+ "name": "func_3",
+ },
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": None,
+ "reasoning_content": "",
+ "tool_calls": [
+ {
+ "id": "call_4",
+ "type": "function",
+ "function": {
+ "name": "func_4",
+ "arguments": '{"arg": "value4"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_4",
+ "content": "result_4",
+ "name": "func_4",
+ },
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": [{"type": "text", "text": "text_3"}],
+ "reasoning_content": "thinking_3",
+ },
+ ],
+ res,
+ )
+
+ async def test_chat_formatter_hint_block(self) -> None:
+ """HintBlock flushes preceding content and becomes a user message."""
+ fmt = MoonshotChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ TextBlock(text="Let me think about that."),
+ HintBlock(hint="Remember to be concise."),
+ TextBlock(text="Here is my answer."),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": [
+ {"type": "text", "text": "Let me think about that."},
+ ],
+ "reasoning_content": "",
+ },
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "Remember to be concise."},
+ ],
+ },
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": [
+ {"type": "text", "text": "Here is my answer."},
+ ],
+ "reasoning_content": "",
+ },
+ ],
+ res,
+ )
diff --git a/tests/formatter_ollama_test.py b/tests/formatter_ollama_test.py
index 9567ac09e..2b1e5294c 100644
--- a/tests/formatter_ollama_test.py
+++ b/tests/formatter_ollama_test.py
@@ -3,17 +3,25 @@
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 (
- Msg,
+ UserMsg,
+ AssistantMsg,
+ SystemMsg,
TextBlock,
DataBlock,
ToolCallBlock,
ToolResultBlock,
+ ToolResultState,
Base64Source,
+ ThinkingBlock,
+ HintBlock,
)
-from agentscope.message._block import ToolResultState
+
+
+_FIXED_ID = "TESTID1234567"
class TestOllamaFormatter(IsolatedAsyncioTestCase):
@@ -27,41 +35,35 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
self.image_b64 = "ZmFrZSBpbWFnZSBkYXRh"
self.msgs_system = [
- Msg(
+ SystemMsg(
name="system",
content="You're a helpful assistant.",
- role="system",
),
]
self.msgs_conversation = [
- Msg(
+ UserMsg(
name="user",
content="What is the capital of France?",
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of France is Paris.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Germany?",
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of Germany is Berlin.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Japan?",
- role="user",
),
]
self.msgs_tools = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ToolCallBlock(
@@ -69,30 +71,16 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
name="get_capital",
input='{"country": "Japan"}',
),
- ],
- role="assistant",
- ),
- Msg(
- name="tool",
- content=[
ToolResultBlock(
id="call_1",
name="get_capital",
output=[
- TextBlock(
- type="text",
- text="The capital of Japan is Tokyo.",
- ),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
state=ToolResultState.SUCCESS,
),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
- role="assistant",
- ),
- Msg(
- name="assistant",
- content="The capital of Japan is Tokyo.",
- role="assistant",
),
]
@@ -140,17 +128,9 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
"assistant:\nThe capital of Germany is Berlin.\n"
"user:\nWhat is the capital of Japan?"
)
- _gt_trailing_asst_nonfirst = {
- "role": "user",
- "content": "assistant:\nThe capital of Japan is Tokyo.",
- }
- self._gt_trailing_asst_first = {
- "role": "user",
- "content": (
- _hist_prompt + "<history>\n"
- "assistant:\nThe capital of Japan is Tokyo.\n"
- "</history>"
- ),
+ self._gt_trailing_asst = {
+ "role": "assistant",
+ "content": "The capital of Japan is Tokyo.",
}
self._gt_tool_call = {
"role": "assistant",
@@ -179,7 +159,7 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
},
self._gt_tool_call,
self._gt_tool_result,
- _gt_trailing_asst_nonfirst,
+ self._gt_trailing_asst,
]
# ------------------------------------------------------------------
@@ -189,7 +169,6 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
async def test_chat_formatter(self) -> None:
"""Chat formatter produces exact output for various subsets."""
fmt = OllamaChatFormatter()
- self.maxDiff = None
# Full history
res = await fmt.format(
@@ -202,15 +181,16 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
self.assertListEqual(self.gt_chat[1:], res)
# Without conversation
+ n_tools_gt = len(self.gt_chat) - 1 - len(self.msgs_conversation)
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
- [self.gt_chat[0]] + self.gt_chat[-len(self.msgs_tools) :],
+ [self.gt_chat[0]] + self.gt_chat[-n_tools_gt:],
res,
)
# Without tools
res = await fmt.format([*self.msgs_system, *self.msgs_conversation])
- self.assertListEqual(self.gt_chat[: -len(self.msgs_tools)], res)
+ self.assertListEqual(self.gt_chat[:-n_tools_gt], res)
# Empty
self.assertListEqual([], await fmt.format([]))
@@ -220,36 +200,137 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
fmt = OllamaChatFormatter()
tc = ToolCallBlock(id="c1", name="search", input='{"q": "weather"}')
res = await fmt.format(
- [Msg(name="assistant", content=[tc], role="assistant")],
+ [AssistantMsg(name="assistant", content=[tc])],
+ )
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [
+ {
+ "function": {
+ "name": "search",
+ "arguments": {"q": "weather"},
+ },
+ },
+ ],
+ },
+ ],
+ res,
)
- args = res[0]["tool_calls"][0]["function"]["arguments"]
- self.assertIsInstance(args, dict)
- self.assertEqual(args["q"], "weather")
async def test_chat_formatter_base64_image(self) -> None:
"""Base64 image is placed in the 'images' list as a raw base64
string."""
fmt = OllamaChatFormatter()
msgs = [
- Msg(
+ UserMsg(
name="user",
content=[
- TextBlock(type="text", text="What is this?"),
+ TextBlock(text="What is this?"),
DataBlock(
source=Base64Source(
- type="base64",
data=self.image_b64,
media_type="image/png",
),
),
],
- role="user",
),
]
res = await fmt.format(msgs)
- self.assertEqual(len(res), 1)
- self.assertEqual(res[0]["content"], "What is this?")
- self.assertEqual(res[0]["images"], [self.image_b64])
+ self.assertListEqual(
+ [
+ {
+ "role": "user",
+ "content": "What is this?",
+ "images": [self.image_b64],
+ },
+ ],
+ 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."""
+ fmt = OllamaChatFormatter()
+ 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=Base64Source(
+ data=self.image_b64,
+ media_type="image/png",
+ ),
+ ),
+ ],
+ state=ToolResultState.SUCCESS,
+ ),
+ TextBlock(text="Here is the map of Tokyo."),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+
+ expected_tool_content = (
+ "Here is the map.\n"
+ f"<system-reminder>A(n) image file is returned "
+ f"and will be presented to you with the identifier "
+ f"[{_FIXED_ID}].</system-reminder>"
+ )
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [
+ {
+ "function": {
+ "name": "get_map",
+ "arguments": {"city": "Tokyo"},
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "content": expected_tool_content,
+ },
+ {
+ "role": "user",
+ "content": (
+ "<system-reminder>The multimodal data "
+ "and their identifiers are listed as follows:\n"
+ f"- {_FIXED_ID} (image file): \n"
+ "</system-reminder>"
+ ),
+ "images": [self.image_b64],
+ },
+ {
+ "role": "assistant",
+ "content": "Here is the map of Tokyo.",
+ },
+ ],
+ res,
+ )
# ------------------------------------------------------------------
# OllamaMultiAgentFormatter tests
@@ -258,7 +339,6 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
async def test_multiagent_formatter(self) -> None:
"""MultiAgent formatter produces exact output for various subsets."""
fmt = OllamaMultiAgentFormatter()
- self.maxDiff = None
# Full
res = await fmt.format(
@@ -282,28 +362,181 @@ class TestOllamaFormatter(IsolatedAsyncioTestCase):
res = await fmt.format(self.msgs_conversation)
self.assertListEqual([self.gt_multiagent[1]], res)
- # Tools only (is_first=True for trailing assistant)
+ # Tools only
res = await fmt.format(self.msgs_tools)
self.assertListEqual(
[
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
- # System + tools (is_first=True for trailing assistant)
+ # System + tools
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
[
self.gt_multiagent[0],
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
# Empty
self.assertListEqual([], await fmt.format([]))
+
+ async def test_chat_formatter_complex_multi_step(self) -> None:
+ """Complex multi-step sequence with interleaved thinking, text,
+ tool calls, and tool results."""
+ fmt = OllamaChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ThinkingBlock(thinking="thinking_1"),
+ TextBlock(text="text_1"),
+ ToolCallBlock(
+ id="call_1",
+ name="func_1",
+ input='{"arg": "value1"}',
+ ),
+ ToolCallBlock(
+ id="call_2",
+ name="func_2",
+ input='{"arg": "value2"}',
+ ),
+ ToolResultBlock(
+ id="call_1",
+ name="func_1",
+ output=[TextBlock(text="result_1")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolResultBlock(
+ id="call_2",
+ name="func_2",
+ output=[TextBlock(text="result_2")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_2"),
+ TextBlock(text="text_2"),
+ ToolCallBlock(
+ id="call_3",
+ name="func_3",
+ input='{"arg": "value3"}',
+ ),
+ ToolResultBlock(
+ id="call_3",
+ name="func_3",
+ output=[TextBlock(text="result_3")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolCallBlock(
+ id="call_4",
+ name="func_4",
+ input='{"arg": "value4"}',
+ ),
+ ToolResultBlock(
+ id="call_4",
+ name="func_4",
+ output=[TextBlock(text="result_4")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_3"),
+ TextBlock(text="text_3"),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": "text_1",
+ "tool_calls": [
+ {
+ "function": {
+ "name": "func_1",
+ "arguments": {"arg": "value1"},
+ },
+ },
+ ],
+ },
+ {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [
+ {
+ "function": {
+ "name": "func_2",
+ "arguments": {"arg": "value2"},
+ },
+ },
+ ],
+ },
+ {"role": "tool", "content": "result_1"},
+ {"role": "tool", "content": "result_2"},
+ {
+ "role": "assistant",
+ "content": "text_2",
+ "tool_calls": [
+ {
+ "function": {
+ "name": "func_3",
+ "arguments": {"arg": "value3"},
+ },
+ },
+ ],
+ },
+ {"role": "tool", "content": "result_3"},
+ {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [
+ {
+ "function": {
+ "name": "func_4",
+ "arguments": {"arg": "value4"},
+ },
+ },
+ ],
+ },
+ {"role": "tool", "content": "result_4"},
+ {"role": "assistant", "content": "text_3"},
+ ],
+ res,
+ )
+
+ async def test_chat_formatter_hint_block(self) -> None:
+ """HintBlock flushes preceding content and becomes a user message."""
+ fmt = OllamaChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ TextBlock(text="Let me think about that."),
+ HintBlock(hint="Remember to be concise."),
+ TextBlock(text="Here is my answer."),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": "Let me think about that.",
+ },
+ {
+ "role": "user",
+ "content": "Remember to be concise.",
+ },
+ {
+ "role": "assistant",
+ "content": "Here is my answer.",
+ },
+ ],
+ res,
+ )
diff --git a/tests/formatter_openai_chat_test.py b/tests/formatter_openai_chat_test.py
index 266a540e9..6b178a944 100644
--- a/tests/formatter_openai_chat_test.py
+++ b/tests/formatter_openai_chat_test.py
@@ -6,23 +6,24 @@ ground-truth comparisons.
from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch
-import shortuuid
-
from agentscope.formatter import (
OpenAIChatFormatter,
OpenAIMultiAgentFormatter,
)
from agentscope.message import (
- Msg,
+ UserMsg,
+ AssistantMsg,
+ SystemMsg,
TextBlock,
DataBlock,
ToolCallBlock,
ToolResultBlock,
+ ToolResultState,
Base64Source,
URLSource,
ThinkingBlock,
+ HintBlock,
)
-from agentscope.message._block import ToolResultState
_FIXED_ID = "TESTID1234567"
@@ -47,21 +48,17 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
# (No audio in conversation: OpenAI URL audio requires a download)
# ---------------------------------------------------------------
self.msgs_system = [
- Msg(
+ SystemMsg(
name="system",
content="You're a helpful assistant.",
- role="system",
),
]
self.msgs_conversation = [
- Msg(
+ UserMsg(
name="user",
content=[
- TextBlock(
- type="text",
- text="What is the capital of France?",
- ),
+ TextBlock(text="What is the capital of France?"),
DataBlock(
source=URLSource(
url=self.image_url,
@@ -69,32 +66,27 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
),
),
],
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of France is Paris.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Germany?",
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of Germany is Berlin.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Japan?",
- role="user",
),
]
self.msgs_tools = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ToolCallBlock(
@@ -102,30 +94,16 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
name="get_capital",
input='{"country": "Japan"}',
),
- ],
- role="assistant",
- ),
- Msg(
- name="tool",
- content=[
ToolResultBlock(
id="call_1",
name="get_capital",
output=[
- TextBlock(
- type="text",
- text="The capital of Japan is Tokyo.",
- ),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
state=ToolResultState.SUCCESS,
),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
- role="assistant",
- ),
- Msg(
- name="assistant",
- content="The capital of Japan is Tokyo.",
- role="assistant",
),
]
@@ -241,29 +219,13 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
"user: What is the capital of Japan?"
)
- _gt_trailing_asst_nonfirst = {
- "role": "user",
- "content": [
- {
- "type": "text",
- "text": (
- "<history>\n"
- "assistant: The capital of Japan is Tokyo.\n"
- "</history>"
- ),
- },
- ],
- }
- self._gt_trailing_asst_first = {
- "role": "user",
+ self._gt_trailing_asst = {
+ "role": "assistant",
+ "name": "assistant",
"content": [
{
"type": "text",
- "text": (
- _hist_prompt + "<history>\n"
- "assistant: The capital of Japan is Tokyo.\n"
- "</history>"
- ),
+ "text": "The capital of Japan is Tokyo.",
},
],
}
@@ -315,7 +277,7 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
},
self._gt_tool_call,
self._gt_tool_result,
- _gt_trailing_asst_nonfirst,
+ self._gt_trailing_asst,
]
# -------------------------------------------------------------------
@@ -325,7 +287,6 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
async def test_chat_formatter(self) -> None:
"""Chat formatter produces exact output for various subsets."""
fmt = OpenAIChatFormatter()
- self.maxDiff = None
# Full history
res = await fmt.format(
@@ -338,15 +299,16 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
self.assertListEqual(self.gt_chat[1:], res)
# Without conversation
+ n_tools_gt = len(self.gt_chat) - 1 - len(self.msgs_conversation)
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
- [self.gt_chat[0]] + self.gt_chat[-len(self.msgs_tools) :],
+ [self.gt_chat[0]] + self.gt_chat[-n_tools_gt:],
res,
)
# Without tools
res = await fmt.format([*self.msgs_system, *self.msgs_conversation])
- self.assertListEqual(self.gt_chat[: -len(self.msgs_tools)], res)
+ self.assertListEqual(self.gt_chat[:-n_tools_gt], res)
# Empty
res = await fmt.format([])
@@ -356,19 +318,17 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
"""Base64-encoded image is inlined as a data URI."""
fmt = OpenAIChatFormatter()
msgs = [
- Msg(
+ UserMsg(
name="user",
content=[
- TextBlock(type="text", text="What's in this image?"),
+ TextBlock(text="What's in this image?"),
DataBlock(
source=Base64Source(
- type="base64",
data=self.image_b64,
media_type="image/png",
),
),
],
- role="user",
),
]
res = await fmt.format(msgs)
@@ -393,65 +353,65 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
"""ThinkingBlock is silently dropped by OpenAI formatter."""
fmt = OpenAIChatFormatter()
msgs = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ThinkingBlock(thinking="inner thoughts"),
- TextBlock(type="text", text="reply"),
+ TextBlock(text="reply"),
],
- role="assistant",
),
]
res = await fmt.format(msgs)
- self.assertEqual(len(res), 1)
- self.assertNotIn("reasoning_content", res[0])
- self.assertEqual(
- res[0]["content"],
- [{"type": "text", "text": "reply"}],
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": [{"type": "text", "text": "reply"}],
+ },
+ ],
+ res,
)
- async def test_chat_formatter_url_image_in_tool_result(self) -> None:
+ @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."""
- with patch.object(shortuuid, "uuid", return_value=_FIXED_ID):
- fmt = OpenAIChatFormatter()
- msgs = [
- Msg(
- name="assistant",
- content=[
- ToolCallBlock(
- id="call_img",
- name="get_map",
- input='{"city": "Tokyo"}',
- ),
- ],
- role="assistant",
- ),
- Msg(
- name="tool",
- content=[
- ToolResultBlock(
- id="call_img",
- name="get_map",
- output=[
- TextBlock(
- type="text",
- text="Here is the map.",
- ),
- DataBlock(
- source=URLSource(
- url=self.image_url,
- media_type="image/png",
- ),
+ 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,
- ),
- ],
- role="assistant",
- ),
- ]
- res = await fmt.format(msgs)
+ ),
+ ],
+ state=ToolResultState.SUCCESS,
+ ),
+ TextBlock(text="Here is the map of Tokyo."),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
expected_tool_content = (
"Here is the map.\n"
@@ -459,19 +419,68 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
f"and will be presented to you with the identifier "
f"[{_FIXED_ID}].</system-reminder>"
)
- self.assertEqual(len(res), 3)
- self.assertEqual(res[0]["role"], "assistant")
- self.assertEqual(res[1]["role"], "tool")
- self.assertEqual(res[1]["content"], expected_tool_content)
- # The promoted multimodal user message
- self.assertEqual(res[2]["role"], "user")
- image_blocks = [
- b
- for b in res[2]["content"]
- if b.get("type") == "image_url"
- and b["image_url"]["url"] == self.image_url
- ]
- self.assertEqual(len(image_blocks), 1)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": None,
+ "tool_calls": [
+ {
+ "id": "call_img",
+ "type": "function",
+ "function": {
+ "name": "get_map",
+ "arguments": '{"city": "Tokyo"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_img",
+ "content": expected_tool_content,
+ "name": "get_map",
+ },
+ {
+ "role": "user",
+ "name": "system-reminder",
+ "content": [
+ {
+ "type": "text",
+ "text": (
+ "<system-reminder>The multimodal data "
+ "and their identifiers are listed as "
+ "follows:"
+ ),
+ },
+ {
+ "type": "text",
+ "text": f"- {_FIXED_ID} (image file): ",
+ },
+ {
+ "type": "image_url",
+ "image_url": {"url": self.image_url},
+ },
+ {
+ "type": "text",
+ "text": "</system-reminder>",
+ },
+ ],
+ },
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": [
+ {
+ "type": "text",
+ "text": "Here is the map of Tokyo.",
+ },
+ ],
+ },
+ ],
+ res,
+ )
# -------------------------------------------------------------------
# OpenAIMultiAgentFormatter tests
@@ -480,7 +489,6 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
async def test_multiagent_formatter(self) -> None:
"""MultiAgent formatter produces exact output for various subsets."""
fmt = OpenAIMultiAgentFormatter()
- self.maxDiff = None
# Full history
res = await fmt.format(
@@ -504,25 +512,25 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
res = await fmt.format(self.msgs_conversation)
self.assertListEqual([self.gt_multiagent[1]], res)
- # Tools only — is_first=True for the trailing assistant message
+ # Tools only
res = await fmt.format(self.msgs_tools)
self.assertListEqual(
[
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
- # System + tools (no conversation) — same is_first=True
+ # System + tools (no conversation)
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
[
self.gt_multiagent[0],
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
@@ -530,3 +538,193 @@ class TestOpenAIFormatter(IsolatedAsyncioTestCase):
# Empty
res = await fmt.format([])
self.assertListEqual([], res)
+
+ async def test_chat_formatter_complex_multi_step(self) -> None:
+ """Complex multi-step sequence with interleaved thinking, text,
+ tool calls, and tool results."""
+ fmt = OpenAIChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ThinkingBlock(thinking="thinking_1"),
+ TextBlock(text="text_1"),
+ ToolCallBlock(
+ id="call_1",
+ name="func_1",
+ input='{"arg": "value1"}',
+ ),
+ ToolCallBlock(
+ id="call_2",
+ name="func_2",
+ input='{"arg": "value2"}',
+ ),
+ ToolResultBlock(
+ id="call_1",
+ name="func_1",
+ output=[TextBlock(text="result_1")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolResultBlock(
+ id="call_2",
+ name="func_2",
+ output=[TextBlock(text="result_2")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_2"),
+ TextBlock(text="text_2"),
+ ToolCallBlock(
+ id="call_3",
+ name="func_3",
+ input='{"arg": "value3"}',
+ ),
+ ToolResultBlock(
+ id="call_3",
+ name="func_3",
+ output=[TextBlock(text="result_3")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolCallBlock(
+ id="call_4",
+ name="func_4",
+ input='{"arg": "value4"}',
+ ),
+ ToolResultBlock(
+ id="call_4",
+ name="func_4",
+ output=[TextBlock(text="result_4")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_3"),
+ TextBlock(text="text_3"),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": [{"type": "text", "text": "text_1"}],
+ "tool_calls": [
+ {
+ "id": "call_1",
+ "type": "function",
+ "function": {
+ "name": "func_1",
+ "arguments": '{"arg": "value1"}',
+ },
+ },
+ {
+ "id": "call_2",
+ "type": "function",
+ "function": {
+ "name": "func_2",
+ "arguments": '{"arg": "value2"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_1",
+ "content": "result_1",
+ "name": "func_1",
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_2",
+ "content": "result_2",
+ "name": "func_2",
+ },
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": [{"type": "text", "text": "text_2"}],
+ "tool_calls": [
+ {
+ "id": "call_3",
+ "type": "function",
+ "function": {
+ "name": "func_3",
+ "arguments": '{"arg": "value3"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_3",
+ "content": "result_3",
+ "name": "func_3",
+ },
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": None,
+ "tool_calls": [
+ {
+ "id": "call_4",
+ "type": "function",
+ "function": {
+ "name": "func_4",
+ "arguments": '{"arg": "value4"}',
+ },
+ },
+ ],
+ },
+ {
+ "role": "tool",
+ "tool_call_id": "call_4",
+ "content": "result_4",
+ "name": "func_4",
+ },
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": [{"type": "text", "text": "text_3"}],
+ },
+ ],
+ res,
+ )
+
+ async def test_chat_formatter_hint_block(self) -> None:
+ """HintBlock flushes preceding content and becomes a user message."""
+ fmt = OpenAIChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ TextBlock(text="Let me think about that."),
+ HintBlock(hint="Remember to be concise."),
+ TextBlock(text="Here is my answer."),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": [
+ {"type": "text", "text": "Let me think about that."},
+ ],
+ },
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "Remember to be concise."},
+ ],
+ },
+ {
+ "role": "assistant",
+ "name": "assistant",
+ "content": [
+ {"type": "text", "text": "Here is my answer."},
+ ],
+ },
+ ],
+ res,
+ )
diff --git a/tests/formatter_openai_response_test.py b/tests/formatter_openai_response_test.py
index c7c51f1f7..be6603cb1 100644
--- a/tests/formatter_openai_response_test.py
+++ b/tests/formatter_openai_response_test.py
@@ -13,23 +13,24 @@ Key differences from OpenAI Chat formatter:
from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch
-import shortuuid
-
from agentscope.formatter import (
OpenAIResponseFormatter,
OpenAIResponseMultiAgentFormatter,
)
from agentscope.message import (
- Msg,
+ UserMsg,
+ AssistantMsg,
+ SystemMsg,
TextBlock,
DataBlock,
ToolCallBlock,
ToolResultBlock,
+ ToolResultState,
Base64Source,
URLSource,
ThinkingBlock,
+ HintBlock,
)
-from agentscope.message._block import ToolResultState
_FIXED_ID = "TESTID1234567"
@@ -53,21 +54,17 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
# Message fixtures (no audio to avoid downloads)
# ---------------------------------------------------------------
self.msgs_system = [
- Msg(
+ SystemMsg(
name="system",
content="You're a helpful assistant.",
- role="system",
),
]
self.msgs_conversation = [
- Msg(
+ UserMsg(
name="user",
content=[
- TextBlock(
- type="text",
- text="What is the capital of France?",
- ),
+ TextBlock(text="What is the capital of France?"),
DataBlock(
source=URLSource(
url=self.image_url,
@@ -75,32 +72,27 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
),
),
],
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of France is Paris.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Germany?",
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of Germany is Berlin.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Japan?",
- role="user",
),
]
self.msgs_tools = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ToolCallBlock(
@@ -108,30 +100,16 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
name="get_capital",
input='{"country": "Japan"}',
),
- ],
- role="assistant",
- ),
- Msg(
- name="tool",
- content=[
ToolResultBlock(
id="call_1",
name="get_capital",
output=[
- TextBlock(
- type="text",
- text="The capital of Japan is Tokyo.",
- ),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
state=ToolResultState.SUCCESS,
),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
- role="assistant",
- ),
- Msg(
- name="assistant",
- content="The capital of Japan is Tokyo.",
- role="assistant",
),
]
@@ -241,29 +219,12 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
"user: What is the capital of Japan?"
)
- _gt_trailing_asst_nonfirst = {
- "role": "user",
+ self._gt_trailing_asst = {
+ "role": "assistant",
"content": [
{
"type": "input_text",
- "text": (
- "<history>\n"
- "assistant: The capital of Japan is Tokyo.\n"
- "</history>"
- ),
- },
- ],
- }
- self._gt_trailing_asst_first = {
- "role": "user",
- "content": [
- {
- "type": "input_text",
- "text": (
- _hist_prompt + "<history>\n"
- "assistant: The capital of Japan is Tokyo.\n"
- "</history>"
- ),
+ "text": "The capital of Japan is Tokyo.",
},
],
}
@@ -303,7 +264,7 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
},
self._gt_tool_call,
self._gt_tool_result,
- _gt_trailing_asst_nonfirst,
+ self._gt_trailing_asst,
]
# -------------------------------------------------------------------
@@ -313,7 +274,6 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
async def test_chat_formatter(self) -> None:
"""Chat formatter produces exact output for various subsets."""
fmt = OpenAIResponseFormatter()
- self.maxDiff = None
# Full history
res = await fmt.format(
@@ -326,15 +286,16 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
self.assertListEqual(self.gt_chat[1:], res)
# Without conversation
+ n_tools_gt = len(self.gt_chat) - 1 - len(self.msgs_conversation)
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
- [self.gt_chat[0]] + self.gt_chat[-len(self.msgs_tools) :],
+ [self.gt_chat[0]] + self.gt_chat[-n_tools_gt:],
res,
)
# Without tools
res = await fmt.format([*self.msgs_system, *self.msgs_conversation])
- self.assertListEqual(self.gt_chat[: -len(self.msgs_tools)], res)
+ self.assertListEqual(self.gt_chat[:-n_tools_gt], res)
# Empty
res = await fmt.format([])
@@ -344,19 +305,17 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
"""Base64-encoded image becomes an input_image item with data URI."""
fmt = OpenAIResponseFormatter()
msgs = [
- Msg(
+ UserMsg(
name="user",
content=[
- TextBlock(type="text", text="What's in this image?"),
+ TextBlock(text="What's in this image?"),
DataBlock(
source=Base64Source(
- type="base64",
data=self.image_b64,
media_type="image/png",
),
),
],
- role="user",
),
]
res = await fmt.format(msgs)
@@ -385,21 +344,26 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
"""ThinkingBlock without reasoning_item_id is silently skipped."""
fmt = OpenAIResponseFormatter()
msgs = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ThinkingBlock(thinking="inner thoughts"),
- TextBlock(type="text", text="reply"),
+ TextBlock(text="reply"),
],
- role="assistant",
),
]
res = await fmt.format(msgs)
- self.assertEqual(len(res), 1)
- self.assertEqual(res[0]["role"], "assistant")
- parts = res[0]["content"]
- self.assertEqual(len(parts), 1)
- self.assertEqual(parts[0]["type"], "input_text")
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": [
+ {"type": "input_text", "text": "reply"},
+ ],
+ },
+ ],
+ res,
+ )
async def test_chat_formatter_thinking_echoed_with_reasoning_item_id(
self,
@@ -410,79 +374,129 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
thinking = ThinkingBlock(thinking="my reasoning")
thinking.reasoning_item_id = "rs_001"
msgs = [
- Msg(
+ AssistantMsg(
name="assistant",
- content=[thinking, TextBlock(type="text", text="reply")],
- role="assistant",
+ content=[thinking, TextBlock(text="reply")],
),
]
res = await fmt.format(msgs)
- reasoning_items = [r for r in res if r.get("type") == "reasoning"]
- self.assertEqual(len(reasoning_items), 1)
- self.assertEqual(reasoning_items[0]["id"], "rs_001")
- self.assertEqual(
- reasoning_items[0]["summary"],
- [{"type": "summary_text", "text": "my reasoning"}],
+ self.assertListEqual(
+ [
+ {
+ "type": "reasoning",
+ "id": "rs_001",
+ "summary": [
+ {"type": "summary_text", "text": "my reasoning"},
+ ],
+ "content": [],
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {"type": "input_text", "text": "reply"},
+ ],
+ },
+ ],
+ res,
)
- async def test_chat_formatter_url_image_in_tool_result(self) -> None:
+ @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."""
- with patch.object(shortuuid, "uuid", return_value=_FIXED_ID):
- fmt = OpenAIResponseFormatter()
- msgs = [
- Msg(
- name="assistant",
- content=[
- ToolCallBlock(
- id="call_img",
- name="get_map",
- input='{"city": "Tokyo"}',
- ),
- ],
- role="assistant",
- ),
- Msg(
- name="tool",
- content=[
- ToolResultBlock(
- id="call_img",
- name="get_map",
- output=[
- TextBlock(
- type="text",
- text="Here is the map.",
- ),
- DataBlock(
- source=URLSource(
- url=self.image_url,
- media_type="image/png",
- ),
+ fmt = OpenAIResponseFormatter()
+ 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,
- ),
- ],
- role="assistant",
- ),
- ]
- res = await fmt.format(msgs)
-
- self.assertEqual(len(res), 3)
- # function_call
- self.assertEqual(res[0]["type"], "function_call")
- # function_call_output with reminder text
- self.assertEqual(res[1]["type"], "function_call_output")
- self.assertIn("Here is the map.", res[1]["output"])
- # promoted user message with the image
- self.assertEqual(res[2]["role"], "user")
- image_parts = [
- p
- for p in res[2]["content"]
- if p.get("type") == "input_image"
- and p.get("image_url") == self.image_url
+ ),
+ ],
+ state=ToolResultState.SUCCESS,
+ ),
+ TextBlock(text="Here is the map of Tokyo."),
+ ],
+ ),
]
- self.assertEqual(len(image_parts), 1)
+ res = await fmt.format(msgs)
+
+ expected_tool_content = (
+ "Here is the map.\n"
+ f"<system-reminder>A(n) image file is returned "
+ f"and will be presented to you with the identifier "
+ f"[{_FIXED_ID}].</system-reminder>"
+ )
+ self.assertListEqual(
+ [
+ {
+ "type": "function_call",
+ "id": "call_img",
+ "call_id": "call_img",
+ "name": "get_map",
+ "arguments": '{"city": "Tokyo"}',
+ },
+ {
+ "type": "function_call_output",
+ "call_id": "call_img",
+ "output": expected_tool_content,
+ },
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "input_text",
+ "text": (
+ "<system-reminder>The multimodal data "
+ "and their identifiers are listed as "
+ "follows:"
+ ),
+ },
+ {
+ "type": "input_text",
+ "text": f"- {_FIXED_ID} (image file): ",
+ },
+ {
+ "type": "input_image",
+ "image_url": self.image_url,
+ },
+ {
+ "type": "input_text",
+ "text": "</system-reminder>",
+ },
+ ],
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "input_text",
+ "text": "Here is the map of Tokyo.",
+ },
+ ],
+ },
+ ],
+ res,
+ )
# -------------------------------------------------------------------
# OpenAIResponseMultiAgentFormatter tests
@@ -491,7 +505,6 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
async def test_multiagent_formatter(self) -> None:
"""MultiAgent formatter produces exact output for various subsets."""
fmt = OpenAIResponseMultiAgentFormatter()
- self.maxDiff = None
# Full history
res = await fmt.format(
@@ -515,25 +528,25 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
res = await fmt.format(self.msgs_conversation)
self.assertListEqual([self.gt_multiagent[1]], res)
- # Tools only — is_first=True for trailing assistant
+ # Tools only
res = await fmt.format(self.msgs_tools)
self.assertListEqual(
[
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
- # System + tools — same is_first=True
+ # System + tools
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
[
self.gt_multiagent[0],
self._gt_tool_call,
self._gt_tool_result,
- self._gt_trailing_asst_first,
+ self._gt_trailing_asst,
],
res,
)
@@ -541,3 +554,184 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
# Empty
res = await fmt.format([])
self.assertListEqual([], res)
+
+ async def test_chat_formatter_complex_multi_step(self) -> None:
+ """Complex multi-step sequence with interleaved thinking, text,
+ tool calls, and tool results."""
+ fmt = OpenAIResponseFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ThinkingBlock(thinking="thinking_1"),
+ TextBlock(text="text_1"),
+ ToolCallBlock(
+ id="call_1",
+ name="func_1",
+ input='{"arg": "value1"}',
+ ),
+ ToolCallBlock(
+ id="call_2",
+ name="func_2",
+ input='{"arg": "value2"}',
+ ),
+ ToolResultBlock(
+ id="call_1",
+ name="func_1",
+ output=[TextBlock(text="result_1")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolResultBlock(
+ id="call_2",
+ name="func_2",
+ output=[TextBlock(text="result_2")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_2"),
+ TextBlock(text="text_2"),
+ ToolCallBlock(
+ id="call_3",
+ name="func_3",
+ input='{"arg": "value3"}',
+ ),
+ ToolResultBlock(
+ id="call_3",
+ name="func_3",
+ output=[TextBlock(text="result_3")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolCallBlock(
+ id="call_4",
+ name="func_4",
+ input='{"arg": "value4"}',
+ ),
+ ToolResultBlock(
+ id="call_4",
+ name="func_4",
+ output=[TextBlock(text="result_4")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_3"),
+ TextBlock(text="text_3"),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": [
+ {"type": "input_text", "text": "text_1"},
+ ],
+ },
+ {
+ "type": "function_call",
+ "id": "call_1",
+ "call_id": "call_1",
+ "name": "func_1",
+ "arguments": '{"arg": "value1"}',
+ },
+ {
+ "type": "function_call",
+ "id": "call_2",
+ "call_id": "call_2",
+ "name": "func_2",
+ "arguments": '{"arg": "value2"}',
+ },
+ {
+ "type": "function_call_output",
+ "call_id": "call_1",
+ "output": "result_1",
+ },
+ {
+ "type": "function_call_output",
+ "call_id": "call_2",
+ "output": "result_2",
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {"type": "input_text", "text": "text_2"},
+ ],
+ },
+ {
+ "type": "function_call",
+ "id": "call_3",
+ "call_id": "call_3",
+ "name": "func_3",
+ "arguments": '{"arg": "value3"}',
+ },
+ {
+ "type": "function_call_output",
+ "call_id": "call_3",
+ "output": "result_3",
+ },
+ {
+ "type": "function_call",
+ "id": "call_4",
+ "call_id": "call_4",
+ "name": "func_4",
+ "arguments": '{"arg": "value4"}',
+ },
+ {
+ "type": "function_call_output",
+ "call_id": "call_4",
+ "output": "result_4",
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {"type": "input_text", "text": "text_3"},
+ ],
+ },
+ ],
+ res,
+ )
+
+ async def test_chat_formatter_hint_block(self) -> None:
+ """HintBlock flushes preceding content and becomes a user message."""
+ fmt = OpenAIResponseFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ TextBlock(text="Let me think about that."),
+ HintBlock(hint="Remember to be concise."),
+ TextBlock(text="Here is my answer."),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "input_text",
+ "text": "Let me think about that.",
+ },
+ ],
+ },
+ {
+ "role": "user",
+ "content": [
+ {
+ "type": "input_text",
+ "text": "Remember to be concise.",
+ },
+ ],
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {
+ "type": "input_text",
+ "text": "Here is my answer.",
+ },
+ ],
+ },
+ ],
+ res,
+ )
diff --git a/tests/formatter_xai_test.py b/tests/formatter_xai_test.py
index 3a0aa9ad3..38082841c 100644
--- a/tests/formatter_xai_test.py
+++ b/tests/formatter_xai_test.py
@@ -4,8 +4,8 @@ XAIMultiAgentFormatter (xAI), following the reference test style.
Because these formatters return xai_sdk protobuf Message objects (not plain
dicts), a lightweight xai_sdk stub is built at module load so that tests run
-without the real package. Assertions check mock object attributes
-(role, args, kwargs, tool_calls, etc.) rather than plain-dict equality.
+without the real package. The stub objects support __eq__ and __repr__ so
+full assertListEqual comparisons work.
"""
import sys
from typing import Any
@@ -15,14 +15,93 @@ from unittest.mock import MagicMock
from agentscope.formatter import XAIChatFormatter, XAIMultiAgentFormatter
from agentscope.message import (
- Msg,
+ UserMsg,
+ AssistantMsg,
+ SystemMsg,
TextBlock,
ToolCallBlock,
ToolResultBlock,
ThinkingBlock,
ToolResultState,
+ HintBlock,
)
+
+# ---------------------------------------------------------------------------
+# Comparable stub objects for xai_sdk protobuf messages.
+# ---------------------------------------------------------------------------
+
+
+class _StubMessage:
+ """Comparable stub for xai_sdk.chat.{user,assistant,system,tool_result}."""
+
+ def __init__(
+ self,
+ role: str,
+ args: tuple = (),
+ kwargs: dict | None = None,
+ ) -> None:
+ self.role = role
+ self.args = args
+ self.kwargs = kwargs or {}
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, _StubMessage):
+ return NotImplemented
+ return (
+ self.role == other.role
+ and self.args == other.args
+ and self.kwargs == other.kwargs
+ )
+
+ def __repr__(self) -> str:
+ parts = [f"role={self.role!r}", f"args={self.args!r}"]
+ if self.kwargs:
+ parts.append(f"kwargs={self.kwargs!r}")
+ return f"_StubMessage({', '.join(parts)})"
+
+
+class _StubImage:
+ """Comparable stub for xai_sdk.chat.image()."""
+
+ def __init__(self, url: str) -> None:
+ self.type = "image"
+ self.url = url
+
+ def __eq__(self, other: object) -> bool:
+ if not isinstance(other, _StubImage):
+ return NotImplemented
+ return self.url == other.url
+
+ def __repr__(self) -> str:
+ return f"_StubImage(url={self.url!r})"
+
+
+def user(*args: Any) -> _StubMessage:
+ """Create a comparable stub user message."""
+ return _StubMessage(role="user", args=args)
+
+
+def assistant(*args: Any) -> _StubMessage:
+ """Create a comparable stub assistant message."""
+ return _StubMessage(role="assistant", args=args)
+
+
+def system(*args: Any) -> _StubMessage:
+ """Create a comparable stub system message."""
+ return _StubMessage(role="system", args=args)
+
+
+def tool_result(*args: Any, **kwargs: Any) -> _StubMessage:
+ """Create a comparable stub tool_result message."""
+ return _StubMessage(role="tool", args=args, kwargs=kwargs)
+
+
+def image(url: str) -> _StubImage:
+ """Create a comparable stub image object."""
+ return _StubImage(url=url)
+
+
# ---------------------------------------------------------------------------
# Build a lightweight xai_sdk stub so tests run without the real package.
# ---------------------------------------------------------------------------
@@ -59,13 +138,15 @@ def _build_xai_sdk_stub() -> None:
return item
class _FunctionSpec:
- name: str = ""
- arguments: str = ""
+ def __init__(self) -> None:
+ self.name = ""
+ self.arguments = ""
class _ToolCallProto:
- id: str = ""
- type: int = 0
- function = _FunctionSpec()
+ def __init__(self) -> None:
+ self.id = ""
+ self.type = 0
+ self.function = _FunctionSpec()
class _ContentPart:
text: str = ""
@@ -80,15 +161,11 @@ def _build_xai_sdk_stub() -> None:
xai_chat = ModuleType("xai_sdk.chat")
xai_chat.chat_pb2 = chat_pb2
- xai_chat.user = lambda *args: MagicMock(role="user", args=args)
- xai_chat.assistant = lambda *args: MagicMock(role="assistant", args=args)
- xai_chat.system = lambda *args: MagicMock(role="system", args=args)
- xai_chat.tool_result = lambda *args, **kw: MagicMock(
- role="tool",
- args=args,
- kwargs=kw,
- )
- xai_chat.image = lambda url: MagicMock(type="image", url=url)
+ xai_chat.user = user
+ xai_chat.assistant = assistant
+ xai_chat.system = system
+ xai_chat.tool_result = tool_result
+ xai_chat.image = image
xai_sdk = ModuleType("xai_sdk")
xai_sdk.chat = xai_chat
@@ -105,49 +182,44 @@ _build_xai_sdk_stub()
class TestXAIFormatter(IsolatedAsyncioTestCase):
"""Comprehensive tests for XAI Chat and MultiAgent formatters.
- The formatter returns proto/mock objects rather than dicts, so assertions
- inspect mock attributes (role, args, kwargs, tool_calls, etc.).
+ The stub objects support __eq__, so full assertListEqual works for
+ user/assistant/system/tool_result messages. Tool-call messages use
+ _MessageProto which is checked via attribute assertions.
"""
async def asyncSetUp(self) -> None:
self.msgs_system = [
- Msg(
+ SystemMsg(
name="system",
content="You're a helpful assistant.",
- role="system",
),
]
self.msgs_conversation = [
- Msg(
+ UserMsg(
name="user",
content="What is the capital of France?",
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of France is Paris.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Germany?",
- role="user",
),
- Msg(
+ AssistantMsg(
name="assistant",
content="The capital of Germany is Berlin.",
- role="assistant",
),
- Msg(
+ UserMsg(
name="user",
content="What is the capital of Japan?",
- role="user",
),
]
self.msgs_tools = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ToolCallBlock(
@@ -155,30 +227,16 @@ class TestXAIFormatter(IsolatedAsyncioTestCase):
name="get_capital",
input='{"country": "Japan"}',
),
- ],
- role="assistant",
- ),
- Msg(
- name="tool",
- content=[
ToolResultBlock(
id="call_1",
name="get_capital",
output=[
- TextBlock(
- type="text",
- text="The capital of Japan is Tokyo.",
- ),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
state=ToolResultState.SUCCESS,
),
+ TextBlock(text="The capital of Japan is Tokyo."),
],
- role="assistant",
- ),
- Msg(
- name="assistant",
- content="The capital of Japan is Tokyo.",
- role="assistant",
),
]
@@ -191,20 +249,28 @@ class TestXAIFormatter(IsolatedAsyncioTestCase):
# -------------------------------------------------------------------
async def test_chat_formatter_system_message(self) -> None:
- """System message becomes a system() proto."""
+ """System message becomes a system() stub."""
fmt = XAIChatFormatter()
res = await fmt.format(self.msgs_system)
- self.assertEqual(len(res), 1)
- self.assertEqual(res[0].role, "system")
- self.assertIn("You're a helpful assistant.", res[0].args)
+ self.assertListEqual(
+ [system("You're a helpful assistant.")],
+ res,
+ )
async def test_chat_formatter_user_assistant(self) -> None:
"""User and assistant text messages are passed through correctly."""
fmt = XAIChatFormatter()
res = await fmt.format(self.msgs_conversation)
- roles = [m.role for m in res]
- self.assertEqual(roles.count("user"), 3)
- self.assertEqual(roles.count("assistant"), 2)
+ self.assertListEqual(
+ [
+ user("What is the capital of France?"),
+ assistant("The capital of France is Paris."),
+ user("What is the capital of Germany?"),
+ assistant("The capital of Germany is Berlin."),
+ user("What is the capital of Japan?"),
+ ],
+ res,
+ )
async def test_chat_formatter_tool_call(self) -> None:
"""Assistant tool call becomes a _MessageProto with tool_calls set."""
@@ -218,25 +284,29 @@ class TestXAIFormatter(IsolatedAsyncioTestCase):
for m in res
if hasattr(m, "tool_calls") and len(m.tool_calls) > 0
]
- self.assertEqual(len(tool_call_msgs), 1)
+ self.assertListEqual(
+ [len(tool_call_msgs)],
+ [1],
+ )
tc = tool_call_msgs[0].tool_calls[0]
- self.assertEqual(tc.id, "call_1")
- self.assertEqual(tc.function.name, "get_capital")
- self.assertIn('"country"', tc.function.arguments)
+ self.assertListEqual(
+ [tc.id, tc.function.name, tc.function.arguments],
+ ["call_1", "get_capital", '{"country": "Japan"}'],
+ )
async def test_chat_formatter_tool_result(self) -> None:
- """Tool result becomes a tool_result() proto with the right id."""
+ """Tool result becomes a tool_result() stub with the right id."""
fmt = XAIChatFormatter()
res = await fmt.format(self.msgs_tools)
tool_msgs = [m for m in res if m.role == "tool"]
- self.assertEqual(len(tool_msgs), 1)
- self.assertEqual(
- tool_msgs[0].kwargs.get("tool_call_id"),
- "call_1",
- )
- self.assertIn(
- "The capital of Japan is Tokyo.",
- tool_msgs[0].args,
+ self.assertListEqual(
+ tool_msgs,
+ [
+ tool_result(
+ "The capital of Japan is Tokyo.",
+ tool_call_id="call_1",
+ ),
+ ],
)
async def test_chat_formatter_thinking_dropped(self) -> None:
@@ -244,19 +314,16 @@ class TestXAIFormatter(IsolatedAsyncioTestCase):
messages."""
fmt = XAIChatFormatter()
msgs = [
- Msg(
+ AssistantMsg(
name="assistant",
content=[
ThinkingBlock(thinking="inner thoughts"),
- TextBlock(type="text", text="reply"),
+ TextBlock(text="reply"),
],
- role="assistant",
),
]
res = await fmt.format(msgs)
- # thinking block is silently skipped for assistant text path
- self.assertEqual(len(res), 1)
- self.assertEqual(res[0].role, "assistant")
+ self.assertListEqual([assistant("reply")], res)
async def test_chat_formatter_empty(self) -> None:
"""Empty input returns empty list."""
@@ -269,26 +336,32 @@ class TestXAIFormatter(IsolatedAsyncioTestCase):
# -------------------------------------------------------------------
async def test_multiagent_formatter_system_message(self) -> None:
- """System message is passed through as a system() proto."""
+ """System message is passed through as a system() stub."""
fmt = XAIMultiAgentFormatter()
res = await fmt.format(
[*self.msgs_system, *self.msgs_conversation],
)
- self.assertEqual(res[0].role, "system")
- self.assertIn("You're a helpful assistant.", res[0].args)
+ self.assertEqual(res[0], system("You're a helpful assistant."))
async def test_multiagent_formatter_conversation_history(self) -> None:
"""Non-tool agent messages are collapsed into a user() history
- proto."""
+ stub."""
fmt = XAIMultiAgentFormatter()
res = await fmt.format(self.msgs_conversation)
- self.assertEqual(len(res), 1)
- self.assertEqual(res[0].role, "user")
- hist_text = res[0].args[0]
- self.assertIn("<history>", hist_text)
- self.assertIn("</history>", hist_text)
- self.assertIn("user: What is the capital of France?", hist_text)
- self.assertIn("assistant: The capital of France is Paris.", hist_text)
+ self.assertListEqual(
+ [
+ user(
+ self._hist_prompt + "<history>\n"
+ "user: What is the capital of France?\n"
+ "assistant: The capital of France is Paris.\n"
+ "user: What is the capital of Germany?\n"
+ "assistant: The capital of Germany is Berlin.\n"
+ "user: What is the capital of Japan?\n"
+ "</history>",
+ ),
+ ],
+ res,
+ )
async def test_multiagent_formatter_first_group_has_hist_prompt(
self,
@@ -298,50 +371,190 @@ class TestXAIFormatter(IsolatedAsyncioTestCase):
fmt = XAIMultiAgentFormatter()
res = await fmt.format(self.msgs_conversation)
hist_text = res[0].args[0]
- self.assertTrue(hist_text.startswith(self._hist_prompt))
+ self.assertListEqual(
+ [hist_text.startswith(self._hist_prompt)],
+ [True],
+ )
async def test_multiagent_formatter_full_history(self) -> None:
"""Full history produces system + conv_history + tool_call +
- tool_result + trailing."""
+ tool_result + trailing assistant."""
fmt = XAIMultiAgentFormatter()
res = await fmt.format(
[*self.msgs_system, *self.msgs_conversation, *self.msgs_tools],
)
roles = [m.role for m in res]
- self.assertIn("system", roles)
- self.assertIn("user", roles)
- self.assertIn("tool", roles)
+ self.assertListEqual(
+ sorted(r for r in roles if isinstance(r, str)),
+ sorted(["system", "user", "tool", "assistant"]),
+ )
async def test_multiagent_formatter_tools_only_is_first(self) -> None:
- """When only tools are given, trailing agent uses is_first=True
- history."""
+ """When only tools are given, trailing text is formatted as
+ assistant."""
fmt = XAIMultiAgentFormatter()
res = await fmt.format(self.msgs_tools)
- # tool_call proto, tool_result proto, trailing user history
- trailing = [m for m in res if m.role == "user"]
- self.assertEqual(len(trailing), 1)
- hist_text = trailing[0].args[0]
- self.assertIn(self._hist_prompt, hist_text)
- self.assertIn("assistant: The capital of Japan is Tokyo.", hist_text)
-
- async def test_multiagent_formatter_nonfirst_trailing_no_hist_prompt(
+ trailing = [m for m in res if m.role == "assistant"]
+ self.assertListEqual([len(trailing)], [1])
+
+ async def test_multiagent_formatter_nonfirst_trailing_is_assistant(
self,
) -> None:
- """Trailing agent after a conversation group uses is_first=False
- (no prompt)."""
+ """Trailing text after a tool sequence is formatted as assistant."""
fmt = XAIMultiAgentFormatter()
res = await fmt.format(
[*self.msgs_conversation, *self.msgs_tools],
)
- user_msgs = [m for m in res if m.role == "user"]
- # First is conversation history (is_first=True), last is trailing
- # (is_first=False)
- trailing_text = user_msgs[-1].args[0]
- self.assertNotIn(self._hist_prompt, trailing_text)
- self.assertIn("<history>", trailing_text)
+ trailing = [m for m in res if m.role == "assistant"]
+ self.assertListEqual([len(trailing)], [1])
async def test_multiagent_formatter_empty(self) -> None:
"""Empty input returns empty list."""
fmt = XAIMultiAgentFormatter()
res = await fmt.format([])
self.assertListEqual([], res)
+
+ async def test_chat_formatter_complex_multi_step(self) -> None:
+ """Complex multi-step sequence with interleaved thinking, text,
+ tool calls, and tool results."""
+ fmt = XAIChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ThinkingBlock(thinking="thinking_1"),
+ TextBlock(text="text_1"),
+ ToolCallBlock(
+ id="call_1",
+ name="func_1",
+ input='{"arg": "value1"}',
+ ),
+ ToolCallBlock(
+ id="call_2",
+ name="func_2",
+ input='{"arg": "value2"}',
+ ),
+ ToolResultBlock(
+ id="call_1",
+ name="func_1",
+ output=[TextBlock(text="result_1")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolResultBlock(
+ id="call_2",
+ name="func_2",
+ output=[TextBlock(text="result_2")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_2"),
+ TextBlock(text="text_2"),
+ ToolCallBlock(
+ id="call_3",
+ name="func_3",
+ input='{"arg": "value3"}',
+ ),
+ ToolResultBlock(
+ id="call_3",
+ name="func_3",
+ output=[TextBlock(text="result_3")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ToolCallBlock(
+ id="call_4",
+ name="func_4",
+ input='{"arg": "value4"}',
+ ),
+ ToolResultBlock(
+ id="call_4",
+ name="func_4",
+ output=[TextBlock(text="result_4")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ThinkingBlock(thinking="thinking_3"),
+ TextBlock(text="text_3"),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+
+ # First message: proto with text_1 content + 2 tool_calls
+ self.assertTrue(hasattr(res[0], "tool_calls"))
+ self.assertEqual(len(res[0].tool_calls), 2)
+ self.assertEqual(len(res[0].content), 1)
+ self.assertEqual(res[0].tool_calls[0].id, "call_1")
+ self.assertEqual(res[0].tool_calls[0].function.name, "func_1")
+ self.assertEqual(
+ res[0].tool_calls[0].function.arguments,
+ '{"arg": "value1"}',
+ )
+ self.assertEqual(res[0].tool_calls[1].id, "call_2")
+ self.assertEqual(res[0].tool_calls[1].function.name, "func_2")
+ self.assertEqual(
+ res[0].tool_calls[1].function.arguments,
+ '{"arg": "value2"}',
+ )
+
+ # Tool results
+ self.assertEqual(
+ res[1],
+ tool_result("result_1", tool_call_id="call_1"),
+ )
+ self.assertEqual(
+ res[2],
+ tool_result("result_2", tool_call_id="call_2"),
+ )
+
+ # Second proto: text_2 + 1 tool_call
+ self.assertTrue(hasattr(res[3], "tool_calls"))
+ self.assertEqual(len(res[3].tool_calls), 1)
+ self.assertEqual(len(res[3].content), 1)
+ self.assertEqual(res[3].tool_calls[0].id, "call_3")
+ self.assertEqual(res[3].tool_calls[0].function.name, "func_3")
+
+ # Tool result for call_3
+ self.assertEqual(
+ res[4],
+ tool_result("result_3", tool_call_id="call_3"),
+ )
+
+ # Third proto: no content + 1 tool_call
+ self.assertTrue(hasattr(res[5], "tool_calls"))
+ self.assertEqual(len(res[5].tool_calls), 1)
+ self.assertEqual(len(res[5].content), 0)
+ self.assertEqual(res[5].tool_calls[0].id, "call_4")
+ self.assertEqual(res[5].tool_calls[0].function.name, "func_4")
+
+ # Tool result for call_4
+ self.assertEqual(
+ res[6],
+ tool_result("result_4", tool_call_id="call_4"),
+ )
+
+ # Final assistant text
+ self.assertEqual(res[7], assistant("text_3"))
+
+ # Total 8 items
+ self.assertEqual(len(res), 8)
+
+ async def test_chat_formatter_hint_block(self) -> None:
+ """HintBlock flushes preceding content and becomes a user message."""
+ fmt = XAIChatFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ TextBlock(text="Let me think about that."),
+ HintBlock(hint="Remember to be concise."),
+ TextBlock(text="Here is my answer."),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ assistant("Let me think about that."),
+ user("Remember to be concise."),
+ assistant("Here is my answer."),
+ ],
+ res,
+ )
diff --git a/tests/model_anthropic_test.py b/tests/model_anthropic_test.py
index f28064e3d..6c6dcefb3 100644
--- a/tests/model_anthropic_test.py
+++ b/tests/model_anthropic_test.py
@@ -1,140 +1,419 @@
# -*- coding: utf-8 -*-
# pylint: disable=protected-access
-"""Unit tests for AnthropicChatModel response parsing.
+"""Unit tests for AnthropicChatModel with mocked API responses.
-Formatter tests have been moved to tests/formatter_anthropic_test.py.
+Tests cover both non-streaming and streaming modes.
+Anthropic uses event-based streaming (message_start, content_block_start,
+content_block_delta, message_delta events).
"""
import json
from typing import Any
-from datetime import datetime
import unittest
from unittest import IsolatedAsyncioTestCase
-from unittest.mock import MagicMock
+from unittest.mock import AsyncMock, MagicMock, patch
+
+from utils import AnyString
from agentscope.message import TextBlock, ToolCallBlock, ThinkingBlock
from agentscope.model import AnthropicChatModel
from agentscope.credential import AnthropicCredential
from agentscope.tool import ToolChoice
+A = AnyString()
+
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
-def _make_model() -> Any:
+def _make_model(stream: bool = False) -> Any:
return AnthropicChatModel(
credential=AnthropicCredential(api_key="test"),
model="claude-opus-4-5",
- stream=False,
+ stream=stream,
context_size=200_000,
)
+def _mock_completion(
+ text: Any = None,
+ tool_calls: Any = None,
+ thinking: Any = None,
+ response_id: str = "msg-1",
+) -> MagicMock:
+ """Build a mock non-streaming Anthropic Message response."""
+ blocks = []
+ if thinking:
+ b = MagicMock()
+ b.type = "thinking"
+ b.thinking = thinking
+ b.signature = "sig123"
+ blocks.append(b)
+ if text:
+ b = MagicMock()
+ b.type = "text"
+ b.text = text
+ blocks.append(b)
+ if tool_calls:
+ for tc in tool_calls:
+ b = MagicMock()
+ b.type = "tool_use"
+ b.id = tc["id"]
+ b.name = tc["name"]
+ b.input = tc["input"]
+ blocks.append(b)
+
+ resp = MagicMock()
+ resp.id = response_id
+ resp.content = blocks
+ resp.usage = MagicMock()
+ resp.usage.input_tokens = 10
+ resp.usage.output_tokens = 5
+ resp.usage.cache_creation_input_tokens = 0
+ resp.usage.cache_read_input_tokens = 0
+ return resp
+
+
+def _make_event(event_type: str, **kwargs: Any) -> MagicMock:
+ """Build a mock Anthropic streaming event."""
+ event = MagicMock()
+ event.type = event_type
+ for key, val in kwargs.items():
+ setattr(event, key, val)
+ return event
+
+
+class _MockAsyncEventStream:
+ """Mock async iterator over Anthropic events."""
+
+ def __init__(self, events: list) -> None:
+ self._events = events
+ self._index = 0
+
+ def __aiter__(self) -> "_MockAsyncEventStream":
+ return self
+
+ async def __anext__(self) -> Any:
+ if self._index >= len(self._events):
+ raise StopAsyncIteration
+ event = self._events[self._index]
+ self._index += 1
+ return event
+
+
# ---------------------------------------------------------------------------
-# Model response parsing tests
+# Non-streaming tests
# ---------------------------------------------------------------------------
-class TestAnthropicModelParsing(IsolatedAsyncioTestCase):
- """Unit tests for AnthropicChatModel response parsing."""
+class TestAnthropicNonStream(IsolatedAsyncioTestCase):
+ """Tests for AnthropicChatModel in non-streaming mode."""
def setUp(self) -> None:
- """Set up a fresh model instance and start time."""
- self.model = _make_model()
- self.start = datetime.now()
+ self.model = _make_model(stream=False)
- def _mock_anthropic_response(
+ @patch("anthropic.AsyncAnthropic")
+ async def test_text_response(self, mock_client_cls: MagicMock) -> None:
+ """Non-stream text response returns a single ChatResponse."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(text="Hello!"),
+ )
+ mock_client_cls.return_value.messages.create = mock_create
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (True, [TextBlock.model_construct(id=A, text="Hello!")]),
+ )
+ self.assertEqual(result.id, "msg-1")
+
+ @patch("anthropic.AsyncAnthropic")
+ async def test_tool_call_response(
self,
- text: Any = None,
- tool_calls: Any = None,
- thinking: Any = None,
- ) -> "MagicMock":
- """Build a mock Anthropic API response object."""
- blocks = []
- if thinking:
- b = MagicMock()
- b.type = "thinking"
- b.thinking = thinking
- b.signature = ""
- blocks.append(b)
- if text:
- b = MagicMock()
- b.type = "text"
- b.text = text
- blocks.append(b)
- if tool_calls:
- for tc in tool_calls:
- b = MagicMock()
- b.type = "tool_use"
- b.id = tc["id"]
- b.name = tc["name"]
- b.input = tc["input"]
- blocks.append(b)
-
- resp = MagicMock()
- resp.id = "msg-1"
- resp.content = blocks
- resp.usage.input_tokens = 10
- resp.usage.output_tokens = 5
- return resp
-
- async def test_parse_text_response(self) -> None:
- """Parsing a text response creates a TextBlock."""
- resp = self._mock_anthropic_response(text="Hello!")
- result = await self.model._parse_anthropic_completion_response(
- self.start,
- resp,
- )
- self.assertTrue(result.is_last)
- texts = [b for b in result.content if isinstance(b, TextBlock)]
- self.assertEqual(texts[0].text, "Hello!")
-
- async def test_parse_tool_call_response(self) -> None:
- """Parsing a tool-call response creates a ToolCallBlock."""
- resp = self._mock_anthropic_response(
- tool_calls=[
- {
- "id": "call-1",
- "name": "get_weather",
- "input": {"city": "Beijing"},
- },
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Non-stream tool call response creates ToolCallBlocks."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(
+ tool_calls=[
+ {
+ "id": "toolu_1",
+ "name": "get_weather",
+ "input": {"city": "Beijing"},
+ },
+ ],
+ ),
+ )
+ mock_client_cls.return_value.messages.create = mock_create
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ToolCallBlock(
+ id="toolu_1",
+ name="get_weather",
+ input=json.dumps({"city": "Beijing"}),
+ ),
+ ],
+ ),
+ )
+
+ @patch("anthropic.AsyncAnthropic")
+ async def test_thinking_response(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Non-stream response with reasoning creates ThinkingBlock."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(
+ thinking="Deep thought...",
+ text="Answer",
+ ),
+ )
+ mock_client_cls.return_value.messages.create = mock_create
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Deep thought...",
+ signature="sig123",
+ ),
+ TextBlock.model_construct(id=A, text="Answer"),
+ ],
+ ),
+ )
+
+
+# ---------------------------------------------------------------------------
+# Streaming tests
+# ---------------------------------------------------------------------------
+
+
+class TestAnthropicStream(IsolatedAsyncioTestCase):
+ """Tests for AnthropicChatModel in streaming mode."""
+
+ def setUp(self) -> None:
+ self.model = _make_model(stream=True)
+
+ @patch("anthropic.AsyncAnthropic")
+ async def test_stream_text(self, mock_client_cls: MagicMock) -> None:
+ """Stream text yields n deltas + 1 final with full content."""
+ msg_usage = MagicMock()
+ msg_usage.input_tokens = 10
+ msg_usage.output_tokens = 0
+ msg_usage.cache_creation_input_tokens = 0
+ msg_usage.cache_read_input_tokens = 0
+
+ message = MagicMock()
+ message.id = "msg-1"
+ message.usage = msg_usage
+
+ delta1 = MagicMock()
+ delta1.type = "text_delta"
+ delta1.text = "Hello"
+
+ delta2 = MagicMock()
+ delta2.type = "text_delta"
+ delta2.text = " world"
+
+ msg_delta_usage = MagicMock()
+ msg_delta_usage.output_tokens = 5
+
+ events = [
+ _make_event("message_start", message=message),
+ _make_event("content_block_delta", index=0, delta=delta1),
+ _make_event("content_block_delta", index=0, delta=delta2),
+ _make_event(
+ "message_delta",
+ usage=msg_delta_usage,
+ ),
+ ]
+ mock_create = AsyncMock(
+ return_value=_MockAsyncEventStream(events),
+ )
+ mock_client_cls.return_value.messages.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [TextBlock.model_construct(id=A, text="Hello")]),
+ (False, [TextBlock.model_construct(id=A, text=" world")]),
+ (True, [TextBlock.model_construct(id=A, text="Hello world")]),
],
)
- result = await self.model._parse_anthropic_completion_response(
- self.start,
- resp,
+ self.assertEqual(responses[-1].id, "msg-1")
+
+ @patch("anthropic.AsyncAnthropic")
+ async def test_stream_thinking_and_text(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream thinking + text yields deltas then final with signature."""
+ msg_usage = MagicMock()
+ msg_usage.input_tokens = 10
+ msg_usage.output_tokens = 0
+ msg_usage.cache_creation_input_tokens = 0
+ msg_usage.cache_read_input_tokens = 0
+
+ message = MagicMock()
+ message.id = "msg-2"
+ message.usage = msg_usage
+
+ thinking_delta = MagicMock()
+ thinking_delta.type = "thinking_delta"
+ thinking_delta.thinking = "Let me think"
+
+ sig_delta = MagicMock()
+ sig_delta.type = "signature_delta"
+ sig_delta.signature = "sig_abc"
+
+ text_delta = MagicMock()
+ text_delta.type = "text_delta"
+ text_delta.text = "Result"
+
+ events = [
+ _make_event("message_start", message=message),
+ _make_event("content_block_delta", index=0, delta=thinking_delta),
+ _make_event("content_block_delta", index=0, delta=sig_delta),
+ _make_event("content_block_delta", index=1, delta=text_delta),
+ ]
+ mock_create = AsyncMock(
+ return_value=_MockAsyncEventStream(events),
)
- tcs = [b for b in result.content if isinstance(b, ToolCallBlock)]
- self.assertEqual(tcs[0].id, "call-1")
- self.assertEqual(tcs[0].name, "get_weather")
- self.assertEqual(json.loads(tcs[0].input)["city"], "Beijing")
+ mock_client_cls.return_value.messages.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
- async def test_parse_thinking_response(self) -> None:
- """Parsing a response with thinking creates a ThinkingBlock."""
- resp = self._mock_anthropic_response(
- thinking="Let me think...",
- text="Answer",
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Let me think",
+ ),
+ ],
+ ),
+ (False, [TextBlock.model_construct(id=A, text="Result")]),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Let me think",
+ signature="sig_abc",
+ ),
+ TextBlock.model_construct(id=A, text="Result"),
+ ],
+ ),
+ ],
)
- result = await self.model._parse_anthropic_completion_response(
- self.start,
- resp,
+
+ @patch("anthropic.AsyncAnthropic")
+ async def test_stream_tool_call(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream tool call yields partial deltas then full accumulated
+ input."""
+ msg_usage = MagicMock()
+ msg_usage.input_tokens = 10
+ msg_usage.output_tokens = 0
+ msg_usage.cache_creation_input_tokens = 0
+ msg_usage.cache_read_input_tokens = 0
+
+ message = MagicMock()
+ message.id = "msg-3"
+ message.usage = msg_usage
+
+ tool_block = MagicMock()
+ tool_block.type = "tool_use"
+ tool_block.id = "toolu_1"
+ tool_block.name = "get_weather"
+
+ json_delta1 = MagicMock()
+ json_delta1.type = "input_json_delta"
+ json_delta1.partial_json = '{"city":'
+
+ json_delta2 = MagicMock()
+ json_delta2.type = "input_json_delta"
+ json_delta2.partial_json = '"BJ"}'
+
+ events = [
+ _make_event("message_start", message=message),
+ _make_event(
+ "content_block_start",
+ index=0,
+ content_block=tool_block,
+ ),
+ _make_event("content_block_delta", index=0, delta=json_delta1),
+ _make_event("content_block_delta", index=0, delta=json_delta2),
+ ]
+ mock_create = AsyncMock(
+ return_value=_MockAsyncEventStream(events),
)
- thinkings = [b for b in result.content if isinstance(b, ThinkingBlock)]
- self.assertEqual(thinkings[0].thinking, "Let me think...")
+ mock_client_cls.return_value.messages.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
- async def test_response_id_set(self) -> None:
- """The response ID from the API is stored in the ChatResponse."""
- resp = self._mock_anthropic_response(text="Hi")
- result = await self.model._parse_anthropic_completion_response(
- self.start,
- resp,
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [
+ ToolCallBlock(
+ id="toolu_1",
+ name="get_weather",
+ input='{"city":',
+ ),
+ ],
+ ),
+ (
+ False,
+ [
+ ToolCallBlock(
+ id="toolu_1",
+ name="get_weather",
+ input='"BJ"}',
+ ),
+ ],
+ ),
+ (
+ True,
+ [
+ ToolCallBlock(
+ id="toolu_1",
+ name="get_weather",
+ input='{"city":"BJ"}',
+ ),
+ ],
+ ),
+ ],
)
- self.assertEqual(result.id, "msg-1")
# ---------------------------------------------------------------------------
-# Shared _format_tools fixtures
+# _format_tools tests
# ---------------------------------------------------------------------------
_FT_TOOLS = [
@@ -164,7 +443,6 @@ _FT_TOOLS = [
},
]
-
_FT_TOOLS_ANTHROPIC = [
{
"name": "get_weather",
@@ -187,7 +465,6 @@ _FT_TOOLS_ANTHROPIC = [
]
-# pylint: disable=protected-access
class TestAnthropicFormatTools(unittest.TestCase):
"""Tests for AnthropicChatModel._format_tools."""
diff --git a/tests/model_dashscope_test.py b/tests/model_dashscope_test.py
index a74a6e11d..709e1bdbb 100644
--- a/tests/model_dashscope_test.py
+++ b/tests/model_dashscope_test.py
@@ -1,34 +1,36 @@
# -*- coding: utf-8 -*-
# pylint: disable=protected-access
-"""Unit tests for DashScopeChatModel response parsing.
+"""Unit tests for DashScopeChatModel with mocked API responses.
-Formatter tests have been moved to tests/formatter_dashscope_test.py.
+Tests cover both non-streaming and streaming modes.
"""
-import json
from typing import Any
-from datetime import datetime
import unittest
from unittest import IsolatedAsyncioTestCase
-from unittest.mock import MagicMock
+from unittest.mock import AsyncMock, MagicMock, patch
-from agentscope.message import TextBlock, ToolCallBlock
+from utils import AnyString
+
+from agentscope.message import TextBlock, ToolCallBlock, ThinkingBlock
from agentscope.model import DashScopeChatModel
from agentscope.credential import DashScopeCredential
from agentscope.tool import ToolChoice
+A = AnyString()
+
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
-def _make_model() -> Any:
+def _make_model(stream: bool = False) -> Any:
return DashScopeChatModel(
credential=DashScopeCredential(api_key="test"),
model="qwen3-max",
- stream=False,
+ stream=stream,
max_retries=3,
- context_size=int(1500),
+ context_size=131_072,
parameters=DashScopeChatModel.Parameters(
max_tokens=1000,
thinking_enable=True,
@@ -37,77 +39,381 @@ def _make_model() -> Any:
)
+def _mock_completion(
+ text: Any = None,
+ tool_calls: Any = None,
+ reasoning: Any = None,
+ response_id: str = "req-1",
+) -> MagicMock:
+ """Build a mock non-streaming ChatCompletion response."""
+ msg = MagicMock()
+ msg.content = text
+ msg.reasoning_content = reasoning
+ msg.tool_calls = None
+
+ if tool_calls:
+ tc_mocks = []
+ for tc in tool_calls:
+ m = MagicMock()
+ m.id = tc["id"]
+ m.function.name = tc["name"]
+ m.function.arguments = tc["arguments"]
+ tc_mocks.append(m)
+ msg.tool_calls = tc_mocks
+
+ choice = MagicMock()
+ choice.message = msg
+
+ resp = MagicMock()
+ resp.id = response_id
+ resp.choices = [choice]
+ resp.usage = MagicMock()
+ resp.usage.prompt_tokens = 10
+ resp.usage.completion_tokens = 5
+ resp.usage.prompt_tokens_details = None
+ return resp
+
+
+def _make_stream_chunk(
+ delta_text: str | None = None,
+ delta_reasoning: str | None = None,
+ tool_calls: list | None = None,
+ response_id: str = "req-1",
+ usage: dict | None = None,
+ has_choices: bool = True,
+) -> MagicMock:
+ """Build a single mock streaming chunk."""
+ chunk = MagicMock()
+ chunk.id = response_id
+
+ if usage:
+ chunk.usage = MagicMock()
+ chunk.usage.prompt_tokens = usage.get("prompt_tokens", 0)
+ chunk.usage.completion_tokens = usage.get("completion_tokens", 0)
+ chunk.usage.prompt_tokens_details = None
+ else:
+ chunk.usage = None
+
+ if has_choices:
+ delta = MagicMock()
+ delta.content = delta_text
+ delta.reasoning_content = delta_reasoning
+ delta.tool_calls = tool_calls
+ delta.audio = None
+ choice = MagicMock()
+ choice.delta = delta
+ chunk.choices = [choice]
+ else:
+ chunk.choices = []
+
+ return chunk
+
+
+def _make_tool_call_delta(
+ index: int,
+ tc_id: str | None = None,
+ name: str | None = None,
+ arguments: str | None = None,
+) -> MagicMock:
+ tc = MagicMock()
+ tc.index = index
+ tc.id = tc_id
+ tc.function = MagicMock()
+ tc.function.name = name
+ tc.function.arguments = arguments
+ return tc
+
+
+class _MockAsyncStream:
+ """Mock async stream (context manager + async iterator)."""
+
+ def __init__(self, chunks: list) -> None:
+ self._chunks = chunks
+ self._index = 0
+
+ async def __aenter__(self) -> "_MockAsyncStream":
+ return self
+
+ async def __aexit__(self, *args: Any) -> None:
+ pass
+
+ def __aiter__(self) -> "_MockAsyncStream":
+ return self
+
+ async def __anext__(self) -> Any:
+ if self._index >= len(self._chunks):
+ raise StopAsyncIteration
+ chunk = self._chunks[self._index]
+ self._index += 1
+ return chunk
+
+
# ---------------------------------------------------------------------------
-# Model response parsing tests
+# Non-streaming tests
# ---------------------------------------------------------------------------
-class TestDashScopeModelParsing(IsolatedAsyncioTestCase):
- """Unit tests for DashScopeChatModel response parsing."""
+class TestDashScopeNonStream(IsolatedAsyncioTestCase):
+ """Tests for DashScopeChatModel in non-streaming mode."""
def setUp(self) -> None:
- """Set up a fresh model instance and start time."""
- self.model = _make_model()
- self.start = datetime.now()
+ self.model = _make_model(stream=False)
+
+ @patch("openai.AsyncClient")
+ async def test_text_response(self, mock_client_cls: MagicMock) -> None:
+ """Non-stream text response returns a single ChatResponse."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(text="Hello!"),
+ )
+ mock_client_cls.return_value.chat.completions.create = mock_create
- def _mock_response(
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (True, [TextBlock.model_construct(id=A, text="Hello!")]),
+ )
+ self.assertEqual(result.id, "req-1")
+
+ @patch("openai.AsyncClient")
+ async def test_tool_call_response(
self,
- content: Any = None,
- tool_calls: Any = None,
- reasoning_content: Any = None,
- ) -> "MagicMock":
- """Build a minimal OpenAI ChatCompletion-style mock."""
- message = MagicMock()
- message.content = content
- message.tool_calls = tool_calls
- message.reasoning_content = reasoning_content
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Non-stream tool call response creates ToolCallBlocks."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(
+ tool_calls=[
+ {
+ "id": "call-1",
+ "name": "get_weather",
+ "arguments": '{"city":"Hangzhou"}',
+ },
+ ],
+ ),
+ )
+ mock_client_cls.return_value.chat.completions.create = mock_create
- choice = MagicMock()
- choice.message = message
-
- usage = MagicMock()
- usage.prompt_tokens = 10
- usage.completion_tokens = 5
- usage.prompt_tokens_details = None
-
- resp = MagicMock()
- resp.id = "req-1"
- resp.choices = [choice]
- resp.usage = usage
- return resp
-
- async def test_parse_text_response(self) -> None:
- """Parsing a text response creates a TextBlock."""
- resp = self._mock_response(content="Hello!")
- result = self.model._parse_completion_response(
- self.start,
- resp,
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="get_weather",
+ input='{"city":"Hangzhou"}',
+ ),
+ ],
+ ),
)
- self.assertTrue(result.is_last)
- texts = [b for b in result.content if isinstance(b, TextBlock)]
- self.assertEqual(texts[0].text, "Hello!")
-
- async def test_parse_tool_call_response(self) -> None:
- """Parsing a tool-call response creates a ToolCallBlock."""
- tc_mock = MagicMock()
- tc_mock.id = "call-1"
- tc_mock.function.name = "get_weather"
- tc_mock.function.arguments = '{"city":"Beijing"}'
-
- resp = self._mock_response(tool_calls=[tc_mock])
- result = self.model._parse_completion_response(
- self.start,
- resp,
+
+ @patch("openai.AsyncClient")
+ async def test_thinking_response(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Non-stream response with reasoning creates ThinkingBlock."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(
+ text="42",
+ reasoning="Reasoning step...",
+ ),
+ )
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Reasoning step...",
+ ),
+ TextBlock.model_construct(id=A, text="42"),
+ ],
+ ),
+ )
+
+
+# ---------------------------------------------------------------------------
+# Streaming tests
+# ---------------------------------------------------------------------------
+
+
+class TestDashScopeStream(IsolatedAsyncioTestCase):
+ """Tests for DashScopeChatModel in streaming mode."""
+
+ def setUp(self) -> None:
+ self.model = _make_model(stream=True)
+
+ @patch("openai.AsyncClient")
+ async def test_stream_text(self, mock_client_cls: MagicMock) -> None:
+ """Stream text yields n deltas + 1 final with full content."""
+ chunks = [
+ _make_stream_chunk(delta_text="Hello"),
+ _make_stream_chunk(delta_text=" world"),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={"prompt_tokens": 10, "completion_tokens": 2},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [TextBlock.model_construct(id=A, text="Hello")]),
+ (False, [TextBlock.model_construct(id=A, text=" world")]),
+ (True, [TextBlock.model_construct(id=A, text="Hello world")]),
+ ],
+ )
+
+ @patch("openai.AsyncClient")
+ async def test_stream_thinking_and_text(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream reasoning + text yields deltas then accumulated final."""
+ chunks = [
+ _make_stream_chunk(delta_reasoning="Think"),
+ _make_stream_chunk(delta_reasoning="ing"),
+ _make_stream_chunk(delta_text="Answer"),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={"prompt_tokens": 10, "completion_tokens": 5},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [ThinkingBlock.model_construct(id=A, thinking="Think")],
+ ),
+ (False, [ThinkingBlock.model_construct(id=A, thinking="ing")]),
+ (False, [TextBlock.model_construct(id=A, text="Answer")]),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Thinking",
+ ),
+ TextBlock.model_construct(id=A, text="Answer"),
+ ],
+ ),
+ ],
+ )
+
+ @patch("openai.AsyncClient")
+ async def test_stream_tool_calls(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream tool call chunks accumulate into final ToolCallBlock."""
+ chunks = [
+ _make_stream_chunk(
+ tool_calls=[
+ _make_tool_call_delta(0, "call-1", "search", '{"q":'),
+ ],
+ ),
+ _make_stream_chunk(
+ tool_calls=[
+ _make_tool_call_delta(0, None, None, '"hello"}'),
+ ],
+ ),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={"prompt_tokens": 10, "completion_tokens": 5},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="search",
+ input='{"q":',
+ ),
+ ],
+ ),
+ (
+ False,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="search",
+ input='"hello"}',
+ ),
+ ],
+ ),
+ (
+ True,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="search",
+ input='{"q":"hello"}',
+ ),
+ ],
+ ),
+ ],
+ )
+
+ @patch("openai.AsyncClient")
+ async def test_stream_usage(self, mock_client_cls: MagicMock) -> None:
+ """Stream usage chunk attaches token counts to final response."""
+ chunks = [
+ _make_stream_chunk(delta_text="X"),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={"prompt_tokens": 50, "completion_tokens": 10},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [TextBlock.model_construct(id=A, text="X")]),
+ (True, [TextBlock.model_construct(id=A, text="X")]),
+ ],
)
- tcs = [b for b in result.content if isinstance(b, ToolCallBlock)]
- self.assertEqual(len(tcs), 1)
- self.assertEqual(tcs[0].id, "call-1")
- self.assertEqual(tcs[0].name, "get_weather")
- self.assertEqual(json.loads(tcs[0].input)["city"], "Beijing")
+ self.assertEqual(responses[-1].usage.input_tokens, 50)
+ self.assertEqual(responses[-1].usage.output_tokens, 10)
# ---------------------------------------------------------------------------
-# Shared _format_tools fixtures
+# _format_tools tests
# ---------------------------------------------------------------------------
_FT_TOOLS = [
diff --git a/tests/model_deepseek_test.py b/tests/model_deepseek_test.py
index cec1fb221..f5562a2e1 100644
--- a/tests/model_deepseek_test.py
+++ b/tests/model_deepseek_test.py
@@ -1,121 +1,523 @@
# -*- coding: utf-8 -*-
# pylint: disable=protected-access
-"""Unit tests for DeepSeekChatModel response parsing.
+"""Unit tests for DeepSeekChatModel with mocked API responses.
-Formatter tests have been moved to tests/formatter_deepseek_test.py.
+Tests cover both non-streaming and streaming modes, verifying that:
+- Non-stream mode returns a single ChatResponse with is_last=True.
+- Stream mode yields n delta ChatResponses (is_last=False) followed by
+ 1 final ChatResponse (is_last=True) with the full accumulated content.
"""
from typing import Any
-from datetime import datetime
import unittest
from unittest import IsolatedAsyncioTestCase
-from unittest.mock import MagicMock
+from unittest.mock import AsyncMock, MagicMock, patch
+
+from utils import AnyString
from agentscope.message import TextBlock, ToolCallBlock, ThinkingBlock
from agentscope.model import DeepSeekChatModel
from agentscope.credential import DeepSeekCredential
from agentscope.tool import ToolChoice
+A = AnyString()
+
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
-def _make_model() -> Any:
+def _make_model(stream: bool = False) -> Any:
return DeepSeekChatModel(
credential=DeepSeekCredential(api_key="test"),
model="deepseek-v4-pro",
- stream=False,
+ stream=stream,
context_size=65_536,
)
+def _mock_completion(
+ text: Any = None,
+ tool_calls: Any = None,
+ reasoning: Any = None,
+ response_id: str = "deepseek-1",
+) -> MagicMock:
+ """Build a mock non-streaming ChatCompletion response."""
+ msg = MagicMock()
+ msg.content = text
+ msg.reasoning_content = reasoning
+ msg.tool_calls = None
+
+ if tool_calls:
+ tc_mocks = []
+ for tc in tool_calls:
+ m = MagicMock()
+ m.id = tc["id"]
+ m.function.name = tc["name"]
+ m.function.arguments = tc["arguments"]
+ tc_mocks.append(m)
+ msg.tool_calls = tc_mocks
+
+ choice = MagicMock()
+ choice.message = msg
+
+ resp = MagicMock()
+ resp.id = response_id
+ resp.choices = [choice]
+ resp.usage.prompt_tokens = 10
+ resp.usage.completion_tokens = 5
+ resp.usage.prompt_cache_hit_tokens = 0
+ return resp
+
+
+def _make_stream_chunk(
+ delta_text: str | None = None,
+ delta_reasoning: str | None = None,
+ tool_calls: list | None = None,
+ response_id: str = "deepseek-1",
+ usage: dict | None = None,
+ has_choices: bool = True,
+) -> MagicMock:
+ """Build a single mock streaming chunk."""
+ chunk = MagicMock()
+ chunk.id = response_id
+
+ if usage:
+ chunk.usage = MagicMock()
+ chunk.usage.prompt_tokens = usage.get("prompt_tokens", 0)
+ chunk.usage.completion_tokens = usage.get("completion_tokens", 0)
+ chunk.usage.prompt_cache_hit_tokens = usage.get(
+ "prompt_cache_hit_tokens",
+ 0,
+ )
+ else:
+ chunk.usage = None
+
+ if has_choices:
+ delta = MagicMock()
+ delta.content = delta_text
+ delta.reasoning_content = delta_reasoning
+ delta.tool_calls = tool_calls
+ choice = MagicMock()
+ choice.delta = delta
+ chunk.choices = [choice]
+ else:
+ chunk.choices = []
+
+ return chunk
+
+
+def _make_tool_call_delta(
+ index: int,
+ tc_id: str | None = None,
+ name: str | None = None,
+ arguments: str | None = None,
+) -> MagicMock:
+ """Build a tool_call delta item for streaming."""
+ tc = MagicMock()
+ tc.index = index
+ tc.id = tc_id
+ tc.function.name = name
+ tc.function.arguments = arguments
+ return tc
+
+
+class _MockAsyncStream:
+ """Mock async stream that acts as an async context manager + iterator."""
+
+ def __init__(self, chunks: list) -> None:
+ self._chunks = chunks
+ self._index = 0
+
+ async def __aenter__(self) -> "_MockAsyncStream":
+ return self
+
+ async def __aexit__(self, *args: Any) -> None:
+ pass
+
+ def __aiter__(self) -> "_MockAsyncStream":
+ return self
+
+ async def __anext__(self) -> Any:
+ if self._index >= len(self._chunks):
+ raise StopAsyncIteration
+ chunk = self._chunks[self._index]
+ self._index += 1
+ return chunk
+
+
# ---------------------------------------------------------------------------
-# Model response parsing tests
+# Non-streaming tests
# ---------------------------------------------------------------------------
-class TestDeepSeekModelParsing(IsolatedAsyncioTestCase):
- """Unit tests for DeepSeekChatModel response parsing."""
+class TestDeepSeekNonStream(IsolatedAsyncioTestCase):
+ """Tests for DeepSeekChatModel in non-streaming mode."""
def setUp(self) -> None:
- """Set up a fresh model instance and start time."""
- self.model = _make_model()
- self.start = datetime.now()
+ self.model = _make_model(stream=False)
+
+ @patch("openai.AsyncClient")
+ async def test_text_response(self, mock_client_cls: MagicMock) -> None:
+ """Non-stream text response returns a single ChatResponse."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(text="Hello world!"),
+ )
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (True, [TextBlock.model_construct(id=A, text="Hello world!")]),
+ )
+ self.assertEqual(result.id, "deepseek-1")
- def _mock_completion(
+ @patch("openai.AsyncClient")
+ async def test_tool_call_response(
self,
- text: Any = None,
- tool_calls: Any = None,
- reasoning: Any = None,
- ) -> "MagicMock":
- """Build a mock DeepSeek API completion response object."""
- msg = MagicMock()
- msg.content = text
- setattr(msg, "reasoning_content", reasoning)
- msg.tool_calls = None
-
- if tool_calls:
- tc_mocks = []
- for tc in tool_calls:
- m = MagicMock()
- m.id = tc["id"]
- m.function.name = tc["name"]
- m.function.arguments = tc["arguments"]
- tc_mocks.append(m)
- msg.tool_calls = tc_mocks
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Non-stream tool call response creates ToolCallBlocks."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(
+ tool_calls=[
+ {
+ "id": "call-1",
+ "name": "get_weather",
+ "arguments": '{"city":"Beijing"}',
+ },
+ {
+ "id": "call-2",
+ "name": "get_time",
+ "arguments": '{"tz":"UTC"}',
+ },
+ ],
+ ),
+ )
+ mock_client_cls.return_value.chat.completions.create = mock_create
- choice = MagicMock()
- choice.message = msg
-
- resp = MagicMock()
- resp.id = "deepseek-1"
- resp.choices = [choice]
- resp.usage.prompt_tokens = 10
- resp.usage.completion_tokens = 5
- return resp
-
- def test_parse_text_response(self) -> None:
- """Parsing a text response creates a TextBlock."""
- resp = self._mock_completion(text="Hello!")
- result = self.model._parse_completion_response(self.start, resp)
- self.assertTrue(result.is_last)
- texts = [b for b in result.content if isinstance(b, TextBlock)]
- self.assertEqual(texts[0].text, "Hello!")
-
- def test_parse_tool_call_response(self) -> None:
- """Parsing a tool-call response creates a ToolCallBlock."""
- resp = self._mock_completion(
- tool_calls=[
- {
- "id": "call-1",
- "name": "get_weather",
- "arguments": '{"city":"Beijing"}',
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="get_weather",
+ input='{"city":"Beijing"}',
+ ),
+ ToolCallBlock(
+ id="call-2",
+ name="get_time",
+ input='{"tz":"UTC"}',
+ ),
+ ],
+ ),
+ )
+
+ @patch("openai.AsyncClient")
+ async def test_thinking_response(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Non-stream response with reasoning creates ThinkingBlock."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(
+ text="The answer is 42.",
+ reasoning="Let me think step by step...",
+ ),
+ )
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Let me think step by step...",
+ ),
+ TextBlock.model_construct(id=A, text="The answer is 42."),
+ ],
+ ),
+ )
+
+
+# ---------------------------------------------------------------------------
+# Streaming tests
+# ---------------------------------------------------------------------------
+
+
+class TestDeepSeekStream(IsolatedAsyncioTestCase):
+ """Tests for DeepSeekChatModel in streaming mode."""
+
+ def setUp(self) -> None:
+ self.model = _make_model(stream=True)
+
+ @patch("openai.AsyncClient")
+ async def test_stream_text_response(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream text yields n deltas (is_last=False) + 1 final
+ (is_last=True) with full content."""
+ chunks = [
+ _make_stream_chunk(delta_text="Hello"),
+ _make_stream_chunk(delta_text=" world"),
+ _make_stream_chunk(delta_text="!"),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={"prompt_tokens": 10, "completion_tokens": 3},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [TextBlock.model_construct(id=A, text="Hello")]),
+ (False, [TextBlock.model_construct(id=A, text=" world")]),
+ (False, [TextBlock.model_construct(id=A, text="!")]),
+ (True, [TextBlock.model_construct(id=A, text="Hello world!")]),
+ ],
+ )
+ self.assertEqual(responses[-1].id, "deepseek-1")
+
+ @patch("openai.AsyncClient")
+ async def test_stream_thinking_and_text(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream with thinking + text yields deltas then final with both."""
+ chunks = [
+ _make_stream_chunk(delta_reasoning="Think"),
+ _make_stream_chunk(delta_reasoning="ing..."),
+ _make_stream_chunk(delta_text="Answer"),
+ _make_stream_chunk(delta_text=" here."),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={"prompt_tokens": 10, "completion_tokens": 8},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [ThinkingBlock.model_construct(id=A, thinking="Think")],
+ ),
+ (
+ False,
+ [ThinkingBlock.model_construct(id=A, thinking="ing...")],
+ ),
+ (False, [TextBlock.model_construct(id=A, text="Answer")]),
+ (False, [TextBlock.model_construct(id=A, text=" here.")]),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Thinking...",
+ ),
+ TextBlock.model_construct(id=A, text="Answer here."),
+ ],
+ ),
+ ],
+ )
+
+ @patch("openai.AsyncClient")
+ async def test_stream_tool_calls(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream tool calls accumulate across chunks into final response."""
+ chunks = [
+ _make_stream_chunk(
+ delta_text=None,
+ tool_calls=[
+ _make_tool_call_delta(0, "call-1", "get_weather", '{"ci'),
+ ],
+ ),
+ _make_stream_chunk(
+ delta_text=None,
+ tool_calls=[
+ _make_tool_call_delta(0, None, None, 'ty":"BJ"}'),
+ ],
+ ),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={"prompt_tokens": 10, "completion_tokens": 5},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="get_weather",
+ input='{"ci',
+ ),
+ ],
+ ),
+ (
+ False,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="get_weather",
+ input='ty":"BJ"}',
+ ),
+ ],
+ ),
+ (
+ True,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="get_weather",
+ input='{"city":"BJ"}',
+ ),
+ ],
+ ),
+ ],
+ )
+
+ @patch("openai.AsyncClient")
+ async def test_stream_text_then_tool_call(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream with text followed by tool call accumulates both."""
+ chunks = [
+ _make_stream_chunk(delta_reasoning="Let me check"),
+ _make_stream_chunk(delta_text="I'll look it up."),
+ _make_stream_chunk(
+ delta_text=None,
+ tool_calls=[
+ _make_tool_call_delta(
+ 0,
+ "call-1",
+ "search",
+ '{"q":"weather"}',
+ ),
+ ],
+ ),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={"prompt_tokens": 15, "completion_tokens": 10},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Let me check",
+ ),
+ ],
+ ),
+ (
+ False,
+ [TextBlock.model_construct(id=A, text="I'll look it up.")],
+ ),
+ (
+ False,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="search",
+ input='{"q":"weather"}',
+ ),
+ ],
+ ),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Let me check",
+ ),
+ TextBlock.model_construct(
+ id=A,
+ text="I'll look it up.",
+ ),
+ ToolCallBlock(
+ id="call-1",
+ name="search",
+ input='{"q":"weather"}',
+ ),
+ ],
+ ),
+ ],
+ )
+
+ @patch("openai.AsyncClient")
+ async def test_stream_usage_in_final(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Usage information is captured and present in final response."""
+ chunks = [
+ _make_stream_chunk(delta_text="Hi"),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={
+ "prompt_tokens": 100,
+ "completion_tokens": 20,
+ "prompt_cache_hit_tokens": 50,
},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [TextBlock.model_construct(id=A, text="Hi")]),
+ (True, [TextBlock.model_construct(id=A, text="Hi")]),
],
)
- result = self.model._parse_completion_response(self.start, resp)
- tcs = [b for b in result.content if isinstance(b, ToolCallBlock)]
- self.assertEqual(len(tcs), 1)
- self.assertEqual(tcs[0].id, "call-1")
- self.assertEqual(tcs[0].name, "get_weather")
-
- def test_parse_thinking_response(self) -> None:
- """Parsing a response with reasoning creates a ThinkingBlock."""
- resp = self._mock_completion(
- text="Answer",
- reasoning="Let me think...",
- )
- result = self.model._parse_completion_response(self.start, resp)
- thinkings = [b for b in result.content if isinstance(b, ThinkingBlock)]
- self.assertEqual(len(thinkings), 1)
- self.assertEqual(thinkings[0].thinking, "Let me think...")
-
- def test_response_id_set(self) -> None:
- """The response ID from the API is stored in the ChatResponse."""
- resp = self._mock_completion(text="Hi")
- result = self.model._parse_completion_response(self.start, resp)
- self.assertEqual(result.id, "deepseek-1")
+ self.assertEqual(responses[-1].usage.input_tokens, 100)
+ self.assertEqual(responses[-1].usage.output_tokens, 20)
+ self.assertEqual(responses[-1].usage.cache_input_tokens, 50)
# ---------------------------------------------------------------------------
diff --git a/tests/model_gemini_test.py b/tests/model_gemini_test.py
index c789e14e0..0faebf275 100644
--- a/tests/model_gemini_test.py
+++ b/tests/model_gemini_test.py
@@ -1,126 +1,317 @@
# -*- coding: utf-8 -*-
# pylint: disable=protected-access
-"""Unit tests for GeminiChatModel response parsing.
+"""Unit tests for GeminiChatModel with mocked API responses.
-Formatter tests have been moved to tests/formatter_gemini_test.py.
+Tests cover both non-streaming and streaming modes.
+Gemini uses google.genai client with async iterator streaming.
"""
import json
from typing import Any
-from datetime import datetime
import unittest
from unittest import IsolatedAsyncioTestCase
-from unittest.mock import MagicMock
+from unittest.mock import AsyncMock, MagicMock, patch
+
+from utils import AnyString
from agentscope.message import TextBlock, ToolCallBlock, ThinkingBlock
from agentscope.model import GeminiChatModel
from agentscope.credential import GeminiCredential
from agentscope.tool import ToolChoice
+A = AnyString()
+
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
-def _make_model() -> Any:
+def _make_model(stream: bool = False) -> Any:
return GeminiChatModel(
credential=GeminiCredential(api_key="test"),
model="gemini-2.5-flash",
- stream=False,
+ stream=stream,
context_size=1_048_576,
)
+def _make_part(
+ text: str | None = None,
+ thought: bool = False,
+ function_call: dict | None = None,
+ thought_signature: Any = None,
+) -> MagicMock:
+ """Build a mock Gemini Part."""
+ part = MagicMock()
+ part.text = text
+ part.thought = thought
+ part.thought_signature = thought_signature
+ if function_call:
+ part.function_call = MagicMock()
+ part.function_call.name = function_call["name"]
+ part.function_call.args = function_call.get("args", {})
+ part.function_call.id = function_call.get("id", "call-1")
+ else:
+ part.function_call = None
+ return part
+
+
+def _mock_completion(
+ parts: list,
+ response_id: str = "resp-gem-1",
+) -> MagicMock:
+ """Build a mock non-streaming Gemini response."""
+ resp = MagicMock()
+ resp.response_id = response_id
+ resp.candidates = [MagicMock()]
+ resp.candidates[0].content = MagicMock()
+ resp.candidates[0].content.parts = parts
+ resp.usage_metadata = MagicMock()
+ resp.usage_metadata.prompt_token_count = 10
+ resp.usage_metadata.candidates_token_count = 5
+ return resp
+
+
+def _make_stream_chunk(
+ parts: list,
+ response_id: str = "resp-gem-1",
+) -> MagicMock:
+ """Build a single mock streaming chunk."""
+ chunk = MagicMock()
+ chunk.response_id = response_id
+ chunk.candidates = [MagicMock()]
+ chunk.candidates[0].content = MagicMock()
+ chunk.candidates[0].content.parts = parts
+ chunk.usage_metadata = MagicMock()
+ chunk.usage_metadata.prompt_token_count = 10
+ chunk.usage_metadata.candidates_token_count = 5
+ return chunk
+
+
+class _MockAsyncStream:
+ """Mock async iterator for Gemini stream."""
+
+ def __init__(self, chunks: list) -> None:
+ self._chunks = chunks
+ self._index = 0
+
+ def __aiter__(self) -> "_MockAsyncStream":
+ return self
+
+ async def __anext__(self) -> Any:
+ if self._index >= len(self._chunks):
+ raise StopAsyncIteration
+ chunk = self._chunks[self._index]
+ self._index += 1
+ return chunk
+
+
# ---------------------------------------------------------------------------
-# Model response parsing tests
+# Non-streaming tests
# ---------------------------------------------------------------------------
-class TestGeminiModelParsing(IsolatedAsyncioTestCase):
- """Unit tests for GeminiChatModel response parsing."""
+class TestGeminiNonStream(IsolatedAsyncioTestCase):
+ """Tests for GeminiChatModel in non-streaming mode."""
def setUp(self) -> None:
- """Set up a fresh model instance and start time."""
- self.model = _make_model()
- self.start = datetime.now()
+ self.model = _make_model(stream=False)
+
+ @patch("google.genai.Client")
+ async def test_text_response(self, mock_client_cls: MagicMock) -> None:
+ """Non-stream text response returns a single ChatResponse."""
+ parts = [_make_part(text="Hello!")]
+ mock_client_cls.return_value.aio.models.generate_content = AsyncMock(
+ return_value=_mock_completion(parts),
+ )
+
+ result = await self.model([])
- def _mock_response(
+ self.assertEqual(
+ (result.is_last, result.content),
+ (True, [TextBlock.model_construct(id=A, text="Hello!")]),
+ )
+
+ @patch("google.genai.Client")
+ async def test_tool_call_response(
self,
- text: Any = None,
- function_calls: Any = None,
- thought: Any = None,
- ) -> "MagicMock":
- """Build a mock Gemini API response object."""
- parts = []
- if thought:
- p = MagicMock()
- p.text = thought
- p.thought = True
- p.function_call = None
- p.thought_signature = None
- parts.append(p)
- if text:
- p = MagicMock()
- p.text = text
- p.thought = False
- p.function_call = None
- p.thought_signature = None
- parts.append(p)
- if function_calls:
- for fc in function_calls:
- p = MagicMock()
- p.text = None
- p.thought = False
- p.thought_signature = None
- p.function_call.name = fc["name"]
- p.function_call.args = fc["args"]
- p.function_call.id = fc.get("id", "call-1")
- parts.append(p)
-
- resp = MagicMock()
- resp.candidates = [MagicMock()]
- resp.candidates[0].content.parts = parts
- resp.usage_metadata = MagicMock()
- resp.usage_metadata.prompt_token_count = 10
- resp.usage_metadata.candidates_token_count = 5
- return resp
-
- def test_parse_text_response(self) -> None:
- """Parsing a text response creates a TextBlock."""
- resp = self._mock_response(text="Hello!")
- result = self.model._parse_completion_response(self.start, resp)
- self.assertTrue(result.is_last)
- texts = [b for b in result.content if isinstance(b, TextBlock)]
- self.assertEqual(texts[0].text, "Hello!")
-
- def test_parse_tool_call_response(self) -> None:
- """Parsing a function_call response creates a ToolCallBlock."""
- resp = self._mock_response(
- function_calls=[
- {
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Non-stream tool call response creates ToolCallBlocks."""
+ parts = [
+ _make_part(
+ function_call={
"name": "get_weather",
- "args": {"city": "Beijing"},
+ "args": {"city": "Tokyo"},
"id": "call-1",
},
+ ),
+ ]
+ mock_client_cls.return_value.aio.models.generate_content = AsyncMock(
+ return_value=_mock_completion(parts),
+ )
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="get_weather",
+ input=json.dumps(
+ {"city": "Tokyo"},
+ ensure_ascii=False,
+ ),
+ ),
+ ],
+ ),
+ )
+
+ @patch("google.genai.Client")
+ async def test_thinking_response(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Non-stream response with reasoning creates ThinkingBlock."""
+ parts = [
+ _make_part(text="Let me think...", thought=True),
+ _make_part(text="Answer"),
+ ]
+ mock_client_cls.return_value.aio.models.generate_content = AsyncMock(
+ return_value=_mock_completion(parts),
+ )
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Let me think...",
+ ),
+ TextBlock.model_construct(id=A, text="Answer"),
+ ],
+ ),
+ )
+
+
+# ---------------------------------------------------------------------------
+# Streaming tests
+# ---------------------------------------------------------------------------
+
+
+class TestGeminiStream(IsolatedAsyncioTestCase):
+ """Tests for GeminiChatModel in streaming mode."""
+
+ def setUp(self) -> None:
+ self.model = _make_model(stream=True)
+
+ @patch("google.genai.Client")
+ async def test_stream_text(self, mock_client_cls: MagicMock) -> None:
+ """Stream text yields n deltas + 1 final with full content."""
+ chunks = [
+ _make_stream_chunk([_make_part(text="Hello")]),
+ _make_stream_chunk([_make_part(text=" world")]),
+ ]
+ mock_client_cls.return_value.aio.models.generate_content_stream = (
+ AsyncMock(return_value=_MockAsyncStream(chunks))
+ )
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [TextBlock.model_construct(id=A, text="Hello")]),
+ (False, [TextBlock.model_construct(id=A, text=" world")]),
+ (True, [TextBlock.model_construct(id=A, text="Hello world")]),
+ ],
+ )
+
+ @patch("google.genai.Client")
+ async def test_stream_thinking_and_text(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream thinking + text yields deltas then accumulated final."""
+ chunks = [
+ _make_stream_chunk([_make_part(text="Think", thought=True)]),
+ _make_stream_chunk([_make_part(text="Answer")]),
+ ]
+ mock_client_cls.return_value.aio.models.generate_content_stream = (
+ AsyncMock(return_value=_MockAsyncStream(chunks))
+ )
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [ThinkingBlock.model_construct(id=A, thinking="Think")],
+ ),
+ (False, [TextBlock.model_construct(id=A, text="Answer")]),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(id=A, thinking="Think"),
+ TextBlock.model_construct(id=A, text="Answer"),
+ ],
+ ),
],
)
- result = self.model._parse_completion_response(self.start, resp)
- tcs = [b for b in result.content if isinstance(b, ToolCallBlock)]
- self.assertEqual(len(tcs), 1)
- self.assertEqual(tcs[0].name, "get_weather")
- self.assertEqual(json.loads(tcs[0].input)["city"], "Beijing")
- def test_parse_thinking_response(self) -> None:
- """Parsing a response with thought creates a ThinkingBlock."""
- resp = self._mock_response(thought="Let me think...", text="Answer")
- result = self.model._parse_completion_response(self.start, resp)
- thinkings = [b for b in result.content if isinstance(b, ThinkingBlock)]
- self.assertEqual(len(thinkings), 1)
- self.assertEqual(thinkings[0].thinking, "Let me think...")
+ @patch("google.genai.Client")
+ async def test_stream_tool_call(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream tool call yields delta then final with same ToolCallBlock."""
+ chunks = [
+ _make_stream_chunk(
+ [
+ _make_part(
+ function_call={
+ "name": "search",
+ "args": {"q": "test"},
+ "id": "call-1",
+ },
+ ),
+ ],
+ ),
+ ]
+ mock_client_cls.return_value.aio.models.generate_content_stream = (
+ AsyncMock(return_value=_MockAsyncStream(chunks))
+ )
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ tool_block = ToolCallBlock(
+ id="call-1",
+ name="search",
+ input=json.dumps({"q": "test"}, ensure_ascii=False),
+ )
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [tool_block]),
+ (True, [tool_block]),
+ ],
+ )
# ---------------------------------------------------------------------------
-# Shared _format_tools fixtures
+# _format_tools tests
# ---------------------------------------------------------------------------
_FT_TOOLS = [
@@ -150,7 +341,6 @@ _FT_TOOLS = [
},
]
-
_FT_TOOLS_GEMINI = [
{
"function_declarations": [
@@ -177,7 +367,6 @@ _FT_TOOLS_GEMINI = [
]
-# pylint: disable=protected-access
class TestGeminiFormatTools(unittest.TestCase):
"""Tests for GeminiChatModel._format_tools."""
@@ -240,7 +429,7 @@ class TestGeminiFormatTools(unittest.TestCase):
def test_tools_filtered(self) -> None:
"""When tool_choice.tools is set, only those tools are included."""
- fmt_tools, fmt_choice = self.model._format_tools(
+ fmt_tools, _ = self.model._format_tools(
_FT_TOOLS,
ToolChoice(mode="auto", tools=["get_weather"]),
)
@@ -249,10 +438,6 @@ class TestGeminiFormatTools(unittest.TestCase):
fmt_tools[0]["function_declarations"][0]["name"],
"get_weather",
)
- self.assertEqual(
- fmt_choice,
- {"function_calling_config": {"mode": "AUTO"}},
- )
def test_no_tool_choice(self) -> None:
"""Without tool_choice, returns function_declarations and None."""
diff --git a/tests/model_moonshot_test.py b/tests/model_moonshot_test.py
index 9ee817fff..92ffabc7a 100644
--- a/tests/model_moonshot_test.py
+++ b/tests/model_moonshot_test.py
@@ -1,124 +1,388 @@
# -*- coding: utf-8 -*-
# pylint: disable=protected-access
-"""Unit tests for MoonshotChatModel response parsing.
+"""Unit tests for MoonshotChatModel with mocked API responses.
-Formatter tests have been moved to tests/formatter_moonshot_test.py.
+Tests cover both non-streaming and streaming modes.
"""
-import json
from typing import Any
-from datetime import datetime
import unittest
from unittest import IsolatedAsyncioTestCase
-from unittest.mock import MagicMock
+from unittest.mock import AsyncMock, MagicMock, patch
+
+from utils import AnyString
from agentscope.message import TextBlock, ToolCallBlock, ThinkingBlock
from agentscope.model import MoonshotChatModel
from agentscope.credential import MoonshotCredential
from agentscope.tool import ToolChoice
+A = AnyString()
+
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
-def _make_model() -> Any:
+def _make_model(stream: bool = False) -> Any:
return MoonshotChatModel(
credential=MoonshotCredential(api_key="test"),
model="kimi-k2-5",
- stream=False,
+ stream=stream,
context_size=131_072,
)
+def _mock_completion(
+ text: Any = None,
+ tool_calls: Any = None,
+ reasoning: Any = None,
+ response_id: str = "kimi-1",
+) -> MagicMock:
+ """Build a mock non-streaming ChatCompletion response."""
+ msg = MagicMock()
+ msg.content = text
+ msg.reasoning_content = reasoning
+ msg.tool_calls = None
+
+ if tool_calls:
+ tc_mocks = []
+ for tc in tool_calls:
+ m = MagicMock()
+ m.id = tc["id"]
+ m.function.name = tc["name"]
+ m.function.arguments = tc["arguments"]
+ tc_mocks.append(m)
+ msg.tool_calls = tc_mocks
+
+ choice = MagicMock()
+ choice.message = msg
+
+ resp = MagicMock()
+ resp.id = response_id
+ resp.choices = [choice]
+ resp.usage.prompt_tokens = 10
+ resp.usage.completion_tokens = 5
+ resp.usage.cached_tokens = 0
+ return resp
+
+
+def _make_stream_chunk(
+ delta_text: str | None = None,
+ delta_reasoning: str | None = None,
+ tool_calls: list | None = None,
+ response_id: str = "kimi-1",
+ usage: dict | None = None,
+ has_choices: bool = True,
+) -> MagicMock:
+ """Build a single mock streaming chunk."""
+ chunk = MagicMock()
+ chunk.id = response_id
+
+ if usage:
+ chunk.usage = MagicMock()
+ chunk.usage.prompt_tokens = usage.get("prompt_tokens", 0)
+ chunk.usage.completion_tokens = usage.get("completion_tokens", 0)
+ chunk.usage.cached_tokens = usage.get("cached_tokens", 0)
+ else:
+ chunk.usage = None
+
+ if has_choices:
+ delta = MagicMock()
+ delta.content = delta_text
+ delta.reasoning_content = delta_reasoning
+ delta.tool_calls = tool_calls
+ choice = MagicMock()
+ choice.delta = delta
+ chunk.choices = [choice]
+ else:
+ chunk.choices = []
+
+ return chunk
+
+
+def _make_tool_call_delta(
+ index: int,
+ tc_id: str | None = None,
+ name: str | None = None,
+ arguments: str | None = None,
+) -> MagicMock:
+ tc = MagicMock()
+ tc.index = index
+ tc.id = tc_id
+ tc.function.name = name
+ tc.function.arguments = arguments
+ return tc
+
+
+class _MockAsyncStream:
+ """Mock async stream (context manager + async iterator)."""
+
+ def __init__(self, chunks: list) -> None:
+ self._chunks = chunks
+ self._index = 0
+
+ async def __aenter__(self) -> "_MockAsyncStream":
+ return self
+
+ async def __aexit__(self, *args: Any) -> None:
+ pass
+
+ def __aiter__(self) -> "_MockAsyncStream":
+ return self
+
+ async def __anext__(self) -> Any:
+ if self._index >= len(self._chunks):
+ raise StopAsyncIteration
+ chunk = self._chunks[self._index]
+ self._index += 1
+ return chunk
+
+
# ---------------------------------------------------------------------------
-# Model response parsing tests
+# Non-streaming tests
# ---------------------------------------------------------------------------
-class TestMoonshotModelParsing(IsolatedAsyncioTestCase):
- """Unit tests for MoonshotChatModel response parsing."""
+class TestMoonshotNonStream(IsolatedAsyncioTestCase):
+ """Tests for MoonshotChatModel in non-streaming mode."""
def setUp(self) -> None:
- """Set up a fresh model instance and start time."""
- self.model = _make_model()
- self.start = datetime.now()
+ self.model = _make_model(stream=False)
+
+ @patch("openai.AsyncClient")
+ async def test_text_response(self, mock_client_cls: MagicMock) -> None:
+ """Non-stream text response returns a single ChatResponse."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(text="Hello!"),
+ )
+ mock_client_cls.return_value.chat.completions.create = mock_create
- def _mock_completion(
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (True, [TextBlock.model_construct(id=A, text="Hello!")]),
+ )
+ self.assertEqual(result.id, "kimi-1")
+
+ @patch("openai.AsyncClient")
+ async def test_tool_call_response(
self,
- text: Any = None,
- tool_calls: Any = None,
- reasoning: Any = None,
- ) -> "MagicMock":
- """Build a mock Moonshot API completion response object."""
- msg = MagicMock()
- msg.content = text
- setattr(msg, "reasoning_content", reasoning)
- msg.tool_calls = None
-
- if tool_calls:
- tc_mocks = []
- for tc in tool_calls:
- m = MagicMock()
- m.id = tc["id"]
- m.function.name = tc["name"]
- m.function.arguments = tc["arguments"]
- tc_mocks.append(m)
- msg.tool_calls = tc_mocks
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Non-stream tool call response creates ToolCallBlocks."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(
+ tool_calls=[
+ {
+ "id": "call-1",
+ "name": "get_weather",
+ "arguments": '{"city":"Shanghai"}',
+ },
+ ],
+ ),
+ )
+ mock_client_cls.return_value.chat.completions.create = mock_create
- choice = MagicMock()
- choice.message = msg
-
- resp = MagicMock()
- resp.id = "kimi-1"
- resp.choices = [choice]
- resp.usage.prompt_tokens = 10
- resp.usage.completion_tokens = 5
- return resp
-
- def test_parse_text_response(self) -> None:
- """Parsing a text response creates a TextBlock."""
- resp = self._mock_completion(text="Hello!")
- result = self.model._parse_completion_response(self.start, resp)
- self.assertTrue(result.is_last)
- texts = [b for b in result.content if isinstance(b, TextBlock)]
- self.assertEqual(texts[0].text, "Hello!")
-
- def test_parse_tool_call_response(self) -> None:
- """Parsing a tool-call response creates a ToolCallBlock."""
- resp = self._mock_completion(
- tool_calls=[
- {
- "id": "call-1",
- "name": "get_weather",
- "arguments": '{"city":"Beijing"}',
- },
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="get_weather",
+ input='{"city":"Shanghai"}',
+ ),
+ ],
+ ),
+ )
+
+ @patch("openai.AsyncClient")
+ async def test_thinking_response(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Non-stream response with reasoning creates ThinkingBlock."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(
+ text="42",
+ reasoning="Step by step...",
+ ),
+ )
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Step by step...",
+ ),
+ TextBlock.model_construct(id=A, text="42"),
+ ],
+ ),
+ )
+
+
+# ---------------------------------------------------------------------------
+# Streaming tests
+# ---------------------------------------------------------------------------
+
+
+class TestMoonshotStream(IsolatedAsyncioTestCase):
+ """Tests for MoonshotChatModel in streaming mode."""
+
+ def setUp(self) -> None:
+ self.model = _make_model(stream=True)
+
+ @patch("openai.AsyncClient")
+ async def test_stream_text(self, mock_client_cls: MagicMock) -> None:
+ """Stream text yields n deltas (is_last=False) + 1 final
+ (is_last=True) with full content."""
+ chunks = [
+ _make_stream_chunk(delta_text="Hi"),
+ _make_stream_chunk(delta_text=" there"),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={"prompt_tokens": 10, "completion_tokens": 2},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [TextBlock.model_construct(id=A, text="Hi")]),
+ (False, [TextBlock.model_construct(id=A, text=" there")]),
+ (True, [TextBlock.model_construct(id=A, text="Hi there")]),
+ ],
+ )
+
+ @patch("openai.AsyncClient")
+ async def test_stream_thinking_then_text(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Moonshot yields thinking chunks separately before text."""
+ chunks = [
+ _make_stream_chunk(delta_reasoning="Let me"),
+ _make_stream_chunk(delta_reasoning=" think"),
+ _make_stream_chunk(delta_text="Result"),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={"prompt_tokens": 10, "completion_tokens": 5},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [ThinkingBlock.model_construct(id=A, thinking="Let me")],
+ ),
+ (
+ False,
+ [ThinkingBlock.model_construct(id=A, thinking=" think")],
+ ),
+ (False, [TextBlock.model_construct(id=A, text="Result")]),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Let me think",
+ ),
+ TextBlock.model_construct(id=A, text="Result"),
+ ],
+ ),
+ ],
+ )
+
+ @patch("openai.AsyncClient")
+ async def test_stream_tool_calls(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream tool calls accumulate across chunks into final response."""
+ chunks = [
+ _make_stream_chunk(
+ tool_calls=[
+ _make_tool_call_delta(0, "call-1", "search", '{"q":'),
+ ],
+ ),
+ _make_stream_chunk(
+ tool_calls=[
+ _make_tool_call_delta(0, None, None, '"test"}'),
+ ],
+ ),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={"prompt_tokens": 10, "completion_tokens": 5},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="search",
+ input='{"q":',
+ ),
+ ],
+ ),
+ (
+ False,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="search",
+ input='"test"}',
+ ),
+ ],
+ ),
+ (
+ True,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="search",
+ input='{"q":"test"}',
+ ),
+ ],
+ ),
],
)
- result = self.model._parse_completion_response(self.start, resp)
- tcs = [b for b in result.content if isinstance(b, ToolCallBlock)]
- self.assertEqual(len(tcs), 1)
- self.assertEqual(tcs[0].id, "call-1")
- self.assertEqual(tcs[0].name, "get_weather")
- self.assertEqual(json.loads(tcs[0].input)["city"], "Beijing")
-
- def test_parse_thinking_response(self) -> None:
- """Parsing a response with reasoning creates a ThinkingBlock."""
- resp = self._mock_completion(text="Answer", reasoning="Thinking...")
- result = self.model._parse_completion_response(self.start, resp)
- thinkings = [b for b in result.content if isinstance(b, ThinkingBlock)]
- self.assertEqual(len(thinkings), 1)
- self.assertEqual(thinkings[0].thinking, "Thinking...")
-
- def test_response_id_set(self) -> None:
- """The response ID from the API is stored in the ChatResponse."""
- resp = self._mock_completion(text="Hi")
- result = self.model._parse_completion_response(self.start, resp)
- self.assertEqual(result.id, "kimi-1")
# ---------------------------------------------------------------------------
-# Shared _format_tools fixtures
+# _format_tools tests
# ---------------------------------------------------------------------------
_FT_TOOLS = [
diff --git a/tests/model_ollama_test.py b/tests/model_ollama_test.py
index 4eb89f5e8..fc7dd960a 100644
--- a/tests/model_ollama_test.py
+++ b/tests/model_ollama_test.py
@@ -1,110 +1,315 @@
# -*- coding: utf-8 -*-
# pylint: disable=protected-access
-"""Unit tests for OllamaChatModel response parsing.
+"""Unit tests for OllamaChatModel with mocked API responses.
-Formatter tests have been moved to tests/formatter_ollama_test.py.
+Tests cover both non-streaming and streaming modes.
+Ollama uses ollama.AsyncClient with async iterator streaming.
"""
import json
from typing import Any
-from datetime import datetime
import unittest
from unittest import IsolatedAsyncioTestCase
-from unittest.mock import MagicMock
+from unittest.mock import AsyncMock, MagicMock, patch
+
+from utils import AnyString
from agentscope.message import TextBlock, ToolCallBlock, ThinkingBlock
from agentscope.model import OllamaChatModel
from agentscope.tool import ToolChoice
+A = AnyString()
+
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
-def _make_model() -> Any:
+def _make_model(stream: bool = False) -> Any:
return OllamaChatModel(
model="qwen3:8b",
- stream=False,
+ stream=stream,
context_size=40_960,
)
+def _mock_completion(
+ content: str = "",
+ thinking: str | None = None,
+ tool_calls: list | None = None,
+) -> MagicMock:
+ """Build a mock non-streaming Ollama response."""
+ msg = MagicMock()
+ msg.content = content
+ msg.thinking = thinking
+
+ if tool_calls:
+ tc_mocks = []
+ for tc in tool_calls:
+ m = MagicMock()
+ m.function.name = tc["name"]
+ m.function.arguments = tc["args"]
+ tc_mocks.append(m)
+ msg.tool_calls = tc_mocks
+ else:
+ msg.tool_calls = None
+
+ resp = MagicMock()
+ resp.message = msg
+ resp.prompt_eval_count = 10
+ resp.eval_count = 5
+ resp.id = None
+ return resp
+
+
+def _make_stream_chunk(
+ content: str = "",
+ thinking: str | None = None,
+ tool_calls: list | None = None,
+) -> MagicMock:
+ """Build a single mock Ollama streaming chunk."""
+ msg = MagicMock()
+ msg.content = content
+ msg.thinking = thinking
+
+ if tool_calls:
+ tc_mocks = []
+ for tc in tool_calls:
+ m = MagicMock()
+ m.function.name = tc["name"]
+ m.function.arguments = tc["args"]
+ tc_mocks.append(m)
+ msg.tool_calls = tc_mocks
+ else:
+ msg.tool_calls = None
+
+ chunk = MagicMock()
+ chunk.message = msg
+ chunk.prompt_eval_count = 10
+ chunk.eval_count = 5
+ chunk.id = None
+ return chunk
+
+
+class _MockAsyncStream:
+ """Mock async iterator for Ollama stream."""
+
+ def __init__(self, chunks: list) -> None:
+ self._chunks = chunks
+ self._index = 0
+
+ def __aiter__(self) -> "_MockAsyncStream":
+ return self
+
+ async def __anext__(self) -> Any:
+ if self._index >= len(self._chunks):
+ raise StopAsyncIteration
+ chunk = self._chunks[self._index]
+ self._index += 1
+ return chunk
+
+
# ---------------------------------------------------------------------------
-# Model response parsing tests
+# Non-streaming tests
# ---------------------------------------------------------------------------
-class TestOllamaModelParsing(IsolatedAsyncioTestCase):
- """Unit tests for OllamaChatModel response parsing."""
+class TestOllamaNonStream(IsolatedAsyncioTestCase):
+ """Tests for OllamaChatModel in non-streaming mode."""
def setUp(self) -> None:
- """Set up a fresh model instance and start time."""
- self.model = _make_model()
- self.start = datetime.now()
+ self.model = _make_model(stream=False)
+
+ @patch("ollama.AsyncClient")
+ async def test_text_response(self, mock_client_cls: MagicMock) -> None:
+ """Non-stream text response returns a single ChatResponse."""
+ mock_client_cls.return_value.chat = AsyncMock(
+ return_value=_mock_completion(content="Hello!"),
+ )
+
+ result = await self.model([])
- def _mock_response(
+ self.assertEqual(
+ (result.is_last, result.content),
+ (True, [TextBlock.model_construct(id=A, text="Hello!")]),
+ )
+
+ @patch("ollama.AsyncClient")
+ async def test_tool_call_response(
self,
- content: Any = None,
- tool_calls: Any = None,
- thinking: Any = None,
- ) -> "MagicMock":
- """Build a mock Ollama API response object."""
- msg = MagicMock()
- msg.content = content or ""
- setattr(msg, "thinking", thinking)
-
- if tool_calls:
- tc_mocks = []
- for tc in tool_calls:
- m = MagicMock()
- m.function.name = tc["name"]
- m.function.arguments = tc["args"]
- tc_mocks.append(m)
- msg.tool_calls = tc_mocks
- else:
- msg.tool_calls = None
-
- resp = MagicMock()
- resp.message = msg
- resp.prompt_eval_count = 10
- resp.eval_count = 5
- setattr(resp, "id", "ollama-1")
- return resp
-
- async def test_parse_text_response(self) -> None:
- """Parsing a text response creates a TextBlock."""
- resp = self._mock_response(content="Hello!")
- result = await self.model._parse_completion_response(self.start, resp)
- self.assertTrue(result.is_last)
- texts = [b for b in result.content if isinstance(b, TextBlock)]
- self.assertEqual(texts[0].text, "Hello!")
-
- async def test_parse_tool_call_response(self) -> None:
+ mock_client_cls: MagicMock,
+ ) -> None:
"""Parsing a tool-call response creates a ToolCallBlock."""
- resp = self._mock_response(
- tool_calls=[
- {"name": "get_weather", "args": {"city": "Beijing"}},
+ mock_client_cls.return_value.chat = AsyncMock(
+ return_value=_mock_completion(
+ tool_calls=[
+ {"name": "get_weather", "args": {"city": "SH"}},
+ ],
+ ),
+ )
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ToolCallBlock(
+ id="0_get_weather",
+ name="get_weather",
+ input=json.dumps({"city": "SH"}),
+ ),
+ ],
+ ),
+ )
+
+ @patch("ollama.AsyncClient")
+ async def test_thinking_response(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Non-stream thinking plus text returns ThinkingBlock then
+ TextBlock."""
+ mock_client_cls.return_value.chat = AsyncMock(
+ return_value=_mock_completion(
+ content="Answer",
+ thinking="Let me think...",
+ ),
+ )
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Let me think...",
+ ),
+ TextBlock.model_construct(id=A, text="Answer"),
+ ],
+ ),
+ )
+
+
+# ---------------------------------------------------------------------------
+# Streaming tests
+# ---------------------------------------------------------------------------
+
+
+class TestOllamaStream(IsolatedAsyncioTestCase):
+ """Tests for OllamaChatModel in streaming mode."""
+
+ def setUp(self) -> None:
+ self.model = _make_model(stream=True)
+
+ @patch("ollama.AsyncClient")
+ async def test_stream_text(self, mock_client_cls: MagicMock) -> None:
+ """Stream text yields deltas then final with full content."""
+ chunks = [
+ _make_stream_chunk(content="Hi"),
+ _make_stream_chunk(content=" there"),
+ ]
+ mock_client_cls.return_value.chat = AsyncMock(
+ return_value=_MockAsyncStream(chunks),
+ )
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [TextBlock.model_construct(id=A, text="Hi")]),
+ (False, [TextBlock.model_construct(id=A, text=" there")]),
+ (True, [TextBlock.model_construct(id=A, text="Hi there")]),
],
)
- result = await self.model._parse_completion_response(self.start, resp)
- tcs = [b for b in result.content if isinstance(b, ToolCallBlock)]
- self.assertEqual(len(tcs), 1)
- self.assertEqual(tcs[0].name, "get_weather")
- self.assertIn("city", json.loads(tcs[0].input))
-
- async def test_parse_thinking_response(self) -> None:
- """Parsing a response with thinking creates a ThinkingBlock."""
- resp = self._mock_response(
- content="Answer",
- thinking="Let me think...",
+
+ @patch("ollama.AsyncClient")
+ async def test_stream_thinking_and_text(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream thinking and text deltas then final with accumulated
+ content."""
+ chunks = [
+ _make_stream_chunk(thinking="Think step"),
+ _make_stream_chunk(content="Result"),
+ ]
+ mock_client_cls.return_value.chat = AsyncMock(
+ return_value=_MockAsyncStream(chunks),
+ )
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Think step",
+ ),
+ ],
+ ),
+ (False, [TextBlock.model_construct(id=A, text="Result")]),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Think step",
+ ),
+ TextBlock.model_construct(id=A, text="Result"),
+ ],
+ ),
+ ],
+ )
+
+ @patch("ollama.AsyncClient")
+ async def test_stream_tool_call(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream tool-call chunk yields delta then final with same
+ ToolCallBlock."""
+ chunks = [
+ _make_stream_chunk(
+ tool_calls=[
+ {"name": "search", "args": {"q": "hello"}},
+ ],
+ ),
+ ]
+ mock_client_cls.return_value.chat = AsyncMock(
+ return_value=_MockAsyncStream(chunks),
+ )
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ tool_block = ToolCallBlock(
+ id="0_search",
+ name="search",
+ input=json.dumps({"q": "hello"}),
+ )
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [tool_block]),
+ (True, [tool_block]),
+ ],
)
- result = await self.model._parse_completion_response(self.start, resp)
- thinkings = [b for b in result.content if isinstance(b, ThinkingBlock)]
- self.assertEqual(thinkings[0].thinking, "Let me think...")
# ---------------------------------------------------------------------------
-# Shared _format_tools fixtures
+# _format_tools tests
# ---------------------------------------------------------------------------
_FT_TOOLS = [
@@ -139,17 +344,16 @@ class TestOllamaFormatTools(unittest.TestCase):
"""Tests for OllamaChatModel._format_tools."""
def setUp(self) -> None:
- """Set up model instance."""
self.model = _make_model()
def test_tools_forwarded_no_choice(self) -> None:
- """Without tool_choice, returns tools unchanged and None."""
+ """Tools are forwarded unchanged when tool_choice is None."""
fmt_tools, fmt_choice = self.model._format_tools(_FT_TOOLS, None)
self.assertEqual(fmt_tools, _FT_TOOLS)
self.assertIsNone(fmt_choice)
def test_tools_filtered(self) -> None:
- """When tool_choice.tools is set, only those tools are included."""
+ """ToolChoice with tools list filters to matching function names."""
fmt_tools, fmt_choice = self.model._format_tools(
_FT_TOOLS,
ToolChoice(mode="auto", tools=["get_weather"]),
diff --git a/tests/model_openai_chat_test.py b/tests/model_openai_chat_test.py
index 0ba0efbe0..9058b5ff2 100644
--- a/tests/model_openai_chat_test.py
+++ b/tests/model_openai_chat_test.py
@@ -1,20 +1,25 @@
# -*- coding: utf-8 -*-
# pylint: disable=protected-access
-"""Unit tests for OpenAIChatModel response parsing.
+"""Unit tests for OpenAIChatModel with mocked API responses.
-Formatter tests have been moved to tests/formatter_openai_chat_test.py.
+Tests cover both non-streaming and streaming modes, verifying that:
+- Non-stream mode returns a single ChatResponse with is_last=True.
+- Stream mode yields n delta ChatResponses (is_last=False) followed by
+ 1 final ChatResponse (is_last=True) with the full accumulated content.
"""
-import json
from typing import Any
-from datetime import datetime
import unittest
from unittest import IsolatedAsyncioTestCase
-from unittest.mock import MagicMock
+from unittest.mock import AsyncMock, MagicMock, patch
+
+from utils import AnyString
from agentscope.message import TextBlock, ToolCallBlock, ThinkingBlock
from agentscope.model import OpenAIChatModel
-from agentscope.tool import ToolChoice
from agentscope.credential import OpenAICredential
+from agentscope.tool import ToolChoice
+
+A = AnyString()
# ---------------------------------------------------------------------------
@@ -22,106 +27,399 @@ from agentscope.credential import OpenAICredential
# ---------------------------------------------------------------------------
-def _make_model() -> Any:
+def _make_model(stream: bool = False) -> Any:
return OpenAIChatModel(
credential=OpenAICredential(api_key="test"),
model="gpt-4o",
- stream=False,
+ stream=stream,
context_size=128_000,
)
+def _mock_completion(
+ text: Any = None,
+ tool_calls: Any = None,
+ reasoning: Any = None,
+ response_id: str = "resp-1",
+) -> MagicMock:
+ """Build a mock non-streaming ChatCompletion response."""
+ msg = MagicMock()
+ msg.content = text
+ msg.reasoning_content = reasoning
+ msg.reasoning = None
+ msg.tool_calls = None
+
+ if tool_calls:
+ tc_mocks = []
+ for tc in tool_calls:
+ m = MagicMock()
+ m.id = tc["id"]
+ m.function.name = tc["name"]
+ m.function.arguments = tc["arguments"]
+ tc_mocks.append(m)
+ msg.tool_calls = tc_mocks
+
+ choice = MagicMock()
+ choice.message = msg
+
+ resp = MagicMock()
+ resp.id = response_id
+ resp.choices = [choice]
+ resp.usage.prompt_tokens = 10
+ resp.usage.completion_tokens = 5
+ resp.usage.prompt_tokens_details = None
+ return resp
+
+
+def _make_stream_chunk(
+ delta_text: str | None = None,
+ delta_reasoning: str | None = None,
+ tool_calls: list | None = None,
+ response_id: str = "resp-1",
+ usage: dict | None = None,
+ has_choices: bool = True,
+) -> MagicMock:
+ """Build a single mock streaming chunk."""
+ chunk = MagicMock()
+ chunk.id = response_id
+
+ if usage:
+ chunk.usage = MagicMock()
+ chunk.usage.prompt_tokens = usage.get("prompt_tokens", 0)
+ chunk.usage.completion_tokens = usage.get("completion_tokens", 0)
+ chunk.usage.prompt_tokens_details = None
+ else:
+ chunk.usage = None
+
+ if has_choices:
+ delta = MagicMock()
+ delta.content = delta_text
+ delta.reasoning_content = delta_reasoning
+ delta.reasoning = None
+ delta.tool_calls = tool_calls
+ choice = MagicMock()
+ choice.delta = delta
+ chunk.choices = [choice]
+ else:
+ chunk.choices = []
+
+ return chunk
+
+
+def _make_tool_call_delta(
+ index: int,
+ tc_id: str | None = None,
+ name: str | None = None,
+ arguments: str | None = None,
+) -> MagicMock:
+ """Build a tool_call delta item for streaming."""
+ tc = MagicMock()
+ tc.index = index
+ tc.id = tc_id
+ tc.function.name = name
+ tc.function.arguments = arguments
+ return tc
+
+
+class _MockAsyncStream:
+ """Mock async stream that acts as an async context manager + iterator."""
+
+ def __init__(self, chunks: list) -> None:
+ self._chunks = chunks
+ self._index = 0
+
+ async def __aenter__(self) -> "_MockAsyncStream":
+ return self
+
+ async def __aexit__(self, *args: Any) -> None:
+ pass
+
+ def __aiter__(self) -> "_MockAsyncStream":
+ return self
+
+ async def __anext__(self) -> Any:
+ if self._index >= len(self._chunks):
+ raise StopAsyncIteration
+ chunk = self._chunks[self._index]
+ self._index += 1
+ return chunk
+
+
# ---------------------------------------------------------------------------
-# Model response parsing tests
+# Non-streaming tests
# ---------------------------------------------------------------------------
-class TestOpenAIChatModelParsing(IsolatedAsyncioTestCase):
- """Unit tests for OpenAIChatModel response parsing."""
+class TestOpenAIChatNonStream(IsolatedAsyncioTestCase):
+ """Tests for OpenAIChatModel in non-streaming mode."""
def setUp(self) -> None:
- """Set up a fresh model instance and start time."""
- self.model = _make_model()
- self.start = datetime.now()
+ self.model = _make_model(stream=False)
+
+ @patch("openai.AsyncClient")
+ async def test_text_response(self, mock_client_cls: MagicMock) -> None:
+ """Non-stream text response returns a single ChatResponse."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(text="Hello world!"),
+ )
+ mock_client_cls.return_value.chat.completions.create = mock_create
- def _mock_completion(
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (True, [TextBlock.model_construct(id=A, text="Hello world!")]),
+ )
+ self.assertEqual(result.id, "resp-1")
+
+ @patch("openai.AsyncClient")
+ async def test_tool_call_response(
self,
- text: Any = None,
- tool_calls: Any = None,
- reasoning: Any = None,
- ) -> "MagicMock":
- """Build a minimal ChatCompletion mock."""
- msg = MagicMock()
- msg.content = text
- msg.tool_calls = None
- setattr(msg, "reasoning_content", reasoning)
- setattr(msg, "reasoning", None)
-
- if tool_calls:
- tc_mocks = []
- for tc in tool_calls:
- m = MagicMock()
- m.id = tc["id"]
- m.function.name = tc["name"]
- m.function.arguments = tc["arguments"]
- tc_mocks.append(m)
- msg.tool_calls = tc_mocks
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Non-stream tool call response creates ToolCallBlocks."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(
+ tool_calls=[
+ {
+ "id": "call-1",
+ "name": "get_weather",
+ "arguments": '{"city":"Beijing"}',
+ },
+ ],
+ ),
+ )
+ mock_client_cls.return_value.chat.completions.create = mock_create
- choice = MagicMock()
- choice.message = msg
- choice.delta = msg
-
- resp = MagicMock()
- resp.id = "resp-1"
- resp.choices = [choice]
- resp.usage.prompt_tokens = 10
- resp.usage.completion_tokens = 5
- return resp
-
- def test_parse_text_response(self) -> None:
- """Parsing a text response creates a TextBlock."""
- resp = self._mock_completion(text="Hello!")
- result = self.model._parse_completion_response(self.start, resp)
- self.assertTrue(result.is_last)
- texts = [b for b in result.content if isinstance(b, TextBlock)]
- self.assertEqual(len(texts), 1)
- self.assertEqual(texts[0].text, "Hello!")
-
- def test_parse_tool_call_response(self) -> None:
- """Parsing a tool-call response creates a ToolCallBlock."""
- resp = self._mock_completion(
- tool_calls=[
- {
- "id": "call-1",
- "name": "get_weather",
- "arguments": '{"city":"Beijing"}',
- },
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="get_weather",
+ input='{"city":"Beijing"}',
+ ),
+ ],
+ ),
+ )
+
+ @patch("openai.AsyncClient")
+ async def test_thinking_response(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Non-stream response with reasoning creates ThinkingBlock."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(
+ text="The answer is 42.",
+ reasoning="Let me think step by step...",
+ ),
+ )
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Let me think step by step...",
+ ),
+ TextBlock.model_construct(id=A, text="The answer is 42."),
+ ],
+ ),
+ )
+
+
+# ---------------------------------------------------------------------------
+# Streaming tests
+# ---------------------------------------------------------------------------
+
+
+class TestOpenAIChatStream(IsolatedAsyncioTestCase):
+ """Tests for OpenAIChatModel in streaming mode."""
+
+ def setUp(self) -> None:
+ self.model = _make_model(stream=True)
+
+ @patch("openai.AsyncClient")
+ async def test_stream_text_response(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream text yields n deltas (is_last=False) + 1 final
+ (is_last=True) with full content."""
+ chunks = [
+ _make_stream_chunk(delta_text="Hello"),
+ _make_stream_chunk(delta_text=" world"),
+ _make_stream_chunk(delta_text="!"),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={"prompt_tokens": 10, "completion_tokens": 3},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [TextBlock.model_construct(id=A, text="Hello")]),
+ (False, [TextBlock.model_construct(id=A, text=" world")]),
+ (False, [TextBlock.model_construct(id=A, text="!")]),
+ (True, [TextBlock.model_construct(id=A, text="Hello world!")]),
],
)
- result = self.model._parse_completion_response(self.start, resp)
- self.assertTrue(result.is_last)
- tcs = [b for b in result.content if isinstance(b, ToolCallBlock)]
- self.assertEqual(len(tcs), 1)
- self.assertEqual(tcs[0].id, "call-1")
- self.assertEqual(tcs[0].name, "get_weather")
- self.assertEqual(json.loads(tcs[0].input)["city"], "Beijing")
-
- def test_parse_thinking_response(self) -> None:
- """Parsing a response with reasoning creates a ThinkingBlock."""
- resp = self._mock_completion(
- text="Answer",
- reasoning="Let me think...",
- )
- result = self.model._parse_completion_response(self.start, resp)
- thinkings = [b for b in result.content if isinstance(b, ThinkingBlock)]
- self.assertEqual(len(thinkings), 1)
- self.assertEqual(thinkings[0].thinking, "Let me think...")
-
- def test_response_id_set(self) -> None:
- """The response ID from the API is stored in the ChatResponse."""
- resp = self._mock_completion(text="Hi")
- result = self.model._parse_completion_response(self.start, resp)
- self.assertEqual(result.id, "resp-1")
+ self.assertEqual(responses[-1].id, "resp-1")
+
+ @patch("openai.AsyncClient")
+ async def test_stream_thinking_and_text(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream with thinking + text yields deltas then final with both."""
+ chunks = [
+ _make_stream_chunk(delta_reasoning="Think"),
+ _make_stream_chunk(delta_reasoning="ing..."),
+ _make_stream_chunk(delta_text="Answer"),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={"prompt_tokens": 10, "completion_tokens": 8},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [ThinkingBlock.model_construct(id=A, thinking="Think")],
+ ),
+ (
+ False,
+ [ThinkingBlock.model_construct(id=A, thinking="ing...")],
+ ),
+ (False, [TextBlock.model_construct(id=A, text="Answer")]),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Thinking...",
+ ),
+ TextBlock.model_construct(id=A, text="Answer"),
+ ],
+ ),
+ ],
+ )
+
+ @patch("openai.AsyncClient")
+ async def test_stream_tool_calls(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream tool calls accumulate across chunks into final response."""
+ chunks = [
+ _make_stream_chunk(
+ tool_calls=[
+ _make_tool_call_delta(0, "call-1", "get_weather", '{"ci'),
+ ],
+ ),
+ _make_stream_chunk(
+ tool_calls=[
+ _make_tool_call_delta(0, None, None, 'ty":"BJ"}'),
+ ],
+ ),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={"prompt_tokens": 10, "completion_tokens": 5},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="get_weather",
+ input='{"ci',
+ ),
+ ],
+ ),
+ (
+ False,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="get_weather",
+ input='ty":"BJ"}',
+ ),
+ ],
+ ),
+ (
+ True,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="get_weather",
+ input='{"city":"BJ"}',
+ ),
+ ],
+ ),
+ ],
+ )
+
+ @patch("openai.AsyncClient")
+ async def test_stream_usage_in_final(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Usage information is captured and present in final response."""
+ chunks = [
+ _make_stream_chunk(delta_text="Hi"),
+ _make_stream_chunk(
+ has_choices=False,
+ usage={"prompt_tokens": 100, "completion_tokens": 20},
+ ),
+ ]
+ mock_create = AsyncMock(return_value=_MockAsyncStream(chunks))
+ mock_client_cls.return_value.chat.completions.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [TextBlock.model_construct(id=A, text="Hi")]),
+ (True, [TextBlock.model_construct(id=A, text="Hi")]),
+ ],
+ )
+ self.assertEqual(responses[-1].usage.input_tokens, 100)
+ self.assertEqual(responses[-1].usage.output_tokens, 20)
# ---------------------------------------------------------------------------
diff --git a/tests/model_openai_response_test.py b/tests/model_openai_response_test.py
index 8db83acf0..eda6e09f7 100644
--- a/tests/model_openai_response_test.py
+++ b/tests/model_openai_response_test.py
@@ -1,20 +1,23 @@
# -*- coding: utf-8 -*-
# pylint: disable=protected-access
-"""Unit tests for OpenAIResponseModel response parsing.
+"""Unit tests for OpenAIResponseModel with mocked API responses.
-Formatter tests have been moved to tests/formatter_openai_response_test.py.
+Tests cover both non-streaming and streaming modes.
+OpenAI Responses API uses event-based streaming with response.completed.
"""
-import json
from typing import Any
-from datetime import datetime
import unittest
from unittest import IsolatedAsyncioTestCase
-from unittest.mock import MagicMock
+from unittest.mock import AsyncMock, MagicMock, patch
+
+from utils import AnyString
from agentscope.message import TextBlock, ToolCallBlock, ThinkingBlock
from agentscope.model import OpenAIResponseModel
-from agentscope.tool import ToolChoice
from agentscope.credential import OpenAICredential
+from agentscope.tool import ToolChoice
+
+A = AnyString()
# ---------------------------------------------------------------------------
@@ -22,126 +25,393 @@ from agentscope.credential import OpenAICredential
# ---------------------------------------------------------------------------
-def _make_model() -> Any:
+def _make_model(stream: bool = False) -> Any:
return OpenAIResponseModel(
credential=OpenAICredential(api_key="test"),
model="o4-mini",
- stream=False,
+ stream=stream,
context_size=200_000,
)
+def _mock_completion(
+ text: Any = None,
+ function_calls: Any = None,
+ reasoning_summary: Any = None,
+ reasoning_id: str = "rs_test123",
+ response_id: str = "resp-openai-1",
+) -> MagicMock:
+ """Build a mock non-streaming Responses API response."""
+ output = []
+
+ if reasoning_summary:
+ reasoning_item = MagicMock()
+ reasoning_item.type = "reasoning"
+ reasoning_item.id = reasoning_id
+ summary_mock = MagicMock()
+ summary_mock.text = reasoning_summary
+ reasoning_item.summary = [summary_mock]
+ output.append(reasoning_item)
+
+ if text:
+ msg_item = MagicMock()
+ msg_item.type = "message"
+ part = MagicMock()
+ part.type = "output_text"
+ part.text = text
+ msg_item.content = [part]
+ output.append(msg_item)
+
+ if function_calls:
+ for fc in function_calls:
+ fc_item = MagicMock()
+ fc_item.type = "function_call"
+ fc_item.id = fc["id"]
+ fc_item.call_id = fc["call_id"]
+ fc_item.name = fc["name"]
+ fc_item.arguments = fc["arguments"]
+ output.append(fc_item)
+
+ resp = MagicMock()
+ resp.id = response_id
+ resp.output = output
+ resp.usage = MagicMock()
+ resp.usage.input_tokens = 10
+ resp.usage.output_tokens = 5
+ resp.usage.input_tokens_details = None
+ return resp
+
+
+def _make_event(event_type: str, **kwargs: Any) -> MagicMock:
+ """Build a mock Responses API streaming event."""
+ event = MagicMock()
+ event.type = event_type
+ for key, val in kwargs.items():
+ setattr(event, key, val)
+ # Default: no response attribute
+ if "response" not in kwargs:
+ event.response = None
+ return event
+
+
+class _MockAsyncEventStream:
+ """Mock async iterator over Response events."""
+
+ def __init__(self, events: list) -> None:
+ self._events = events
+ self._index = 0
+
+ def __aiter__(self) -> "_MockAsyncEventStream":
+ return self
+
+ async def __anext__(self) -> Any:
+ if self._index >= len(self._events):
+ raise StopAsyncIteration
+ event = self._events[self._index]
+ self._index += 1
+ return event
+
+
# ---------------------------------------------------------------------------
-# Model response parsing tests
+# Non-streaming tests
# ---------------------------------------------------------------------------
-class TestOpenAIResponseModelParsing(IsolatedAsyncioTestCase):
- """Unit tests for OpenAIResponseModel response parsing."""
+class TestOpenAIResponseNonStream(IsolatedAsyncioTestCase):
+ """Tests for OpenAIResponseModel in non-streaming mode."""
def setUp(self) -> None:
- """Set up a fresh model instance and start time."""
- self.model = _make_model()
- self.start = datetime.now()
+ self.model = _make_model(stream=False)
+
+ @patch("openai.AsyncClient")
+ async def test_text_response(self, mock_client_cls: MagicMock) -> None:
+ """Non-stream text response returns a single ChatResponse."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(text="Hello!"),
+ )
+ mock_client_cls.return_value.responses.create = mock_create
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (True, [TextBlock.model_construct(id=A, text="Hello!")]),
+ )
+ self.assertEqual(result.id, "resp-openai-1")
- def _mock_response(
+ @patch("openai.AsyncClient")
+ async def test_tool_call_response(
self,
- text: Any = None,
- function_calls: Any = None,
- reasoning_summary: Any = None,
- reasoning_id: str = "rs_test123",
- ) -> "MagicMock":
- """Build a mock OpenAI Responses API response object."""
- output = []
-
- if reasoning_summary:
- reasoning_item = MagicMock()
- reasoning_item.type = "reasoning"
- reasoning_item.id = reasoning_id
- summary_mock = MagicMock()
- summary_mock.text = reasoning_summary
- reasoning_item.summary = [summary_mock]
- output.append(reasoning_item)
-
- if text:
- msg_item = MagicMock()
- msg_item.type = "message"
- part = MagicMock()
- part.type = "output_text"
- part.text = text
- msg_item.content = [part]
- output.append(msg_item)
-
- if function_calls:
- for fc in function_calls:
- fc_item = MagicMock()
- fc_item.type = "function_call"
- fc_item.id = fc["id"]
- fc_item.call_id = fc["call_id"]
- fc_item.name = fc["name"]
- fc_item.arguments = fc["arguments"]
- output.append(fc_item)
-
- resp = MagicMock()
- resp.id = "resp-openai-1"
- resp.output = output
- resp.usage.input_tokens = 10
- resp.usage.output_tokens = 5
- return resp
-
- def test_parse_text_response(self) -> None:
- """Parsing a text response creates a TextBlock."""
- resp = self._mock_response(text="Hello!")
- result = self.model._parse_completion_response(self.start, resp)
- self.assertTrue(result.is_last)
- texts = [b for b in result.content if isinstance(b, TextBlock)]
- self.assertEqual(texts[0].text, "Hello!")
-
- def test_parse_tool_call_response(self) -> None:
- """Parsing a function_call response creates a ToolCallBlock."""
- resp = self._mock_response(
- function_calls=[
- {
- "id": "fc_abc123",
- "call_id": "call-1",
- "name": "get_weather",
- "arguments": '{"city":"Beijing"}',
- },
- ],
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Parsing a tool-call response creates a ToolCallBlock with
+ call_id."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(
+ function_calls=[
+ {
+ "id": "fc_abc",
+ "call_id": "call-1",
+ "name": "get_weather",
+ "arguments": '{"city":"BJ"}',
+ },
+ ],
+ ),
+ )
+ mock_client_cls.return_value.responses.create = mock_create
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ToolCallBlock(
+ id="fc_abc",
+ call_id="call-1",
+ name="get_weather",
+ input='{"city":"BJ"}',
+ ),
+ ],
+ ),
)
- result = self.model._parse_completion_response(self.start, resp)
- tcs = [b for b in result.content if isinstance(b, ToolCallBlock)]
- self.assertEqual(len(tcs), 1)
- self.assertEqual(tcs[0].id, "fc_abc123")
- self.assertEqual(tcs[0].call_id, "call-1")
- self.assertEqual(tcs[0].name, "get_weather")
- self.assertEqual(json.loads(tcs[0].input)["city"], "Beijing")
-
- def test_parse_reasoning_response(self) -> None:
- """Parsing a reasoning item creates a ThinkingBlock with item id."""
- resp = self._mock_response(
- reasoning_summary="Thinking step...",
- text="Answer",
- reasoning_id="rs_abc999",
+
+ @patch("openai.AsyncClient")
+ async def test_reasoning_response(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Non-stream reasoning summary plus text returns both block types."""
+ mock_create = AsyncMock(
+ return_value=_mock_completion(
+ reasoning_summary="Thinking step...",
+ text="Answer",
+ reasoning_id="rs_abc999",
+ ),
)
- result = self.model._parse_completion_response(self.start, resp)
- thinkings = [b for b in result.content if isinstance(b, ThinkingBlock)]
- self.assertEqual(len(thinkings), 1)
- self.assertEqual(thinkings[0].thinking, "Thinking step...")
+ mock_client_cls.return_value.responses.create = mock_create
+
+ result = await self.model([])
+
self.assertEqual(
- getattr(thinkings[0], "reasoning_item_id", None),
- "rs_abc999",
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Thinking step...",
+ reasoning_item_id="rs_abc999",
+ ),
+ TextBlock.model_construct(id=A, text="Answer"),
+ ],
+ ),
)
- def test_response_id_set(self) -> None:
- """The response ID from the API is stored in the ChatResponse."""
- resp = self._mock_response(text="Hi")
- result = self.model._parse_completion_response(self.start, resp)
- self.assertEqual(result.id, "resp-openai-1")
+
+# ---------------------------------------------------------------------------
+# Streaming tests
+# ---------------------------------------------------------------------------
+
+
+class TestOpenAIResponseStream(IsolatedAsyncioTestCase):
+ """Tests for OpenAIResponseModel in streaming mode."""
+
+ def setUp(self) -> None:
+ self.model = _make_model(stream=True)
+
+ @patch("openai.AsyncClient")
+ async def test_stream_text(self, mock_client_cls: MagicMock) -> None:
+ """Stream text yields deltas then final with full content."""
+ completed_resp = MagicMock()
+ completed_resp.id = "resp-1"
+ completed_resp.output = []
+ completed_resp.usage = MagicMock()
+ completed_resp.usage.input_tokens = 10
+ completed_resp.usage.output_tokens = 5
+ completed_resp.usage.input_tokens_details = None
+
+ events = [
+ _make_event(
+ "response.output_text.delta",
+ delta="Hello",
+ response=MagicMock(id="resp-1"),
+ ),
+ _make_event(
+ "response.output_text.delta",
+ delta=" world",
+ ),
+ _make_event("response.completed", response=completed_resp),
+ ]
+ mock_create = AsyncMock(
+ return_value=_MockAsyncEventStream(events),
+ )
+ mock_client_cls.return_value.responses.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [TextBlock.model_construct(id=A, text="Hello")]),
+ (False, [TextBlock.model_construct(id=A, text=" world")]),
+ (True, [TextBlock.model_construct(id=A, text="Hello world")]),
+ ],
+ )
+ self.assertEqual(responses[-1].id, "resp-1")
+
+ @patch("openai.AsyncClient")
+ async def test_stream_reasoning_and_text(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream reasoning and text deltas then final with
+ reasoning_item_id."""
+ reasoning_item = MagicMock()
+ reasoning_item.type = "reasoning"
+ reasoning_item.id = "rs_123"
+
+ completed_resp = MagicMock()
+ completed_resp.id = "resp-2"
+ completed_resp.output = [reasoning_item]
+ completed_resp.usage = MagicMock()
+ completed_resp.usage.input_tokens = 10
+ completed_resp.usage.output_tokens = 5
+ completed_resp.usage.input_tokens_details = None
+
+ events = [
+ _make_event(
+ "response.reasoning_summary_text.delta",
+ delta="Thinking",
+ response=MagicMock(id="resp-2"),
+ ),
+ _make_event(
+ "response.output_text.delta",
+ delta="Answer",
+ ),
+ _make_event("response.completed", response=completed_resp),
+ ]
+ mock_create = AsyncMock(
+ return_value=_MockAsyncEventStream(events),
+ )
+ mock_client_cls.return_value.responses.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [ThinkingBlock.model_construct(id=A, thinking="Thinking")],
+ ),
+ (False, [TextBlock.model_construct(id=A, text="Answer")]),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Thinking",
+ reasoning_item_id="rs_123",
+ ),
+ TextBlock.model_construct(id=A, text="Answer"),
+ ],
+ ),
+ ],
+ )
+
+ @patch("openai.AsyncClient")
+ async def test_stream_function_call(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream function-call events yield deltas then final
+ ToolCallBlock."""
+ fc_item = MagicMock()
+ fc_item.type = "function_call"
+ fc_item.id = "fc_1"
+ fc_item.call_id = "call-1"
+ fc_item.name = "search"
+
+ completed_resp = MagicMock()
+ completed_resp.id = "resp-3"
+ completed_resp.output = []
+ completed_resp.usage = MagicMock()
+ completed_resp.usage.input_tokens = 10
+ completed_resp.usage.output_tokens = 5
+ completed_resp.usage.input_tokens_details = None
+
+ events = [
+ _make_event(
+ "response.output_item.added",
+ item=fc_item,
+ response=MagicMock(id="resp-3"),
+ ),
+ _make_event(
+ "response.function_call_arguments.delta",
+ item_id="fc_1",
+ delta='{"q":',
+ ),
+ _make_event(
+ "response.function_call_arguments.delta",
+ item_id="fc_1",
+ delta='"test"}',
+ ),
+ _make_event("response.completed", response=completed_resp),
+ ]
+ mock_create = AsyncMock(
+ return_value=_MockAsyncEventStream(events),
+ )
+ mock_client_cls.return_value.responses.create = mock_create
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [
+ ToolCallBlock(
+ id="fc_1",
+ call_id="call-1",
+ name="search",
+ input='{"q":',
+ ),
+ ],
+ ),
+ (
+ False,
+ [
+ ToolCallBlock(
+ id="fc_1",
+ call_id="call-1",
+ name="search",
+ input='"test"}',
+ ),
+ ],
+ ),
+ (
+ True,
+ [
+ ToolCallBlock(
+ id="fc_1",
+ call_id="call-1",
+ name="search",
+ input='{"q":"test"}',
+ ),
+ ],
+ ),
+ ],
+ )
# ---------------------------------------------------------------------------
-# Shared _format_tools fixtures
+# _format_tools tests
# ---------------------------------------------------------------------------
_FT_TOOLS = [
@@ -171,7 +441,6 @@ _FT_TOOLS = [
},
]
-
_FT_TOOLS_RESPONSE = [
{
"type": "function",
@@ -196,16 +465,14 @@ _FT_TOOLS_RESPONSE = [
]
-# pylint: disable=protected-access
class TestOpenAIResponseFormatTools(unittest.TestCase):
"""Tests for OpenAIResponseModel._format_tools."""
def setUp(self) -> None:
- """Set up model instance."""
self.model = _make_model()
def test_auto_mode(self) -> None:
- """Auto mode returns flat tools and string 'auto'."""
+ """Auto mode converts tools and sets choice to 'auto'."""
fmt_tools, fmt_choice = self.model._format_tools(
_FT_TOOLS,
ToolChoice(mode="auto"),
@@ -214,7 +481,7 @@ class TestOpenAIResponseFormatTools(unittest.TestCase):
self.assertEqual(fmt_choice, "auto")
def test_none_mode(self) -> None:
- """None mode returns flat tools and string 'none'."""
+ """None mode converts tools and sets choice to 'none'."""
fmt_tools, fmt_choice = self.model._format_tools(
_FT_TOOLS,
ToolChoice(mode="none"),
@@ -223,7 +490,7 @@ class TestOpenAIResponseFormatTools(unittest.TestCase):
self.assertEqual(fmt_choice, "none")
def test_required_mode(self) -> None:
- """Required mode returns flat tools and string 'required'."""
+ """Required mode converts tools and sets choice to 'required'."""
fmt_tools, fmt_choice = self.model._format_tools(
_FT_TOOLS,
ToolChoice(mode="required"),
@@ -232,7 +499,7 @@ class TestOpenAIResponseFormatTools(unittest.TestCase):
self.assertEqual(fmt_choice, "required")
def test_str_mode_force_call(self) -> None:
- """A specific tool name returns a type=function dict with name."""
+ """String mode forces a function call for the named tool."""
fmt_tools, fmt_choice = self.model._format_tools(
_FT_TOOLS,
ToolChoice(mode="get_weather"),
@@ -244,7 +511,7 @@ class TestOpenAIResponseFormatTools(unittest.TestCase):
)
def test_tools_filtered(self) -> None:
- """When tool_choice.tools is set, only those tools are included."""
+ """ToolChoice with tools list filters to matching function names."""
fmt_tools, fmt_choice = self.model._format_tools(
_FT_TOOLS,
ToolChoice(mode="auto", tools=["get_weather"]),
@@ -254,7 +521,7 @@ class TestOpenAIResponseFormatTools(unittest.TestCase):
self.assertEqual(fmt_choice, "auto")
def test_no_tool_choice(self) -> None:
- """Without tool_choice, returns flat tools and None."""
+ """Tools are converted when tool_choice is None."""
fmt_tools, fmt_choice = self.model._format_tools(_FT_TOOLS, None)
self.assertEqual(fmt_tools, _FT_TOOLS_RESPONSE)
self.assertIsNone(fmt_choice)
diff --git a/tests/model_xai_test.py b/tests/model_xai_test.py
index 22b678431..64c75e7c7 100644
--- a/tests/model_xai_test.py
+++ b/tests/model_xai_test.py
@@ -1,27 +1,26 @@
# -*- coding: utf-8 -*-
# pylint: disable=protected-access
-"""Unit tests for XAIChatModel (xAI) response parsing.
+"""Unit tests for XAIChatModel with mocked API responses.
-Formatter tests have been moved to tests/formatter_xai_test.py.
+Tests cover both non-streaming and streaming modes.
+XAI uses xai_sdk with chat.stream() for streaming.
"""
-import json
import sys
from typing import Any
-from datetime import datetime
from types import ModuleType
import unittest
from unittest import IsolatedAsyncioTestCase
-from unittest.mock import MagicMock
+from unittest.mock import AsyncMock, MagicMock, patch
-from agentscope.message import (
- TextBlock,
- ToolCallBlock,
- ThinkingBlock,
-)
+from utils import AnyString
+
+from agentscope.message import TextBlock, ToolCallBlock, ThinkingBlock
from agentscope.model import XAIChatModel
from agentscope.credential import XAICredential
from agentscope.tool import ToolChoice
+A = AnyString()
+
# ---------------------------------------------------------------------------
# Build a lightweight xai_sdk stub so tests run without the real package.
@@ -33,7 +32,6 @@ def _build_xai_sdk_stub() -> None:
if "xai_sdk" in sys.modules:
return
- # chat_pb2 stub -----------------------------------------------------------
chat_pb2 = ModuleType("xai_sdk.chat.chat_pb2")
class _EnumHelper:
@@ -84,7 +82,6 @@ def _build_xai_sdk_stub() -> None:
chat_pb2.Message = _MessageProto
- # xai_sdk.chat stub -------------------------------------------------------
xai_chat = ModuleType("xai_sdk.chat")
xai_chat.chat_pb2 = chat_pb2
xai_chat.user = lambda *args: MagicMock(role="user", args=args)
@@ -97,7 +94,6 @@ def _build_xai_sdk_stub() -> None:
)
xai_chat.image = lambda url: MagicMock(type="image", url=url)
- # xai_sdk stub ------------------------------------------------------------
xai_sdk = ModuleType("xai_sdk")
xai_sdk.chat = xai_chat
xai_sdk.AsyncClient = MagicMock()
@@ -109,102 +105,335 @@ def _build_xai_sdk_stub() -> None:
_build_xai_sdk_stub()
+
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
-def _make_model() -> Any:
+def _make_model(stream: bool = False) -> Any:
return XAIChatModel(
credential=XAICredential(api_key="test"),
model="grok-3",
- stream=False,
+ stream=stream,
context_size=131_072,
)
+def _mock_completion(
+ text: str = "",
+ reasoning: str = "",
+ tool_calls: list | None = None,
+ response_id: str = "xai-resp-1",
+) -> MagicMock:
+ """Build a mock xAI non-streaming response."""
+ resp = MagicMock()
+ resp.id = response_id
+ resp.content = text
+ resp.reasoning_content = reasoning
+ resp.tool_calls = None
+ resp.usage = None
+
+ if tool_calls:
+ tc_mocks = []
+ for tc in tool_calls:
+ m = MagicMock()
+ m.id = tc["id"]
+ m.function.name = tc["name"]
+ m.function.arguments = tc["arguments"]
+ tc_mocks.append(m)
+ resp.tool_calls = tc_mocks
+
+ return resp
+
+
+class _MockStreamChunk:
+ """A single chunk from xai chat.stream()."""
+
+ def __init__(
+ self,
+ content: str = "",
+ reasoning_content: str = "",
+ ) -> None:
+ self.content = content
+ self.reasoning_content = reasoning_content
+
+
+class _MockChatStream:
+ """Mock xai_sdk chat session with stream() and sample()."""
+
+ def __init__(
+ self,
+ stream_items: list | None = None,
+ sample_response: Any = None,
+ ) -> None:
+ self._stream_items = stream_items or []
+ self._sample_response = sample_response
+ self._appended: list = []
+
+ def append(self, msg: Any) -> None:
+ """Append a message to the conversation."""
+ self._appended.append(msg)
+
+ async def sample(self) -> Any:
+ """Return the pre-configured sample response."""
+ return self._sample_response
+
+ def stream(self) -> "_MockStreamIterator":
+ """Return an async iterator over pre-configured stream items."""
+ return _MockStreamIterator(self._stream_items)
+
+
+class _MockStreamIterator:
+ """Async iterator for (response, chunk) pairs from xai stream."""
+
+ def __init__(self, items: list) -> None:
+ self._items = items
+ self._index = 0
+
+ def __aiter__(self) -> "_MockStreamIterator":
+ return self
+
+ async def __anext__(self) -> tuple:
+ if self._index >= len(self._items):
+ raise StopAsyncIteration
+ item = self._items[self._index]
+ self._index += 1
+ return item
+
+
# ---------------------------------------------------------------------------
-# Model response parsing tests
+# Non-streaming tests
# ---------------------------------------------------------------------------
-class TestXAIModelParsing(IsolatedAsyncioTestCase):
- """Unit tests for XAIChatModel response parsing."""
+class TestXAINonStream(IsolatedAsyncioTestCase):
+ """Tests for XAIChatModel in non-streaming mode."""
def setUp(self) -> None:
- """Set up a fresh model instance and start time."""
- self.model = _make_model()
- self.start = datetime.now()
+ self.model = _make_model(stream=False)
- def _mock_response(
+ @patch("xai_sdk.AsyncClient")
+ async def test_text_response(self, mock_client_cls: MagicMock) -> None:
+ """Non-stream text response returns a single ChatResponse."""
+ mock_chat = _MockChatStream(
+ sample_response=_mock_completion(text="Hello!"),
+ )
+ mock_client_cls.return_value.chat.create.return_value = mock_chat
+ mock_client_cls.return_value.close = AsyncMock()
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (True, [TextBlock.model_construct(id=A, text="Hello!")]),
+ )
+ self.assertEqual(result.id, "xai-resp-1")
+
+ @patch("xai_sdk.AsyncClient")
+ async def test_tool_call_response(
self,
- text: Any = None,
- tool_calls: Any = None,
- reasoning: Any = None,
- ) -> "MagicMock":
- """Build a mock xAI API response object."""
- resp = MagicMock()
- resp.id = "xai-resp-1"
- resp.content = text or ""
- resp.reasoning_content = reasoning or ""
- resp.tool_calls = None
- resp.usage = None
-
- if tool_calls:
- tc_mocks = []
- for tc in tool_calls:
- m = MagicMock()
- m.id = tc["id"]
- m.function.name = tc["name"]
- m.function.arguments = tc["arguments"]
- tc_mocks.append(m)
- resp.tool_calls = tc_mocks
-
- return resp
-
- def test_parse_text_response(self) -> None:
- """Parsing a text response creates a TextBlock."""
- resp = self._mock_response(text="Hello!")
- result = self.model._parse_completion_response(self.start, resp)
- self.assertTrue(result.is_last)
- texts = [b for b in result.content if isinstance(b, TextBlock)]
- self.assertEqual(texts[0].text, "Hello!")
-
- def test_parse_tool_call_response(self) -> None:
- """Parsing a tool call response creates a ToolCallBlock."""
- resp = self._mock_response(
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Parsing a tool-call response creates a ToolCallBlock."""
+ mock_chat = _MockChatStream(
+ sample_response=_mock_completion(
+ tool_calls=[
+ {
+ "id": "call-1",
+ "name": "get_weather",
+ "arguments": '{"city":"NY"}',
+ },
+ ],
+ ),
+ )
+ mock_client_cls.return_value.chat.create.return_value = mock_chat
+ mock_client_cls.return_value.close = AsyncMock()
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ToolCallBlock(
+ id="call-1",
+ name="get_weather",
+ input='{"city":"NY"}',
+ ),
+ ],
+ ),
+ )
+
+ @patch("xai_sdk.AsyncClient")
+ async def test_thinking_response(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Non-stream reasoning plus text returns ThinkingBlock then
+ TextBlock."""
+ mock_chat = _MockChatStream(
+ sample_response=_mock_completion(
+ text="Answer",
+ reasoning="Deep thinking...",
+ ),
+ )
+ mock_client_cls.return_value.chat.create.return_value = mock_chat
+ mock_client_cls.return_value.close = AsyncMock()
+
+ result = await self.model([])
+
+ self.assertEqual(
+ (result.is_last, result.content),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(
+ id=A,
+ thinking="Deep thinking...",
+ ),
+ TextBlock.model_construct(id=A, text="Answer"),
+ ],
+ ),
+ )
+
+
+# ---------------------------------------------------------------------------
+# Streaming tests
+# ---------------------------------------------------------------------------
+
+
+class TestXAIStream(IsolatedAsyncioTestCase):
+ """Tests for XAIChatModel in streaming mode."""
+
+ def setUp(self) -> None:
+ self.model = _make_model(stream=True)
+
+ @patch("xai_sdk.AsyncClient")
+ async def test_stream_text(self, mock_client_cls: MagicMock) -> None:
+ """Stream text yields deltas then final with full content."""
+ final_response = _mock_completion(text="Hello world")
+ final_response.tool_calls = None
+ final_response.usage = MagicMock()
+ final_response.usage.prompt_tokens = 10
+ final_response.usage.completion_tokens = 5
+ final_response.usage.cached_prompt_text_tokens = 0
+
+ stream_items = [
+ (final_response, _MockStreamChunk(content="Hello")),
+ (final_response, _MockStreamChunk(content=" world")),
+ ]
+ mock_chat = _MockChatStream(stream_items=stream_items)
+ mock_client_cls.return_value.chat.create.return_value = mock_chat
+ mock_client_cls.return_value.close = AsyncMock()
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [TextBlock.model_construct(id=A, text="Hello")]),
+ (False, [TextBlock.model_construct(id=A, text=" world")]),
+ (True, [TextBlock.model_construct(id=A, text="Hello world")]),
+ ],
+ )
+
+ @patch("xai_sdk.AsyncClient")
+ async def test_stream_thinking_and_text(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream reasoning and text deltas then final with accumulated
+ content."""
+ final_response = _mock_completion(text="")
+ final_response.tool_calls = None
+ final_response.usage = MagicMock()
+ final_response.usage.prompt_tokens = 10
+ final_response.usage.completion_tokens = 5
+ final_response.usage.cached_prompt_text_tokens = 0
+
+ stream_items = [
+ (final_response, _MockStreamChunk(reasoning_content="Think")),
+ (final_response, _MockStreamChunk(content="Answer")),
+ ]
+ mock_chat = _MockChatStream(stream_items=stream_items)
+ mock_client_cls.return_value.chat.create.return_value = mock_chat
+ mock_client_cls.return_value.close = AsyncMock()
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (
+ False,
+ [ThinkingBlock.model_construct(id=A, thinking="Think")],
+ ),
+ (False, [TextBlock.model_construct(id=A, text="Answer")]),
+ (
+ True,
+ [
+ ThinkingBlock.model_construct(id=A, thinking="Think"),
+ TextBlock.model_construct(id=A, text="Answer"),
+ ],
+ ),
+ ],
+ )
+
+ @patch("xai_sdk.AsyncClient")
+ async def test_stream_tool_calls_in_final(
+ self,
+ mock_client_cls: MagicMock,
+ ) -> None:
+ """Stream text delta then final adds tool calls from last_response."""
+ final_response = _mock_completion(
tool_calls=[
{
"id": "call-1",
- "name": "get_weather",
- "arguments": '{"city":"Beijing"}',
+ "name": "search",
+ "arguments": '{"q":"test"}',
},
],
)
- result = self.model._parse_completion_response(self.start, resp)
- tcs = [b for b in result.content if isinstance(b, ToolCallBlock)]
- self.assertEqual(len(tcs), 1)
- self.assertEqual(tcs[0].id, "call-1")
- self.assertEqual(tcs[0].name, "get_weather")
- self.assertEqual(json.loads(tcs[0].input)["city"], "Beijing")
-
- def test_parse_thinking_response(self) -> None:
- """Parsing a response with reasoning creates a ThinkingBlock."""
- resp = self._mock_response(text="Answer", reasoning="Let me think...")
- result = self.model._parse_completion_response(self.start, resp)
- thinkings = [b for b in result.content if isinstance(b, ThinkingBlock)]
- self.assertEqual(len(thinkings), 1)
- self.assertEqual(thinkings[0].thinking, "Let me think...")
-
- def test_response_id_set(self) -> None:
- """The response ID from the API is stored in the ChatResponse."""
- resp = self._mock_response(text="Hi")
- result = self.model._parse_completion_response(self.start, resp)
- self.assertEqual(result.id, "xai-resp-1")
+ final_response.usage = MagicMock()
+ final_response.usage.prompt_tokens = 10
+ final_response.usage.completion_tokens = 5
+ final_response.usage.cached_prompt_text_tokens = 0
+
+ stream_items = [
+ (final_response, _MockStreamChunk(content="I'll search")),
+ ]
+ mock_chat = _MockChatStream(stream_items=stream_items)
+ mock_client_cls.return_value.chat.create.return_value = mock_chat
+ mock_client_cls.return_value.close = AsyncMock()
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [TextBlock.model_construct(id=A, text="I'll search")]),
+ (
+ True,
+ [
+ TextBlock.model_construct(id=A, text="I'll search"),
+ ToolCallBlock(
+ id="call-1",
+ name="search",
+ input='{"q":"test"}',
+ ),
+ ],
+ ),
+ ],
+ )
# ---------------------------------------------------------------------------
-# Shared _format_tools fixtures
+# _format_tools tests
# ---------------------------------------------------------------------------
_FT_TOOLS = [
@@ -242,13 +471,10 @@ def _extend_xai_stub_for_tools() -> None:
return
class _RequiredTool:
- """Minimal stub for xai_sdk.chat.required_tool(name)."""
-
def __init__(self, tool_name: str) -> None:
self.tool_name = tool_name
def __eq__(self, other: object) -> bool:
- """Compare by tool_name."""
return (
isinstance(other, _RequiredTool)
and self.tool_name == other.tool_name
@@ -259,7 +485,6 @@ def _extend_xai_stub_for_tools() -> None:
description: str = "",
parameters: Any = None,
) -> MagicMock:
- """Minimal stub for xai_sdk.chat.tool(name, ...)."""
m = MagicMock()
m.name = name
m.description = description
@@ -277,18 +502,17 @@ class TestXAIFormatTools(unittest.TestCase):
"""Tests for XAIChatModel._format_tools."""
def setUp(self) -> None:
- """Set up model instance."""
self.model = _make_model()
def test_no_tool_choice(self) -> None:
- """Without tool_choice, returns xai_sdk tools and None."""
+ """All tools are returned when tool_choice is None."""
fmt_tools, fmt_choice = self.model._format_tools(_FT_TOOLS, None)
self.assertIsNotNone(fmt_tools)
self.assertEqual(len(fmt_tools), 2)
self.assertIsNone(fmt_choice)
def test_auto_mode(self) -> None:
- """Auto mode returns xai_sdk tools and string 'auto'."""
+ """Auto mode passes tools through with choice 'auto'."""
fmt_tools, fmt_choice = self.model._format_tools(
_FT_TOOLS,
ToolChoice(mode="auto"),
@@ -297,7 +521,7 @@ class TestXAIFormatTools(unittest.TestCase):
self.assertEqual(fmt_choice, "auto")
def test_none_mode(self) -> None:
- """None mode returns xai_sdk tools and string 'none'."""
+ """None mode passes tools through with choice 'none'."""
fmt_tools, fmt_choice = self.model._format_tools(
_FT_TOOLS,
ToolChoice(mode="none"),
@@ -306,7 +530,7 @@ class TestXAIFormatTools(unittest.TestCase):
self.assertEqual(fmt_choice, "none")
def test_str_mode_force_call(self) -> None:
- """A specific tool name returns a required_tool stub object."""
+ """String mode forces a required_tool for the named function."""
fmt_tools, fmt_choice = self.model._format_tools(
_FT_TOOLS,
ToolChoice(mode="get_weather"),
@@ -315,7 +539,7 @@ class TestXAIFormatTools(unittest.TestCase):
self.assertEqual(fmt_choice.tool_name, "get_weather")
def test_tools_filtered(self) -> None:
- """When tool_choice.tools is set, only those tools are included."""
+ """ToolChoice with tools list filters to matching function names."""
fmt_tools, fmt_choice = self.model._format_tools(
_FT_TOOLS,
ToolChoice(mode="auto", tools=["get_weather"]),
diff --git a/tests/toolkit_task_test.py b/tests/toolkit_task_test.py
index bcb78552b..2eb40fa3b 100644
--- a/tests/toolkit_task_test.py
+++ b/tests/toolkit_task_test.py
@@ -23,7 +23,6 @@ class _ToolkitTaskTestBase(IsolatedAsyncioTestCase):
async def asyncSetUp(self) -> None:
"""Set up shared test fixtures."""
- self.maxDiff = None
self.agent_state = AgentState()
self.toolkit = Toolkit(
tools=[
diff --git a/tests/toolkit_test.py b/tests/toolkit_test.py
index f02c7422e..a35c717fa 100644
--- a/tests/toolkit_test.py
+++ b/tests/toolkit_test.py
@@ -852,7 +852,6 @@ class ToolGroupTest(IsolatedAsyncioTestCase):
group_name="group_1",
description="Group 1",
)
- self.maxDiff = None
# The group is created successfully
self.assertEqual(len(toolkit.groups), 1)