Skip to content

Agent Zero AI framework

Stream native tool arguments through the existing live display

Alessandro71f32b7b8bd1
diff --git a/agent.py b/agent.py
index 0cd058693..8b34c2e75 100644
--- a/agent.py
+++ b/agent.py
@@ -458,6 +458,12 @@ class Agent:
printer.print("Response: ") # start of response
# Pass chunk and full data to extensions for processing
stream_data = {"chunk": chunk, "full": full}
+ await extension.call_extensions_async(
+ "response_stream_chunk",
+ self,
+ loop_data=self.loop_data,
+ stream_data=stream_data,
+ )
tool_request = extract_tools.extract_tool_request(full)
if tool_request is not None:
try:
@@ -465,16 +471,10 @@ class Agent:
except Exception:
pass
else:
- await self.handle_response_stream(full)
+ await self.handle_response_stream(stream_data["full"])
response_stream_pending = False
return full.strip()
- await extension.call_extensions_async(
- "response_stream_chunk",
- self,
- loop_data=self.loop_data,
- stream_data=stream_data,
- )
# Stream masked chunk after extensions processed it
if stream_data.get("chunk"):
printer.stream(stream_data["chunk"])
@@ -1566,6 +1566,11 @@ class Agent:
return # no reason to try
response = DirtyJson.parse_string(stream)
if isinstance(response, dict):
+ try:
+ tool_name, tool_args = extract_tools.normalize_tool_request(response)
+ response.update(tool_name=tool_name, tool_args=tool_args)
+ except ValueError:
+ pass # Tool fields may still be incomplete.
await extension.call_extensions_async(
"response_stream",
self,
diff --git a/extensions/python/response_stream/AGENTS.md b/extensions/python/response_stream/AGENTS.md
index 5da570f6b..45c67f31f 100644
--- a/extensions/python/response_stream/AGENTS.md
+++ b/extensions/python/response_stream/AGENTS.md
@@ -11,6 +11,7 @@
## Local Contracts
- Keep streaming output synchronized with UI log items.
+- Agent.handle_response_stream normalizes complete tool name/argument aliases before these consumers; incomplete snapshots remain partial data. This display normalization does not execute or authorize tools.
- Treat parsed stream snapshots as partial data; nested tool fields may be `None`
until their values arrive.
- Preserve include-alias replacement semantics where prompts/tools rely on them.
diff --git a/helpers/litellm_transport.py b/helpers/litellm_transport.py
index 7f2cd0993..e1a7411a6 100644
--- a/helpers/litellm_transport.py
+++ b/helpers/litellm_transport.py
@@ -428,6 +428,8 @@ class LiteLLMTransport:
self, parser: "ResponsesEventParser", request: dict[str, Any]
) -> LLMResult | None:
if parser.completed_response is None:
+ if parser.function_calls:
+ raise RuntimeError("Responses stream ended before native tool calls completed")
return None
response = _object_to_dict(parser.completed_response)
output = _as_list(response.get("output"))
@@ -1163,7 +1165,8 @@ class ResponsesEventParser:
self.function_calls: dict[str, dict[str, Any]] = {}
self.output_index_keys: dict[str, str] = {}
self.emitted_function_calls: set[str] = set()
- self.streamed_response_calls: dict[str, str] = {}
+ self.streamed_function_calls: dict[str, str] = {}
+ self.pending_function_calls: dict[str, Any] = {}
self.seen_response_delta = False
self.seen_reasoning_delta = False
self.completed_response: Any = None
@@ -1194,6 +1197,8 @@ class ResponsesEventParser:
response_delta = self._complete_output_item(_get_value(event, "item"), event)
elif event_type == "response.completed":
response_delta, reasoning_delta = self._complete_response(event)
+ elif event_type == "response.incomplete":
+ raise RuntimeError("Responses generation incomplete; tool calls were not executed")
elif event_type == "response.failed":
raise RuntimeError(self._response_error_message(event))
elif event_type == "error":
@@ -1229,14 +1234,20 @@ class ResponsesEventParser:
current = self.function_calls.setdefault(key, {"type": "function_call"})
delta = str(_get_value(event, "delta") or "")
current["arguments"] = str(current.get("arguments") or "") + delta
- if current.get("name") != "response":
+ if (
+ not current.get("name")
+ or key in self.emitted_function_calls
+ or key in self.pending_function_calls
+ or (self.streamed_function_calls and key not in self.streamed_function_calls)
+ ):
return ""
- if key not in self.streamed_response_calls:
- self.streamed_response_calls[key] = str(current["arguments"])
- return '{"tool_name":"response","tool_args":' + str(
- current["arguments"]
+ if key not in self.streamed_function_calls:
+ self.streamed_function_calls[key] = str(current["arguments"])
+ return (
+ '{"tool_name":' + json.dumps(current["name"], ensure_ascii=False)
+ + ',"tool_args":' + str(current["arguments"])
)
- self.streamed_response_calls[key] += delta
+ self.streamed_function_calls[key] += delta
return delta
def _complete_function_call(self, event: Any) -> str:
@@ -1248,24 +1259,29 @@ class ResponsesEventParser:
current["arguments"] = _get_value(event, "arguments")
if _get_value(event, "name"):
current["name"] = _get_value(event, "name")
- if key in self.streamed_response_calls:
- return self._finish_response_call(key, current)
+ if key in self.streamed_function_calls:
+ return self._finish_function_call(key, current)
return self._emit_function_call(key, current)
def _complete_output_item(self, item: Any, event: Any) -> str:
key = self._remember_function_call(item, event)
if not key:
return ""
- if key in self.streamed_response_calls:
- return self._finish_response_call(key, self.function_calls[key])
+ if key in self.streamed_function_calls:
+ return self._finish_function_call(key, self.function_calls[key])
return self._emit_function_call(key, self.function_calls[key])
- def _finish_response_call(self, key: str, item: Any) -> str:
- streamed = self.streamed_response_calls.pop(key)
+ def _finish_function_call(self, key: str, item: Any) -> str:
+ streamed = self.streamed_function_calls.pop(key)
arguments = str(_get_value(item, "arguments") or "")
self.emitted_function_calls.add(key)
tail = arguments[len(streamed) :] if arguments.startswith(streamed) else ""
- return tail + "}"
+ pending = self.pending_function_calls
+ self.pending_function_calls = {}
+ return tail + "}" + "".join(
+ self._emit_function_call(pending_key, pending_item)
+ for pending_key, pending_item in pending.items()
+ )
def _complete_response(self, event: Any) -> tuple[str, str]:
self.completed_response = _get_value(event, "response")
@@ -1279,6 +1295,9 @@ class ResponsesEventParser:
def _emit_function_call(self, key: str, item: Any) -> str:
if key in self.emitted_function_calls:
return ""
+ if self.streamed_function_calls:
+ self.pending_function_calls[key] = item
+ return ""
text = ResponsesTransport.function_call_text(item)
if text:
self.emitted_function_calls.add(key)
diff --git a/helpers/litellm_transport.py.dox.md b/helpers/litellm_transport.py.dox.md
index a6ab44b01..24c454b2b 100644
--- a/helpers/litellm_transport.py.dox.md
+++ b/helpers/litellm_transport.py.dox.md
@@ -36,7 +36,8 @@
- Preserve Chat Completions tool calls from both non-streaming responses and streaming deltas as canonical `LLMResult` function-call items.
- Preserve provider usage and LiteLLM response cost for both transports only when the response or stream actually supplies them; do not synthesize unavailable provider accounting.
- Preserve Responses function calls collected from stream events when a terminal completed event omits them.
-- Stream native `response` function arguments through a canonical response-tool envelope while continuing to buffer other function calls until completion.
+- Stream named native function arguments through canonical tool envelopes for display. Buffer interleaved calls until the active envelope closes; preserve independent original call metadata. Native execution still waits for the completed model turn.
+- Fail incomplete Responses generations and native-call streams that end without completion; never promote partial call previews to a Chat result.
- Serialize synthesized Responses function-call JSON with literal Unicode so streamed raw-response logs preserve tool arguments.
- Preserve provider-state metadata when Responses API calls succeed, and fall back to local replay when provider state is unsupported.
- Keep prompt-cache markers only for providers that accept them.
diff --git a/tests/test_plain_response_logging.py b/tests/test_plain_response_logging.py
index 04a4a0cef..df43c8088 100644
--- a/tests/test_plain_response_logging.py
+++ b/tests/test_plain_response_logging.py
@@ -213,3 +213,34 @@ async def test_stream_log_tolerates_partial_tool_arguments(
item = loop_data.params_temporary["log_item_generating"]
assert item.kvps["step"] == expected_step
+
+
[email protected]("tool_request", [
+ {"tool": "code_execution_tool", "args": {"runtime": "python", "code": "print(1)"}},
+ {"actions": [{"tool": "code_execution_tool", "args": {"runtime": "python", "code": "print(1)"}}]},
+ {"type": "function", "name": "code_execution_tool", "parameters": {"runtime": "python", "code": "print(1)"}},
+])
+async def test_stream_aliases_reach_all_consumers_canonically(monkeypatch, tool_request):
+ import json
+ from agent import Agent
+ from helpers import extension
+
+ agent = object.__new__(Agent)
+ agent.loop_data = LoopData()
+ agent.agent_name = "A0"
+ agent.context = SimpleNamespace(log=Log())
+
+ async def no_intervention():
+ pass
+
+ async def consume(name, _agent, **kwargs):
+ assert name == "response_stream"
+ assert kwargs["parsed"]["tool_name"] == "code_execution_tool"
+ assert kwargs["parsed"]["tool_args"] == {"runtime": "python", "code": "print(1)"}
+ await StreamLog(agent=agent).execute(**kwargs)
+
+ agent.handle_intervention = no_intervention
+ monkeypatch.setattr(extension, "call_extensions_async", consume)
+ await agent.handle_response_stream(json.dumps(tool_request))
+ assert agent.loop_data.params_temporary["log_item_generating"].kvps["step"] == "Writing Python code... (8)"
diff --git a/tests/test_response_stream_cadence.py b/tests/test_response_stream_cadence.py
index ce9254a73..2ce3c5f8f 100644
--- a/tests/test_response_stream_cadence.py
+++ b/tests/test_response_stream_cadence.py
@@ -105,5 +105,5 @@ async def test_response_stream_still_stops_on_exact_canonical_root(monkeypatch):
assert result == "done"
assert provider_callbacks == 2
- assert [value for kind, value in events if kind == "handle"] == [message]
- assert events.count(("extension", "response_stream_chunk")) == 1
+ assert [value for kind, value in events if kind == "handle"] == ["masked:" + message]
+ assert events.count(("extension", "response_stream_chunk")) == 2
diff --git a/tests/test_stream_tool_early_stop.py b/tests/test_stream_tool_early_stop.py
index 0825c8e74..ec356542e 100644
--- a/tests/test_stream_tool_early_stop.py
+++ b/tests/test_stream_tool_early_stop.py
@@ -1682,14 +1682,15 @@ def test_responses_stream_parser_accumulates_function_call_arguments():
},
}
) == {"reasoning_delta": "", "response_delta": ""}
- assert parser.parse(
+ partial = parser.parse(
{
"type": "response.function_call_arguments.delta",
"item_id": "fc_1",
"output_index": 0,
"delta": '{"q":',
}
- ) == {"reasoning_delta": "", "response_delta": ""}
+ )
+ assert partial["response_delta"] == '{"tool_name":"lookup","tool_args":{"q":'
parsed = parser.parse(
{
@@ -1701,7 +1702,7 @@ def test_responses_stream_parser_accumulates_function_call_arguments():
}
)
- assert extract_tools.json_parse_dirty(parsed["response_delta"]) == {
+ assert extract_tools.json_parse_dirty(partial["response_delta"] + parsed["response_delta"]) == {
"tool_name": "lookup",
"tool_args": {"q": "a0"},
}
@@ -1873,3 +1874,71 @@ def test_responses_stream_parser_preserves_non_ascii_function_call_arguments():
)
assert parsed["response_delta"] == '{"tool_name": "response", "tool_args": {"text": "привет"}}'
+
+
+async def test_native_argument_progress_keeps_interleaved_calls_and_terminal_metadata(monkeypatch):
+ arguments = '{"runtime":"python","code":"print(\\"日本\\")","nested":{"items":[1,2]}}'
+ first = {"type": "function_call", "id": "fc_1", "call_id": "call_1", "name": "code_execution_tool", "arguments": arguments}
+ second = {"type": "function_call", "id": "fc_2", "call_id": "call_2", "name": "lookup", "arguments": '{"q":"a0"}'}
+ split = arguments.index('日本')
+ events = [
+ {"type": "response.output_item.added", "item": {**first, "arguments": ""}},
+ {"type": "response.function_call_arguments.delta", "item_id": "fc_1", "delta": arguments[:split]},
+ {"type": "response.output_item.added", "item": {**second, "arguments": ""}},
+ {"type": "response.function_call_arguments.delta", "item_id": "fc_2", "delta": second["arguments"]},
+ {"type": "response.output_item.done", "item": second},
+ {"type": "response.function_call_arguments.delta", "item_id": "fc_1", "delta": arguments[split:]},
+ {"type": "response.output_item.done", "item": first},
+ {"type": "response.completed", "response": {"id": "resp_1", "output": [first, second], "usage": {"input_tokens": 2048}}},
+ ]
+ stream = _AsyncChunkStream(events)
+
+ async def aresponses(**kwargs):
+ return stream
+
+ async def no_limiter(*args, **kwargs):
+ return None
+
+ snapshots = []
+
+ async def callback(chunk, full):
+ snapshots.append((stream.index, full))
+ return full # Native turns must still consume completion metadata.
+
+ monkeypatch.setattr(litellm_transport, "aresponses", aresponses)
+ monkeypatch.setattr(models, "apply_rate_limiter", no_limiter)
+ wrapper = models.LiteLLMChatWrapper(model="test-model", provider="openai", model_config=None)
+ result = await wrapper.unified_turn(messages=[], response_callback=callback, a0_api_mode="responses")
+
+ assert snapshots[0][0] == 2
+ assert DirtyJson.parse_string(snapshots[0][1])["tool_args"]["runtime"] == "python"
+ roots = extract_tools.extract_json_root_strings(snapshots[-1][1])
+ assert [json.loads(root)["tool_name"] for root in roots] == ["code_execution_tool", "lookup"]
+ assert json.loads(roots[0])["tool_args"] == json.loads(arguments)
+ assert [call.call_id for call in result.function_calls] == ["call_1", "call_2"]
+ assert result.usage == {"input_tokens": 2048}
+ assert stream.index == len(events)
+
+
[email protected]("incomplete", [False, True])
+async def test_interrupted_native_argument_stream_cannot_become_a_text_tool(monkeypatch, incomplete):
+ events = [
+ {"type": "response.output_item.added", "item": {"type": "function_call", "id": "fc_1", "name": "lookup", "arguments": ""}},
+ {"type": "response.function_call_arguments.delta", "item_id": "fc_1", "delta": '{"q":"partial'},
+ ]
+ if incomplete:
+ events.append({"type": "response.incomplete", "response": {"status": "incomplete"}})
+ stream = _AsyncChunkStream(events)
+
+ async def aresponses(**kwargs):
+ return stream
+
+ monkeypatch.setattr(litellm_transport, "aresponses", aresponses)
+ transport = litellm_transport.LiteLLMTransport(model="openai/test", messages=[], kwargs={"a0_api_mode": "responses"})
+ with pytest.raises(RuntimeError, match="incomplete|before native tool calls completed"):
+ async for _ in transport.astream():
+ pass
+ assert transport.last_result is None
+ assert stream.closed