Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
fix(python): clean up types and match TS merge order
- Remove unnecessary forward-ref quotes (from __future__ annotations already handles it) - Match TS multi-execute merge order: remotes first, locals appended (remote results may have workbench index references) - Simplify error logic to match TS: simple "X out of Y failed" message Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Dhawal Upadhyay committed
Mar 23, 2026 at 17:45 UTC
efcd1e46abf1840c9d9af8d7631c6e65484705f0
3 files changed
+38
-66
python/composio/core/models/tool_router.py
+2
-2
@@ -193,8 +193,8 @@ class ToolRouterExperimentalConfig(te.TypedDict, total=False):
193
"""
194
195
assistive_prompt: ToolRouterAssistivePromptConfig
196
- custom_tools: t.List["CustomTool"]
197
- custom_toolkits: t.List["ExperimentalToolkit"]
196
+ custom_tools: t.List[CustomTool]
197
+ custom_toolkits: t.List[ExperimentalToolkit]
198
199
200
@dataclass
python/composio/core/models/tool_router_session.py
+31
-63
@@ -223,13 +223,10 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
223
"""Route a COMPOSIO_MULTI_EXECUTE_TOOL call.
224
225
Splits the tools[] array into local and remote, executes each
226
- appropriately, and merges results preserving original order.
226
+ appropriately, and merges results: remotes first, locals appended
227
+ (matches TS — remote results may have workbench index references).
228
228
- Note: Results are merged in the original request order (index-based).
229
- This intentionally differs from TS which appends locals after remotes —
230
- preserving order is more predictable for callers.
231
-
232
- Note: modifiers are NOT applied here — the caller (routing_execute)
229
+ Modifiers are NOT applied here — the caller (routing_execute)
230
handles before_execute/after_execute to avoid double application.
231
"""
232
tool_items = input_args.get("tools")
@@ -305,71 +302,42 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
302
if not remote_indices and len(local_results) == 1:
303
return t.cast(t.Dict[str, t.Any], local_results[0][1])
304
308
- # Merge results into the backend's results[] format
309
- remote_data = (remote_result or {}).get("data", {})
310
- remote_results_list: t.List[t.Any] = (
311
- remote_data.get("results", []) if isinstance(remote_data, dict) else []
312
- )
313
-
305
# Build local result entries matching backend format
306
local_entries = []
307
for idx, result in local_results:
317
- local_entries.append(
318
- {
319
- "response": {
320
- "successful": result["successful"],
321
- "data": result["data"],
322
- **({"error": result["error"]} if result.get("error") else {}),
323
- },
324
- "tool_slug": parsed[idx]["tool_slug"],
325
- **({"error": result["error"]} if result.get("error") else {}),
326
- }
327
- )
328
-
329
- # Merge: local entries + remote results, re-indexed sequentially
330
- merged: t.List[t.Any] = []
331
- remote_iter = iter(remote_results_list)
332
- local_map = {idx: entry for (idx, _), entry in zip(local_items, local_entries)}
308
+ entry: t.Dict[str, t.Any] = {
309
+ "response": {
310
+ "successful": result["successful"],
311
+ "data": result["data"],
312
+ },
313
+ "tool_slug": parsed[idx]["tool_slug"],
314
+ }
315
+ if result.get("error"):
316
+ entry["response"]["error"] = result["error"]
317
+ entry["error"] = result["error"]
318
+ local_entries.append(entry)
319
334
- for i in range(len(parsed)):
335
- if i in local_map:
336
- merged.append(local_map[i])
337
- else:
338
- merged.append(next(remote_iter, {}))
339
-
340
- # Detect failures across local and remote results
341
- failed_count = sum(
342
- 1
343
- for entry in merged
344
- if isinstance(entry, dict)
345
- and isinstance(entry.get("response"), dict)
346
- and not entry["response"].get("successful", True)
347
- )
348
- has_any_error = failed_count > 0 or (
349
- remote_result is not None and not remote_result.get("successful", True)
320
+ # Merge: remotes first, locals appended (matches TS behavior —
321
+ # remote results may be stored in workbench with index references)
322
+ remote_data = (remote_result or {}).get("data", {})
323
+ remote_results_list = (
324
+ remote_data.get("results", []) if isinstance(remote_data, dict) else []
325
)
326
+ all_results = [
327
+ {**entry, "index": i}
328
+ for i, entry in enumerate([*remote_results_list, *local_entries])
329
+ ]
330
352
- # Preserve any extra metadata from the remote batch response
353
- merged_data: t.Dict[str, t.Any] = {}
354
- if isinstance(remote_data, dict):
355
- merged_data.update(remote_data)
356
- merged_data["results"] = merged
357
-
358
- # Build error message — include remote batch error if present
359
- remote_batch_error = remote_result.get("error") if remote_result else None
360
- if has_any_error:
361
- parts = []
362
- if failed_count > 0:
363
- parts.append(f"{failed_count} out of {len(merged)} tools failed")
364
- if remote_batch_error:
365
- parts.append(f"remote batch error: {remote_batch_error}")
366
- error_msg = "; ".join(parts) if parts else "execution failed"
367
- else:
368
- error_msg = None
331
+ has_any_error = any(r.get("error") for _, r in local_results) or bool(
332
+ remote_result and remote_result.get("error")
333
+ )
334
+ failed = sum(1 for r in all_results if r.get("error"))
335
336
return {
371
- "data": merged_data,
372
- "error": error_msg,
337
+ "data": {**remote_data, "results": all_results},
338
+ "error": f"{failed} out of {len(all_results)} tools failed"
339
+ if has_any_error
340
+ else None,
341
"successful": not has_any_error,
342
}
343
python/tests/test_custom_tools.py
+5
-1
@@ -605,7 +605,11 @@ class TestMultiExecuteRouting:
605
},
606
tm,
607
)
608
- assert len(result["data"]["results"]) == 2
608
+ results = result["data"]["results"]
609
+ assert len(results) == 2
610
+ # Remotes first, locals appended (matches TS)
611
+ assert results[0]["tool_slug"] == "R"
612
+ assert results[1]["tool_slug"] == "GREP"
613
614
def test_failure_propagated(self):
615
@exp.tool()