Skip to content

The agent that grows with you

fix(tools): parse JSON-string tool_call batch envelopes

Yagna Vudathu7765b196c5f3
diff --git a/tests/tools/test_connector_bridge_wiring.py b/tests/tools/test_connector_bridge_wiring.py
index 4ac1d7a0f93..2bd36a48122 100644
--- a/tests/tools/test_connector_bridge_wiring.py
+++ b/tests/tools/test_connector_bridge_wiring.py
@@ -106,6 +106,23 @@ def test_resolve_legacy_connector_single_shape_routes_to_sentinel():
assert len(args["calls"]) == 1
+def test_normalize_parses_string_envelope_batch():
+ """#114484: a model-emitted JSON-string batch envelope parses like the array form."""
+ calls = [{"name": "session_search", "arguments": {"query": "x"}}]
+ entries, err = normalize_tool_call_entries({"calls": json.dumps(calls)})
+ assert err is None
+ assert entries == calls
+
+
+def test_normalize_parses_string_envelope_single_dict():
+ """#114484: a stringified single dict normalizes to a batch of one."""
+ entries, err = normalize_tool_call_entries(
+ {"calls": json.dumps({"name": "session_search", "arguments": {"query": "x"}})}
+ )
+ assert err is None
+ assert entries == [{"name": "session_search", "arguments": {"query": "x"}}]
+
+
@pytest.mark.parametrize(
"bad,expected_fragment",
[
@@ -115,7 +132,7 @@ def test_resolve_legacy_connector_single_shape_routes_to_sentinel():
({"calls": [{"name": "tool_search"}]}, "itself a bridge tool"),
({"calls": [{"name": "x", "arguments": "not json {"}]}, "not valid JSON"),
({"calls": [{"name": "x", "arguments": 42}]}, "must be an object"),
- ({"calls": "nope"}, "non-empty array"),
+ ({"calls": "nope"}, "not valid JSON"),
],
)
def test_normalize_rejects_malformed_batches(bad, expected_fragment):
diff --git a/tools/tool_search_validation.py b/tools/tool_search_validation.py
index a2ee82b1e04..b6fd04630ed 100644
--- a/tools/tool_search_validation.py
+++ b/tools/tool_search_validation.py
@@ -151,6 +151,13 @@ def normalize_tool_call_entries(args: Dict[str, Any]) -> Tuple[List[Dict[str, An
if not str(args.get("name") or "").strip():
return [], "tool_call requires 'calls' (an array of {name, arguments})"
raw_calls = [{"name": args.get("name"), "arguments": args.get("arguments")}]
+ if isinstance(raw_calls, str):
+ # Tolerate the model emitting the batch envelope as a JSON string —
+ # mirror the per-entry `arguments` handling below (#114484).
+ try:
+ raw_calls = json.loads(raw_calls)
+ except json.JSONDecodeError as e:
+ return [], f"tool_call 'calls' is not valid JSON: {e}"
if isinstance(raw_calls, dict):
raw_calls = [raw_calls]
if not isinstance(raw_calls, list) or not raw_calls: