Skip to content

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

fix(model): use ToolCallBlock.id to store call_id; fix key names to input_text/output_text for text input (#1950)

qbc0c807329f329
diff --git a/src/agentscope/formatter/_openai_response_formatter.py b/src/agentscope/formatter/_openai_response_formatter.py
index d44d64f9c..2d9a370ba 100644
--- a/src/agentscope/formatter/_openai_response_formatter.py
+++ b/src/agentscope/formatter/_openai_response_formatter.py
@@ -126,8 +126,13 @@ class OpenAIResponseFormatter(_OpenAIResponseFormatterBase):
for block in msg.get_content_blocks():
if isinstance(block, TextBlock):
+ text_type = (
+ "output_text"
+ if msg.role == "assistant"
+ else "input_text"
+ )
content_parts.append(
- {"type": "input_text", "text": block.text},
+ {"type": text_type, "text": block.text},
)
elif isinstance(block, DataBlock):
@@ -228,21 +233,10 @@ class OpenAIResponseFormatter(_OpenAIResponseFormatterBase):
)
elif isinstance(block, ToolCallBlock):
- # The Responses API distinguishes two identifiers on a
- # function_call item:
- # id → fc_xxx: the item identifier used when
- # echoing the item in multi-turn history
- # call_id → call_xxx: the identifier that must be
- # echoed in the matching function_call_output
- # For other APIs (Chat Completions, DashScope …) only one
- # ID exists; call_id extra field is None and we fall back
- # to id for both fields.
function_calls.append(
{
"type": "function_call",
- "id": block.id,
- "call_id": getattr(block, "call_id", None)
- or block.id,
+ "call_id": block.id,
"name": block.name,
"arguments": block.input,
},
diff --git a/src/agentscope/message/_block.py b/src/agentscope/message/_block.py
index 86242ab55..38201ff30 100644
--- a/src/agentscope/message/_block.py
+++ b/src/agentscope/message/_block.py
@@ -112,13 +112,9 @@ class ToolCallState(StrEnum):
class ToolCallBlock(BaseModel):
- """The tool call block.
+ """The tool call block."""
- Allows extra provider-specific fields (e.g. the OpenAI Responses API's
- ``call_id``) via ``extra="allow"`` without requiring subclassing.
- """
-
- model_config = ConfigDict(use_enum_values=True, extra="allow")
+ model_config = ConfigDict(use_enum_values=True)
type: Literal["tool_call"] = "tool_call"
"""The type of the tool call block, which is always 'tool_call'."""
diff --git a/src/agentscope/model/_openai_response/_model.py b/src/agentscope/model/_openai_response/_model.py
index 8c1ac4522..9155f6460 100644
--- a/src/agentscope/model/_openai_response/_model.py
+++ b/src/agentscope/model/_openai_response/_model.py
@@ -290,17 +290,15 @@ class OpenAIResponseModel(ChatModelBase):
elif event_type == "response.output_item.added":
item = event.item
if getattr(item, "type", None) == "function_call":
- # item.id -> fc_xxx (used as ToolCallBlock.id
- # and echoed back as function_call.id
- # in multi-turn history)
- # item.call_id -> call_xxx (used as
- # function_call_output.call_id)
- # Only record the id/name/call_id here — do NOT emit
- # an empty-input delta so downstream consumers don't
- # see a leading no-op chunk. The block is created on
- # the first argument delta below.
+ # item.call_id (call_xxx) is used as ToolCallBlock.id
+ # so the formatter can echo it back as
+ # function_call.call_id / function_call_output.call_id.
+ # Only record the mapping here — do NOT emit an
+ # empty-input delta so downstream consumers don't see
+ # a leading no-op chunk. The block is created on the
+ # first argument delta below.
tool_call_mapping[item.id] = (
- getattr(item, "call_id", None),
+ item.call_id,
getattr(item, "name", "") or "unknown",
)
@@ -309,10 +307,9 @@ class OpenAIResponseModel(ChatModelBase):
if item_id in tool_call_mapping:
call_id, name = tool_call_mapping[item_id]
delta_res.append_tool_call(
- block_id=item_id,
+ block_id=call_id,
name=name,
input=event.delta or "",
- call_id=call_id,
)
elif event_type == "response.completed":
@@ -401,8 +398,7 @@ class OpenAIResponseModel(ChatModelBase):
elif item_type == "function_call":
content_blocks.append(
ToolCallBlock(
- id=getattr(item, "id", ""),
- call_id=getattr(item, "call_id", None),
+ id=item.call_id,
name=item.name,
input=getattr(item, "arguments", "") or "{}",
),
diff --git a/tests/formatter_openai_response_test.py b/tests/formatter_openai_response_test.py
index 0ca18e39e..c43189201 100644
--- a/tests/formatter_openai_response_test.py
+++ b/tests/formatter_openai_response_test.py
@@ -145,7 +145,7 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
"role": "assistant",
"content": [
{
- "type": "input_text",
+ "type": "output_text",
"text": "The capital of France is Paris.",
},
],
@@ -163,7 +163,7 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
"role": "assistant",
"content": [
{
- "type": "input_text",
+ "type": "output_text",
"text": "The capital of Germany is Berlin.",
},
],
@@ -179,7 +179,6 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
},
{
"type": "function_call",
- "id": "call_1",
"call_id": "call_1",
"name": "get_capital",
"arguments": '{"country": "Japan"}',
@@ -193,7 +192,7 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
"role": "assistant",
"content": [
{
- "type": "input_text",
+ "type": "output_text",
"text": "The capital of Japan is Tokyo.",
},
],
@@ -223,7 +222,7 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
"role": "assistant",
"content": [
{
- "type": "input_text",
+ "type": "output_text",
"text": "The capital of Japan is Tokyo.",
},
],
@@ -231,7 +230,6 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
self._gt_tool_call = {
"type": "function_call",
- "id": "call_1",
"call_id": "call_1",
"name": "get_capital",
"arguments": '{"country": "Japan"}',
@@ -358,7 +356,7 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
{
"role": "assistant",
"content": [
- {"type": "input_text", "text": "reply"},
+ {"type": "output_text", "text": "reply"},
],
},
],
@@ -393,7 +391,7 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
{
"role": "assistant",
"content": [
- {"type": "input_text", "text": "reply"},
+ {"type": "output_text", "text": "reply"},
],
},
],
@@ -450,7 +448,6 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
[
{
"type": "function_call",
- "id": "call_img",
"call_id": "call_img",
"name": "get_map",
"arguments": '{"city": "Tokyo"}',
@@ -489,7 +486,7 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
"role": "assistant",
"content": [
{
- "type": "input_text",
+ "type": "output_text",
"text": "Here is the map of Tokyo.",
},
],
@@ -622,19 +619,17 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
{
"role": "assistant",
"content": [
- {"type": "input_text", "text": "text_1"},
+ {"type": "output_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"}',
@@ -652,12 +647,11 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
{
"role": "assistant",
"content": [
- {"type": "input_text", "text": "text_2"},
+ {"type": "output_text", "text": "text_2"},
],
},
{
"type": "function_call",
- "id": "call_3",
"call_id": "call_3",
"name": "func_3",
"arguments": '{"arg": "value3"}',
@@ -669,7 +663,6 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
},
{
"type": "function_call",
- "id": "call_4",
"call_id": "call_4",
"name": "func_4",
"arguments": '{"arg": "value4"}',
@@ -682,7 +675,7 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
{
"role": "assistant",
"content": [
- {"type": "input_text", "text": "text_3"},
+ {"type": "output_text", "text": "text_3"},
],
},
],
@@ -709,7 +702,7 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
"role": "assistant",
"content": [
{
- "type": "input_text",
+ "type": "output_text",
"text": "Let me think about that.",
},
],
@@ -727,7 +720,7 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
"role": "assistant",
"content": [
{
- "type": "input_text",
+ "type": "output_text",
"text": "Here is my answer.",
},
],
@@ -777,3 +770,43 @@ class TestOpenAIResponseFormatter(IsolatedAsyncioTestCase):
],
res,
)
+
+ async def test_tool_call_id_used_as_call_id(self) -> None:
+ """The formatter uses ToolCallBlock.id as function_call.call_id
+ and ToolResultBlock.id as function_call_output.call_id."""
+ fmt = OpenAIResponseFormatter()
+ msgs = [
+ AssistantMsg(
+ name="assistant",
+ content=[
+ ToolCallBlock(
+ id="call-1",
+ name="search",
+ input='{"q": "test"}',
+ ),
+ ToolResultBlock(
+ id="call-1",
+ name="search",
+ output=[TextBlock(text="result")],
+ state=ToolResultState.SUCCESS,
+ ),
+ ],
+ ),
+ ]
+ res = await fmt.format(msgs)
+ self.assertListEqual(
+ [
+ {
+ "type": "function_call",
+ "call_id": "call-1",
+ "name": "search",
+ "arguments": '{"q": "test"}',
+ },
+ {
+ "type": "function_call_output",
+ "call_id": "call-1",
+ "output": "result",
+ },
+ ],
+ res,
+ )
diff --git a/tests/model_openai_response_test.py b/tests/model_openai_response_test.py
index 14291a028..739f3b8cb 100644
--- a/tests/model_openai_response_test.py
+++ b/tests/model_openai_response_test.py
@@ -144,8 +144,7 @@ class TestOpenAIResponseNonStream(IsolatedAsyncioTestCase):
self,
mock_client_cls: MagicMock,
) -> None:
- """Parsing a tool-call response creates a ToolCallBlock with
- call_id."""
+ """Parsing a tool-call response stores call_id as ToolCallBlock.id."""
mock_create = AsyncMock(
return_value=_mock_completion(
function_calls=[
@@ -168,8 +167,7 @@ class TestOpenAIResponseNonStream(IsolatedAsyncioTestCase):
True,
[
ToolCallBlock(
- id="fc_abc",
- call_id="call-1",
+ id="call-1",
name="get_weather",
input='{"city":"BJ"}',
),
@@ -369,8 +367,7 @@ class TestOpenAIResponseStream(IsolatedAsyncioTestCase):
self,
mock_client_cls: MagicMock,
) -> None:
- """Stream function-call events yield deltas then final
- ToolCallBlock."""
+ """Stream function-call events use call_id as ToolCallBlock.id."""
fc_item = MagicMock()
fc_item.type = "function_call"
fc_item.id = "fc_1"
@@ -418,8 +415,7 @@ class TestOpenAIResponseStream(IsolatedAsyncioTestCase):
False,
[
ToolCallBlock(
- id="fc_1",
- call_id="call-1",
+ id="call-1",
name="search",
input='{"q":',
),
@@ -429,8 +425,7 @@ class TestOpenAIResponseStream(IsolatedAsyncioTestCase):
False,
[
ToolCallBlock(
- id="fc_1",
- call_id="call-1",
+ id="call-1",
name="search",
input='"test"}',
),
@@ -440,8 +435,7 @@ class TestOpenAIResponseStream(IsolatedAsyncioTestCase):
True,
[
ToolCallBlock(
- id="fc_1",
- call_id="call-1",
+ id="call-1",
name="search",
input='{"q":"test"}',
),