Skip to content

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

fix(model): stream an OpenAI Responses tool call without arguments as {} (#3229)

ffffff5571a0989a7559
diff --git a/src/agentscope/model/_openai_response/_model.py b/src/agentscope/model/_openai_response/_model.py
index b30bbe7d1..2d74d57a9 100644
--- a/src/agentscope/model/_openai_response/_model.py
+++ b/src/agentscope/model/_openai_response/_model.py
@@ -287,6 +287,8 @@ class OpenAIResponseModel(ChatModelBase):
# so subsequent argument deltas can be routed to the right tool
# call block.
tool_call_mapping: dict = OrderedDict()
+ # The item ids of the function calls that received argument text
+ tool_calls_with_input: set[str] = set()
async with response as stream:
async for event in stream:
@@ -356,7 +358,8 @@ class OpenAIResponseModel(ChatModelBase):
# 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.
+ # first argument delta below, or on the arguments-done
+ # event when the call carries no arguments at all.
tool_call_mapping[item.id] = (
item.call_id,
getattr(item, "name", "") or "unknown",
@@ -364,12 +367,29 @@ class OpenAIResponseModel(ChatModelBase):
elif event_type == "response.function_call_arguments.delta":
item_id = event.item_id
- if item_id in tool_call_mapping:
+ if item_id in tool_call_mapping and event.delta:
call_id, name = tool_call_mapping[item_id]
+ tool_calls_with_input.add(item_id)
delta_res.append_tool_call(
block_id=call_id,
name=name,
- input=event.delta or "",
+ input=event.delta,
+ )
+
+ elif event_type == "response.function_call_arguments.done":
+ # A call without arguments streams no argument deltas, so
+ # the block would never be created above; close it with
+ # the empty object the non-streaming response carries.
+ item_id = event.item_id
+ if (
+ item_id in tool_call_mapping
+ and item_id not in tool_calls_with_input
+ ):
+ call_id, name = tool_call_mapping[item_id]
+ delta_res.append_tool_call(
+ block_id=call_id,
+ name=name,
+ input=event.arguments or "{}",
)
elif event_type == "response.completed":
diff --git a/tests/model_openai_response_test.py b/tests/model_openai_response_test.py
index 0a7da8430..b6788e68d 100644
--- a/tests/model_openai_response_test.py
+++ b/tests/model_openai_response_test.py
@@ -1388,6 +1388,106 @@ class TestOpenAIResponseStream(IsolatedAsyncioTestCase):
],
)
+ async def test_stream_function_call_without_arguments(self) -> None:
+ """A function call with no arguments still streams as a {} block.
+
+ Empty-argument calls may emit no argument delta or an empty one, so
+ the block used to be dropped entirely instead of matching the
+ non-streaming ``input or "{}"`` result.
+ """
+ fc_item = MagicMock()
+ fc_item.type = "function_call"
+ fc_item.id = "fc_1"
+ fc_item.call_id = "call-1"
+ fc_item.name = "get_current_time"
+ fc_item.arguments = ""
+
+ completed_resp = MagicMock()
+ completed_resp.id = "resp-4"
+ 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
+
+ non_stream_model = _make_model(stream=False)
+ non_stream_model.client = self.mock_client
+ non_stream_resp = _mock_completion(
+ function_calls=[
+ {
+ "id": "fc_1",
+ "call_id": "call-1",
+ "name": "get_current_time",
+ "arguments": "",
+ },
+ ],
+ response_id="resp-4",
+ )
+
+ cases = {
+ "no_delta": [],
+ "empty_delta": [
+ _make_event(
+ "response.function_call_arguments.delta",
+ item_id="fc_1",
+ delta="",
+ ),
+ ],
+ }
+ expected_block = ToolCallBlock.model_construct(
+ id="call-1",
+ created_at=A,
+ name="get_current_time",
+ input="{}",
+ )
+ for name, argument_events in cases.items():
+ with self.subTest(name=name):
+ events = [
+ _make_event(
+ "response.output_item.added",
+ item=fc_item,
+ response=MagicMock(id="resp-4"),
+ ),
+ *argument_events,
+ _make_event(
+ "response.function_call_arguments.done",
+ item_id="fc_1",
+ arguments="",
+ ),
+ _make_event(
+ "response.completed",
+ response=completed_resp,
+ ),
+ ]
+ self.mock_client.responses.create = AsyncMock(
+ side_effect=[
+ _MockAsyncEventStream(events),
+ non_stream_resp,
+ ],
+ )
+
+ gen = await self.model([])
+ responses = [r async for r in gen]
+ non_stream = await non_stream_model([])
+
+ self.assertListEqual(
+ [(r.is_last, r.content) for r in responses],
+ [
+ (False, [expected_block]),
+ (True, [expected_block]),
+ ],
+ )
+ self.assertListEqual(
+ [
+ (block.id, block.name, block.input)
+ for block in (
+ responses[-1].content[0],
+ non_stream.content[0],
+ )
+ ],
+ [("call-1", "get_current_time", "{}")] * 2,
+ )
+
# ---------------------------------------------------------------------------
# _format_tools tests