Agent Zero AI framework
Stream native response tool output
Alessandro117ae265f9bb
diff --git a/helpers/litellm_transport.py b/helpers/litellm_transport.pyindex bfd9ff6db..5491a5fd6 100644--- a/helpers/litellm_transport.py+++ b/helpers/litellm_transport.py@@ -1166,6 +1166,7 @@ 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.seen_response_delta = Falseself.seen_reasoning_delta = Falseself.completed_response: Any = None@@ -1189,7 +1190,7 @@ class ResponsesEventParser:elif event_type == "response.output_item.added":self._remember_function_call(_get_value(event, "item"), event)elif event_type == "response.function_call_arguments.delta":- self._append_function_call_arguments(event)+ response_delta = self._append_function_call_arguments(event)elif event_type == "response.function_call_arguments.done":response_delta = self._complete_function_call(event)elif event_type == "response.output_item.done":@@ -1224,14 +1225,22 @@ class ResponsesEventParser:self.output_index_keys[str(output_index)] = keyreturn key- def _append_function_call_arguments(self, event: Any) -> None:+ def _append_function_call_arguments(self, event: Any) -> str:key = self._event_key(event)if not key:- return+ return ""current = self.function_calls.setdefault(key, {"type": "function_call"})- current["arguments"] = str(current.get("arguments") or "") + str(- _get_value(event, "delta") or ""- )+ delta = str(_get_value(event, "delta") or "")+ current["arguments"] = str(current.get("arguments") or "") + delta+ if current.get("name") != "response":+ 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"]+ )+ self.streamed_response_calls[key] += delta+ return deltadef _complete_function_call(self, event: Any) -> str:key = self._event_key(event)@@ -1242,14 +1251,25 @@ 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)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])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)+ arguments = str(_get_value(item, "arguments") or "")+ self.emitted_function_calls.add(key)+ tail = arguments[len(streamed) :] if arguments.startswith(streamed) else ""+ return tail + "}"+def _complete_response(self, event: Any) -> tuple[str, str]:self.completed_response = _get_value(event, "response")if self.seen_response_delta or self.emitted_function_calls:diff --git a/helpers/litellm_transport.py.dox.md b/helpers/litellm_transport.py.dox.mdindex b04d0df55..8a80b030e 100644--- a/helpers/litellm_transport.py.dox.md+++ b/helpers/litellm_transport.py.dox.md@@ -35,6 +35,7 @@- 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.- 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_stream_tool_early_stop.py b/tests/test_stream_tool_early_stop.pyindex ae5cf22cf..e688916c0 100644--- a/tests/test_stream_tool_early_stop.py+++ b/tests/test_stream_tool_early_stop.py@@ -13,6 +13,7 @@ if str(PROJECT_ROOT) not in sys.path:import modelsfrom helpers import extract_toolsfrom helpers import litellm_transport+from helpers.dirty_json import DirtyJson@pytest.fixture(autouse=True)@@ -1704,6 +1705,69 @@ def test_responses_stream_parser_accumulates_function_call_arguments():) == {"reasoning_delta": "", "response_delta": ""}+def test_responses_stream_parser_streams_response_function_arguments():+ parser = litellm_transport.ResponsesEventParser()++ parser.parse(+ {+ "type": "response.output_item.added",+ "output_index": 0,+ "item": {+ "type": "function_call",+ "id": "fc_1",+ "name": "response",+ "arguments": "",+ },+ }+ )+ chunks = [+ parser.parse(+ {+ "type": "response.function_call_arguments.delta",+ "item_id": "fc_1",+ "delta": '{"text":"Hello',+ }+ )["response_delta"],+ parser.parse(+ {+ "type": "response.function_call_arguments.delta",+ "item_id": "fc_1",+ "delta": ' world"}',+ }+ )["response_delta"],+ parser.parse(+ {+ "type": "response.function_call_arguments.done",+ "item_id": "fc_1",+ "name": "response",+ "arguments": '{"text":"Hello world"}',+ }+ )["response_delta"],+ ]++ assert chunks[0] == '{"tool_name":"response","tool_args":{"text":"Hello'+ assert DirtyJson.parse_string(chunks[0]) == {+ "tool_name": "response",+ "tool_args": {"text": "Hello"},+ }+ assert extract_tools.json_parse_dirty("".join(chunks)) == {+ "tool_name": "response",+ "tool_args": {"text": "Hello world"},+ }+ assert chunks[-1] == "}"+ assert parser.parse(+ {+ "type": "response.output_item.done",+ "item": {+ "type": "function_call",+ "id": "fc_1",+ "name": "response",+ "arguments": '{"text":"Hello world"}',+ },+ }+ ) == {"reasoning_delta": "", "response_delta": ""}++def test_responses_stream_parser_uses_completed_response_when_no_deltas_arrive():parser = litellm_transport.ResponsesEventParser()