Skip to content

Agent Zero AI framework

Skip incomplete stream roots during tool extraction

Alessandroa9e39ac04a39
diff --git a/helpers/extract_tools.py b/helpers/extract_tools.py
index e1996da34..3cca6a95c 100644
--- a/helpers/extract_tools.py
+++ b/helpers/extract_tools.py
@@ -25,6 +25,9 @@ def extract_tool_request(content: str) -> dict[str, Any] | None:
return None
content = content.strip()
+ if not (content.startswith("{") and content.endswith("}")):
+ return None
+
root = extract_json_root_string(content)
if root != content:
return None
diff --git a/helpers/extract_tools.py.dox.md b/helpers/extract_tools.py.dox.md
index d2892cb3b..ad3333ea3 100644
--- a/helpers/extract_tools.py.dox.md
+++ b/helpers/extract_tools.py.dox.md
@@ -29,6 +29,7 @@
- Dirty parsing scans complete JSON object roots in prose and prefers the first object that normalizes as a valid tool request for permissive repair and legacy callers.
Normalization accepts canonical `tool_name`/`tool_args`, legacy `tool`/`args`, native `type="function"` `name`/`parameters`, and a single-item `actions` wrapper; malformed or multi-action wrappers are rejected.
- `extract_tool_request` is the execution boundary: it accepts a request only when the complete trimmed content is one valid tool object. Plain text, ordinary JSON, and tool-shaped JSON embedded in prose remain final text.
+- `extract_tool_request` rejects content that does not have complete object boundaries before invoking the dirty root scanner. This keeps incomplete streaming prefixes cheap without changing which complete canonical tool objects are accepted.
- `is_misformatted_tool_request` identifies a tool request wrapped in a JSON code fence, concatenated complete roots containing tool intent, or a complete Agent Zero envelope that starts with `thoughts` and whose dirty parser has absorbed `headline`, `tool_name`, and `tool_args` into that list. It routes that output to the existing repair prompt without executing it.
- Streaming tool snapshots use `extract_tool_request`; the permissive root helpers remain available for repair and legacy callers, not tool execution.
- Root extraction ignores objects nested inside an open parent object, so streamed wrapper tools such as `parallel` cannot stop early on the first nested `tool_calls` item.
diff --git a/tests/test_stream_tool_early_stop.py b/tests/test_stream_tool_early_stop.py
index 78ba582ec..ae5cf22cf 100644
--- a/tests/test_stream_tool_early_stop.py
+++ b/tests/test_stream_tool_early_stop.py
@@ -87,6 +87,25 @@ def test_extract_json_root_string_returns_canonical_snapshot():
assert extract_tools.extract_json_root_string('[{"tool_name":"response"}]') is None
+ "content",
+ [
+ '{"tool_name":"response","tool_args":{"text":"partial"',
+ 'prefix {"tool_name":"response","tool_args":{}}',
+ '[{"tool_name":"response","tool_args":{}}]',
+ '```json\n{"tool_name":"response","tool_args":{}}\n```',
+ ],
+)
+def test_extract_tool_request_skips_noncanonical_boundaries(monkeypatch, content):
+ monkeypatch.setattr(
+ extract_tools,
+ "extract_json_root_string",
+ lambda _content: pytest.fail("noncanonical content reached the root scanner"),
+ )
+
+ assert extract_tools.extract_tool_request(content) is None
+
+
def test_json_parse_dirty_prefers_valid_tool_request_after_preamble_object():
text = (
'I will call the tool after this note {"note":"not the tool"}.\n'