Skip to content

Agent Zero AI framework

fix(tools): harden malformed text tool handling

Alessandro6b7302f6642e
diff --git a/agent.py b/agent.py
index f8c902da7..134f8dc39 100644
--- a/agent.py
+++ b/agent.py
@@ -447,24 +447,15 @@ class Agent:
printer.print("Response: ") # start of response
# Pass chunk and full data to extensions for processing
stream_data = {"chunk": chunk, "full": full}
- stop_response: str | None = None
-
- snapshot = extract_tools.extract_json_root_string(full)
- if snapshot:
- parsed_snapshot = extract_tools.json_parse_dirty(snapshot)
- if parsed_snapshot is not None:
- try:
- await self.validate_tool_request(parsed_snapshot)
- except Exception:
- pass
- else:
- previous_full = last_response_stream_full
- stream_data["full"] = snapshot
- if snapshot.startswith(previous_full):
- stream_data["chunk"] = snapshot[len(previous_full) :]
- else:
- stream_data["chunk"] = snapshot
- stop_response = snapshot
+ tool_request = extract_tools.extract_tool_request(full)
+ if tool_request is not None:
+ try:
+ await self.validate_tool_request(tool_request)
+ except Exception:
+ pass
+ else:
+ await self.handle_response_stream(full)
+ return full.strip()
await extension.call_extensions_async(
"response_stream_chunk",
@@ -478,8 +469,6 @@ class Agent:
# Use the potentially modified full text for downstream processing
await self.handle_response_stream(stream_data["full"])
last_response_stream_full = stream_data["full"]
- if stop_response is not None:
- return stop_response
# call main LLM
llm_result = await self.call_chat_model_turn(
@@ -1123,13 +1112,18 @@ class Agent:
return None
if llm_result.builtin_items and not llm_result.response:
return None
- if (
- llm_result.mode == "responses"
- and llm_result.response
- and extract_tools.json_parse_dirty(llm_result.response) is None
- ):
- return llm_result.response
- return await self.process_tools(llm_result.response)
+ message = llm_result.response
+ if not message and llm_result.reasoning:
+ if (
+ extract_tools.extract_tool_request(llm_result.reasoning) is not None
+ or extract_tools.is_misformatted_tool_request(llm_result.reasoning)
+ ):
+ message = llm_result.reasoning
+ if extract_tools.extract_tool_request(message) is None:
+ if extract_tools.is_misformatted_tool_request(message):
+ return await self.process_tools(message)
+ return message
+ return await self.process_tools(message)
async def _execute_tool_request(
self,
@@ -1408,7 +1402,7 @@ class Agent:
@extension.extensible
async def process_tools(self, msg: str):
# search for tool usage requests in agent message
- tool_request = extract_tools.json_parse_dirty(msg)
+ tool_request = extract_tools.extract_tool_request(msg)
raw_tool_name = ""
tool_args = {}
@@ -1417,6 +1411,7 @@ class Agent:
# block was found - the misformat warning path below handles that.
if tool_request is not None:
try:
+ await self.validate_tool_request(tool_request)
raw_tool_name, tool_args = extract_tools.normalize_tool_request(
tool_request
)
@@ -1458,6 +1453,7 @@ class Agent:
)
if tool:
+ tool.args = tool_args
self.loop_data.current_tool = tool # type: ignore
try:
await self.handle_intervention()
diff --git a/extensions/python/_functions/agent/Agent/hist_add_ai_response/end/_10_log_plain_responses.py b/extensions/python/_functions/agent/Agent/hist_add_ai_response/end/_10_log_plain_responses.py
index be026d7e2..e11e660f6 100644
--- a/extensions/python/_functions/agent/Agent/hist_add_ai_response/end/_10_log_plain_responses.py
+++ b/extensions/python/_functions/agent/Agent/hist_add_ai_response/end/_10_log_plain_responses.py
@@ -23,7 +23,7 @@ class LogPlainResponses(Extension):
message = call_args[1]
if not isinstance(message, str) or not message:
return
- if extract_tools.json_parse_dirty(message) is not None:
+ if extract_tools.extract_tool_request(message) is not None:
return
params = getattr(getattr(self.agent, "loop_data", None), "params_temporary", None)
diff --git a/extensions/python/response_stream/_20_live_response.py b/extensions/python/response_stream/_20_live_response.py
index ecabb8a48..3d7fafb1b 100644
--- a/extensions/python/response_stream/_20_live_response.py
+++ b/extensions/python/response_stream/_20_live_response.py
@@ -1,4 +1,5 @@
from helpers import persist_chat, tokens
+from helpers import extract_tools
from helpers.extension import Extension
from agent import LoopData
import asyncio
@@ -19,13 +20,8 @@ class LiveResponse(Extension):
return
try:
- if (
- not "tool_name" in parsed
- or parsed["tool_name"] != "response"
- or "tool_args" not in parsed
- or "text" not in parsed["tool_args"]
- or not parsed["tool_args"]["text"]
- ):
+ tool_name, tool_args = extract_tools.normalize_tool_request(parsed)
+ if tool_name != "response" or not tool_args.get("text"):
return # not a response
# create log message and store it in loop data temporary params
@@ -43,6 +39,6 @@ class LiveResponse(Extension):
# update log message
log_item = loop_data.params_temporary["log_item_response"]
- log_item.update(content=parsed["tool_args"]["text"])
+ log_item.update(content=tool_args["text"])
except Exception as e:
pass
diff --git a/helpers/extract_tools.py b/helpers/extract_tools.py
index 0c49df968..55a1a67e0 100644
--- a/helpers/extract_tools.py
+++ b/helpers/extract_tools.py
@@ -20,17 +20,88 @@ def json_parse_dirty(json: str) -> dict[str, Any] | None:
return first_data
+def extract_tool_request(content: str) -> dict[str, Any] | None:
+ if not content or not isinstance(content, str):
+ return None
+
+ content = content.strip()
+ root = extract_json_root_string(content)
+ if root != content:
+ return None
+
+ request = _parse_json_root_object(root)
+ return request if request is not None and _is_tool_request(request) else None
+
+
+def is_misformatted_tool_request(content: str) -> bool:
+ if not content or not isinstance(content, str):
+ return False
+
+ content = content.strip()
+ for fenced_content in re.findall(
+ r"```(?:json)?\s*(.*?)```", content, flags=re.IGNORECASE | re.DOTALL
+ ):
+ request = json_parse_dirty(fenced_content)
+ if isinstance(request, dict) and _is_tool_request(request):
+ return True
+
+ if (
+ not content.endswith("}")
+ or re.match(r'^\{\s*"thoughts"\s*:', content) is None
+ ):
+ return False
+
+ request = json_parse_dirty(content)
+ thoughts = request.get("thoughts") if isinstance(request, dict) else None
+ thoughts_text = (
+ "\n".join(thought for thought in thoughts if isinstance(thought, str))
+ if isinstance(thoughts, list)
+ else ""
+ )
+ return (
+ isinstance(thoughts, list)
+ and all(
+ f'{field}\":' in thoughts_text
+ for field in ("headline", "tool_name", "tool_args")
+ )
+ )
+
+
def normalize_tool_request(tool_request: Any) -> tuple[str, dict]:
if not isinstance(tool_request, dict):
raise ValueError("Tool request must be a dictionary")
+ if (
+ not tool_request.get("tool_name")
+ and not tool_request.get("tool")
+ and "actions" in tool_request
+ ):
+ actions = tool_request["actions"]
+ # Text tool calls allow one request per turn; do not silently discard extras.
+ if (
+ not isinstance(actions, list)
+ or len(actions) != 1
+ or not isinstance(actions[0], dict)
+ ):
+ raise ValueError(
+ "Tool request actions wrapper must contain exactly one dictionary"
+ )
+ tool_request = actions[0]
+
tool_name = tool_request.get("tool_name")
if not tool_name or not isinstance(tool_name, str):
tool_name = tool_request.get("tool")
+ if (
+ (not tool_name or not isinstance(tool_name, str))
+ and tool_request.get("type") == "function"
+ ):
+ tool_name = tool_request.get("name")
if not tool_name or not isinstance(tool_name, str):
raise ValueError("Tool request must have a tool_name (type string) field")
tool_args = tool_request.get("tool_args")
if not isinstance(tool_args, dict):
tool_args = tool_request.get("args")
+ if not isinstance(tool_args, dict) and tool_request.get("type") == "function":
+ tool_args = tool_request.get("parameters")
if not isinstance(tool_args, dict):
raise ValueError("Tool request must have a tool_args (type dictionary) field")
tool_args = dict(tool_args)
diff --git a/helpers/extract_tools.py.dox.md b/helpers/extract_tools.py.dox.md
index d932d79c2..adfc19881 100644
--- a/helpers/extract_tools.py.dox.md
+++ b/helpers/extract_tools.py.dox.md
@@ -12,6 +12,8 @@
- `extract_tools.py.dox.md` owns durable notes about responsibilities, contracts, side effects, and verification for that implementation.
- Top-level functions:
- `json_parse_dirty(json: str) -> dict[str, Any] | None`
+- `extract_tool_request(content: str) -> dict[str, Any] | None`
+- `is_misformatted_tool_request(content: str) -> bool`
- `normalize_tool_request(tool_request: Any) -> tuple[str, dict]`
- `extract_json_root_string(content: str) -> str | None`
- `extract_json_root_strings(content: str) -> list[str]`
@@ -24,8 +26,11 @@
- Helper modules own reusable framework APIs and must preserve public callers unless all callers, tests, and docs are updated together.
- Update this file whenever public functions, classes, persistence behavior, path/security assumptions, side effects, or cross-module contracts change.
- Observed side-effect areas: settings/state persistence.
-- Dirty parsing scans complete JSON object roots in prose and prefers the first object that normalizes as a valid tool request, so a leading text preamble or incidental non-tool object does not force a misformat warning when a valid tool call follows.
-- Streaming tool snapshots use the same valid-tool preference through `extract_json_root_string`, while preserving the first complete object fallback when no valid tool-call object is present.
+- 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.
+- `is_misformatted_tool_request` identifies either a tool request wrapped in a JSON code fence 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.
- Imported dependency areas include: `dirty_json`, `helpers.modules`, `re`, `regex`, `typing`.
diff --git a/models.py b/models.py
index 057aa1129..805f79c79 100644
--- a/models.py
+++ b/models.py
@@ -738,14 +738,9 @@ class LiteLLMChatWrapper(SimpleChatModel):
output["response_delta"]
)
)
- if (
- stop_response is not None
- and not transport.policy.using_responses
- ):
- result.response = stop_response
- break
if stop_response is not None:
result.response = stop_response
+ break
else:
parsed = await transport.acomplete()
output = result.add_chunk(parsed)
diff --git a/tests/test_plain_response_logging.py b/tests/test_plain_response_logging.py
index 9e444d2bf..7a0d6322e 100644
--- a/tests/test_plain_response_logging.py
+++ b/tests/test_plain_response_logging.py
@@ -1,6 +1,8 @@
+import pytest
from types import SimpleNamespace
from helpers.log import Log
+from extensions.python.response_stream._20_live_response import LiveResponse
from extensions.python._functions.agent.Agent.hist_add_ai_response.end._10_log_plain_responses import (
LogPlainResponses,
)
@@ -50,6 +52,20 @@ def test_responses_tool_json_keeps_generating_log_as_agent_step():
assert "log_item_response" not in agent.loop_data.params_temporary
+def test_responses_plain_json_completion_finishes_generating_log_as_response():
+ agent, item = _agent_with_generating_log()
+ data = {
+ "args": (agent, '{"status":"ok"}'),
+ "kwargs": {"id": "msg-1", "llm_result": SimpleNamespace(mode="responses")},
+ }
+
+ LogPlainResponses(agent=agent).execute(data=data)
+
+ assert item.type == "response"
+ assert item.content == '{"status":"ok"}'
+ assert agent.loop_data.params_temporary["log_item_response"] is item
+
+
def test_responses_plain_text_completion_does_not_replace_live_response_log():
agent, item = _agent_with_generating_log()
live_response = Log().log(type="response", content="Already live")
@@ -64,3 +80,28 @@ def test_responses_plain_text_completion_does_not_replace_live_response_log():
assert item.type == "agent"
assert item.content == ""
assert agent.loop_data.params_temporary["log_item_response"] is live_response
+
+
+async def test_live_response_renders_single_action_wrapper():
+ log = Log()
+ generating = log.log(type="agent", id="msg-1")
+ loop_data = SimpleNamespace(params_temporary={"log_item_generating": generating})
+ agent = SimpleNamespace(
+ context=SimpleNamespace(log=log),
+ agent_name="A0",
+ )
+
+ await LiveResponse(agent=agent).execute(
+ loop_data=loop_data,
+ parsed={
+ "actions": [
+ {"tool_name": "response", "tool_args": {"text": "wrapper works"}}
+ ]
+ },
+ )
+
+ response = loop_data.params_temporary["log_item_response"]
+ assert response.type == "response"
+ assert response.content == "wrapper works"
+ assert response.id == "msg-1"
diff --git a/tests/test_responses_architecture.py b/tests/test_responses_architecture.py
index c893f29e9..48c7e8833 100644
--- a/tests/test_responses_architecture.py
+++ b/tests/test_responses_architecture.py
@@ -1,3 +1,4 @@
+import asyncio
import sys
from pathlib import Path
@@ -11,7 +12,7 @@ if str(PROJECT_ROOT) not in sys.path:
import models
from agent import Agent, AgentConfig, AgentContextType, LoopData
-from helpers import history, litellm_transport
+from helpers import extract_tools, history, litellm_transport
from helpers.log import Log
from helpers.llm_result import LLMResult, result_from_metadata
from helpers.persist_chat import _collect_response_ids
@@ -43,6 +44,28 @@ class _AsyncEventStream:
self.closed = True
+class _StallingAsyncEventStream:
+ def __init__(self, event: dict):
+ self.event = event
+ self.sent = False
+ self.closed = False
+ self._stalled = asyncio.Event()
+
+ def __aiter__(self):
+ return self
+
+ async def __anext__(self):
+ if not self.sent:
+ self.sent = True
+ return self.event
+ await self._stalled.wait()
+ raise StopAsyncIteration
+
+ async def aclose(self):
+ self.closed = True
+ self._stalled.set()
+
+
def test_llm_result_persists_only_durable_responses_metadata():
result = LLMResult.from_response(
{
@@ -248,7 +271,7 @@ async def test_transport_downgrades_unsupported_builtin_tools(monkeypatch):
@pytest.mark.asyncio
-async def test_unified_turn_keeps_stream_open_to_capture_response_id(monkeypatch):
+async def test_unified_turn_captures_response_id_without_stop_request(monkeypatch):
stream = _AsyncEventStream(
[
{
@@ -303,7 +326,7 @@ async def test_unified_turn_keeps_stream_open_to_capture_response_id(monkeypatch
)
async def response_callback(chunk: str, full: str):
- return full
+ return None
result = await wrapper.unified_turn(
messages=[HumanMessage(content="hi")],
@@ -316,6 +339,46 @@ async def test_unified_turn_keeps_stream_open_to_capture_response_id(monkeypatch
assert result.function_calls[0].call_id == "call_1"
+async def test_unified_turn_stops_responses_stream_after_callback_stop(monkeypatch):
+ message = (
+ '{"thoughts":["test"],"actions":['
+ '{"tool_name":"response","tool_args":{"text":"ok"}}]}'
+ )
+ stream = _StallingAsyncEventStream(
+ {"type": "response.output_text.delta", "delta": message}
+ )
+
+ async def fake_aresponses(*args, **kwargs):
+ return stream
+
+ async def fake_rate_limiter(*args, **kwargs):
+ return None
+
+ monkeypatch.setattr(litellm_transport, "aresponses", fake_aresponses)
+ monkeypatch.setattr(models, "apply_rate_limiter", fake_rate_limiter)
+
+ wrapper = models.LiteLLMChatWrapper(
+ model="test-model",
+ provider="openai",
+ model_config=None,
+ )
+
+ async def response_callback(chunk: str, full: str):
+ return full if extract_tools.extract_tool_request(full) else None
+
+ result = await asyncio.wait_for(
+ wrapper.unified_turn(
+ messages=[HumanMessage(content="hi")],
+ response_callback=response_callback,
+ ),
+ timeout=1,
+ )
+
+ assert result.response == message
+ assert stream.closed is True
+
+
def test_collect_response_ids_from_agent_state_and_history_metadata():
payload = {
"agents": [
@@ -411,3 +474,137 @@ async def test_agent_executes_native_responses_function_call_and_records_output(
"output": "done:a0",
}
]
+
+
+async def test_agent_routes_only_complete_text_tool_requests() -> None:
+ agent = object.__new__(Agent)
+ processed: list[str] = []
+
+ async def log_builtin_items(result):
+ return None
+
+ async def process_tools(message):
+ processed.append(message)
+ return message
+
+ agent._log_response_builtin_items = log_builtin_items
+ agent.process_tools = process_tools
+
+ tool_request = '{"type":"function","name":"response","parameters":{"text":"ok"}}'
+ for message in (
+ "Plain final answer.",
+ '{"status":"planning"}',
+ f"Example tool JSON: {tool_request}",
+ ):
+ assert await Agent.process_llm_result_tools(
+ agent, LLMResult.from_chat(response=message)
+ ) == message
+ assert processed == []
+
+ assert await Agent.process_llm_result_tools(
+ agent, LLMResult.from_chat(response=tool_request)
+ ) == tool_request
+ assert processed == [tool_request]
+
+ processed.clear()
+ assert await Agent.process_llm_result_tools(
+ agent, LLMResult(response="", reasoning=tool_request)
+ ) == tool_request
+ assert processed == [tool_request]
+
+ processed.clear()
+ assert await Agent.process_llm_result_tools(
+ agent, LLMResult(response="", reasoning='{"status":"planning"}')
+ ) == ""
+ assert processed == []
+
+
+async def test_agent_routes_misformatted_tool_intent_to_repair() -> None:
+ agent = object.__new__(Agent)
+ processed: list[str] = []
+
+ async def log_builtin_items(result):
+ return None
+
+ async def process_tools(message):
+ processed.append(message)
+ return None
+
+ agent._log_response_builtin_items = log_builtin_items
+ agent.process_tools = process_tools
+
+ malformed = (
+ '{"thoughts":["Plan the work", "Run the tools", '
+ '"headline":"Save results", "tool_name":"parallel", '
+ '"tool_args":{"tool_calls":[{"tool_name":"memory_save",'
+ '"tool_args":{"text":"ok"}}],"wait":true}}'
+ )
+
+ assert await Agent.process_llm_result_tools(
+ agent, LLMResult.from_chat(response=malformed)
+ ) is None
+ assert processed == [malformed]
+
+ processed.clear()
+ assert await Agent.process_llm_result_tools(
+ agent, LLMResult(response="", reasoning=malformed)
+ ) is None
+ assert processed == [malformed]
+
+ fenced = (
+ "I will call the tool.\n\n```json\n"
+ '{"tool_name":"response","tool_args":{"text":"ok"}}\n```'
+ )
+ processed.clear()
+ assert await Agent.process_llm_result_tools(
+ agent, LLMResult.from_chat(response=fenced)
+ ) is None
+ assert processed == [fenced]
+
+
+async def test_text_tool_execution_uses_normalized_tool_args(monkeypatch) -> None:
+ class DummyMCPConfig:
+ def get_tool(self, agent, tool_name):
+ return None
+
+ class DummyTool:
+ def __init__(self):
+ self.args = {}
+
+ async def before_execution(self, **kwargs):
+ assert self.args == {"text": "ok"}
+
+ async def execute(self, **kwargs):
+ assert kwargs == {"text": "ok"}
+ return Response(message=self.args["text"], break_loop=True)
+
+ async def after_execution(self, response):
+ return None
+
+ async def no_extension(*args, **kwargs):
+ return None
+
+ async def no_intervention(*args, **kwargs):
+ return None
+
+ import agent as agent_module
+ from helpers import mcp_handler
+
+ monkeypatch.setattr(
+ mcp_handler.MCPConfig, "get_instance", lambda: DummyMCPConfig()
+ )
+ monkeypatch.setattr(agent_module.extension, "call_extensions_async", no_extension)
+
+ tool = DummyTool()
+ agent = object.__new__(Agent)
+ agent.data = {}
+ agent.loop_data = LoopData()
+ agent.handle_intervention = no_intervention
+ agent.get_tool = lambda **kwargs: tool
+
+ assert await Agent.process_tools(
+ agent, '{"actions":[{"tool_name":"response","tool_args":{"text":"ok"}}]}'
+ ) == "ok"
diff --git a/tests/test_stream_tool_early_stop.py b/tests/test_stream_tool_early_stop.py
index 854779d39..b4235204a 100644
--- a/tests/test_stream_tool_early_stop.py
+++ b/tests/test_stream_tool_early_stop.py
@@ -239,9 +239,7 @@ async def test_unified_call_stops_after_canonical_root_snapshot(monkeypatch):
stream = _AsyncChunkStream(
[
{"type": "response.created"},
- _response_event(
- '{"tool_name":"response","tool_args":{"text":"hello"}} trailing text'
- ),
+ _response_event('{"tool_name":"response","tool_args":{"text":"hello"}}'),
_response_event(" unreachable"),
]
)
@@ -273,10 +271,7 @@ async def test_unified_call_stops_after_canonical_root_snapshot(monkeypatch):
async def response_callback(chunk: str, full: str):
seen.append((chunk, full))
- snapshot = extract_tools.extract_json_root_string(full)
- if snapshot:
- return snapshot
- return None
+ return full.strip() if extract_tools.extract_tool_request(full) else None
response, reasoning = await wrapper.unified_call(
messages=[],
@@ -288,11 +283,11 @@ async def test_unified_call_stops_after_canonical_root_snapshot(monkeypatch):
assert stream.index == 2
assert stream.closed is True
assert len(seen) == 1
- assert seen[0][1] == '{"tool_name":"response","tool_args":{"text":"hello"}} trailing text'
+ assert seen[0][1] == '{"tool_name":"response","tool_args":{"text":"hello"}}'
@pytest.mark.asyncio
-async def test_unified_call_stops_after_tool_root_with_incidental_json(monkeypatch):
+async def test_unified_call_does_not_stop_for_embedded_tool_json(monkeypatch):
stream = _AsyncChunkStream(
[
{"type": "response.created"},
@@ -331,28 +326,21 @@ async def test_unified_call_stops_after_tool_root_with_incidental_json(monkeypat
async def response_callback(chunk: str, full: str):
seen.append((chunk, full))
- snapshot = extract_tools.extract_json_root_string(full)
- if not snapshot:
- return None
- parsed_snapshot = extract_tools.json_parse_dirty(snapshot)
- if parsed_snapshot is None:
- return None
- try:
- extract_tools.normalize_tool_request(parsed_snapshot)
- except ValueError:
- return None
- return snapshot
+ return full.strip() if extract_tools.extract_tool_request(full) else None
response, reasoning = await wrapper.unified_call(
messages=[],
response_callback=response_callback,
)
- assert response == '{"tool_name":"response","tool_args":{"text":"ok"}}'
+ assert response == (
+ 'Preamble {"note":"not the tool"}.\n'
+ '{"tool_name":"response","tool_args":{"text":"ok"}} trailing text unreachable'
+ )
assert reasoning == ""
- assert stream.index == 3
- assert stream.closed is True
- assert len(seen) == 2
+ assert stream.index == 4
+ assert stream.closed is False
+ assert len(seen) == 3
assert seen[0][1] == 'Preamble {"note":"not the tool"}.\n'
assert (
seen[1][1]
@@ -437,6 +425,44 @@ async def test_chat_completions_escape_hatch_still_uses_acompletion(monkeypatch)
assert calls == ["chat"]
+async def test_unified_turn_stops_chat_stream_after_text_tool_request(monkeypatch):
+ message = (
+ '{"thoughts":["test"],"actions":['
+ '{"tool_name":"response","tool_args":{"text":"ok"}}]}'
+ )
+ stream = _AsyncChunkStream([_chunk(message), _chunk(" unreachable")])
+
+ async def fake_acompletion(*args, **kwargs):
+ assert kwargs["stream"] is True
+ return stream
+
+ async def fake_rate_limiter(*args, **kwargs):
+ return None
+
+ monkeypatch.setattr(litellm_transport, "acompletion", fake_acompletion)
+ monkeypatch.setattr(models, "apply_rate_limiter", fake_rate_limiter)
+
+ wrapper = models.LiteLLMChatWrapper(
+ model="test-model",
+ provider="openai",
+ model_config=None,
+ a0_api_mode="chat",
+ )
+
+ async def response_callback(chunk: str, full: str):
+ return full if extract_tools.extract_tool_request(full) else None
+
+ result = await wrapper.unified_turn(
+ messages=[],
+ response_callback=response_callback,
+ )
+
+ assert result.response == message
+ assert stream.index == 1
+ assert stream.closed is True
+
+
@pytest.mark.asyncio
async def test_unified_call_retries_responses_with_high_reasoning(monkeypatch):
validation_error = ValueError(
diff --git a/tests/test_tool_request_normalization.py b/tests/test_tool_request_normalization.py
index 6b4ce1c4e..e04b9b889 100644
--- a/tests/test_tool_request_normalization.py
+++ b/tests/test_tool_request_normalization.py
@@ -9,7 +9,12 @@ PROJECT_ROOT = Path(__file__).resolve().parents[1]
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
-from helpers.extract_tools import normalize_tool_request
+from helpers.extract_tools import (
+ extract_tool_request,
+ is_misformatted_tool_request,
+ json_parse_dirty,
+ normalize_tool_request,
+)
from helpers import parallel_tools
@@ -68,6 +73,81 @@ def test_normalize_tool_request_rejects_missing_args() -> None:
normalize_tool_request({"tool_name": "response"})
+def test_normalize_tool_request_accepts_native_function_format() -> None:
+ request = {
+ "type": "function",
+ "name": "search_engine",
+ "parameters": {"query": "latest Agent Zero release"},
+ }
+
+ assert json_parse_dirty(str(request)) == request
+ assert normalize_tool_request(request) == (
+ "search_engine",
+ {"query": "latest Agent Zero release"},
+ )
+
+
+def test_normalize_tool_request_accepts_single_action_wrapper() -> None:
+ request = {
+ "thoughts": ["Read the requested file."],
+ "actions": [
+ {
+ "tool_name": "text_editor",
+ "tool_args": {"action": "read", "path": "README.md"},
+ }
+ ],
+ }
+
+ assert json_parse_dirty(str(request)) == request
+ assert normalize_tool_request(request) == (
+ "text_editor",
+ {"action": "read", "path": "README.md"},
+ )
+
+
+def test_normalize_tool_request_rejects_multiple_wrapped_actions() -> None:
+ with pytest.raises(ValueError, match="exactly one"):
+ normalize_tool_request(
+ {
+ "actions": [
+ {"tool_name": "response", "tool_args": {"text": "first"}},
+ {"tool_name": "response", "tool_args": {"text": "second"}},
+ ]
+ }
+ )
+
+
+def test_extract_tool_request_requires_a_complete_tool_message() -> None:
+ request = '{"tool_name":"response","tool_args":{"text":"ok"}}'
+
+ assert extract_tool_request(request) == {
+ "tool_name": "response",
+ "tool_args": {"text": "ok"},
+ }
+ assert extract_tool_request('{"status":"ok"}') is None
+ assert extract_tool_request(f"Example: {request}") is None
+ assert extract_tool_request(f"{request} trailing text") is None
+
+
+def test_is_misformatted_tool_request_requires_agent_tool_envelope() -> None:
+ request = '{"tool_name":"response","tool_args":{"text":"ok"}}'
+ malformed = (
+ '{"thoughts":["Plan the work", "Run the tools", '
+ '"headline":"Save results", "tool_name":"parallel", '
+ '"tool_args":{"tool_calls":[{"tool_name":"memory_save",'
+ '"tool_args":{"text":"ok"}}],"wait":true}}'
+ )
+
+ assert extract_tool_request(malformed) is None
+ assert is_misformatted_tool_request(malformed) is True
+ assert is_misformatted_tool_request(f"Intro\n```json\n{request}\n```") is True
+ assert is_misformatted_tool_request('{"status":"planning"}') is False
+ assert is_misformatted_tool_request(f"Example: {request}") is False
+ assert is_misformatted_tool_request(
+ malformed.replace('{"thoughts"', '{"status":"planning","thoughts"')
+ ) is False
+
+
def test_normalize_parallel_tool_calls_accepts_full_agent_reply_shape() -> None:
calls = parallel_tools.normalize_parallel_tool_calls(
[