@devries-cameron20 / composio-1 / commits / 24918adc0
Fork 0

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(tool-router): harden multi-execute edge cases

Dhawal Upadhyay committed Mar 24, 2026 at 15:06 UTC 24918adc011820940e5fcff84c64e4ce9b43c862
5 files changed +142 -37
python/composio/core/models/custom_tool.py
-6
@@ -391,12 +391,6 @@ class ExperimentalToolkit:
391 output_params=output_params,
392 # No extends_toolkit for toolkit tools
393 )
394 - if custom_tool.extends_toolkit:
395 - raise ValidationError(
396 - f'experimental.Toolkit.tool: tool "{custom_tool.slug}" has '
397 - f"extends_toolkit set. Tools in a custom toolkit must not "
398 - f"use extends_toolkit — they inherit the toolkit identity."
399 - )
394 _validate_slug_length(
395 custom_tool.slug, self.slug, f'experimental.Toolkit("{self.slug}").tool'
396 )
python/composio/core/models/tool_router_session.py
+22 -12
@@ -169,6 +169,10 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
169 Applies before_execute/after_execute modifiers around the overall
170 COMPOSIO_MULTI_EXECUTE_TOOL call, consistent with the standard path.
171 """
172 + backend_execute = tools_model._wrap_execute_tool_for_tool_router(
173 + session_id=self.session_id,
174 + modifiers=modifiers,
175 + )
176
177 def routing_execute(slug: str, arguments: t.Dict) -> t.Dict:
178 if slug == COMPOSIO_MULTI_EXECUTE_TOOL:
@@ -204,10 +208,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
208
209 return result
210 # Non-multi-execute meta tools always go to backend
207 - return tools_model._wrap_execute_tool_for_tool_router(
208 - session_id=self.session_id,
209 - modifiers=modifiers,
210 - )(slug, arguments)
211 + return backend_execute(slug, arguments)
212
213 return routing_execute
214
@@ -323,26 +324,35 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
324 local_entries.append(local_entry)
325
326 # Merge: remotes first, locals appended (matches TS behavior —
326 - # remote results may be stored in workbench with index references)
327 - remote_data = (remote_result or {}).get("data", {})
327 + # remote results may have workbench index references)
328 + remote_data_raw = (remote_result or {}).get("data")
329 + remote_data = remote_data_raw if isinstance(remote_data_raw, dict) else {}
330 remote_results_list = (
329 - remote_data.get("results", []) if isinstance(remote_data, dict) else []
331 + remote_data.get("results", []) if isinstance(remote_data.get("results"), list) else []
332 )
333 all_results = [
334 {**entry, "index": i}
335 for i, entry in enumerate([*remote_results_list, *local_entries])
336 ]
337
336 - has_any_error = any(r.get("error") for _, r in local_results) or bool(
337 - remote_result and remote_result.get("error")
338 + remote_error = (
339 + str(remote_result.get("error"))
340 + if remote_result and remote_result.get("error") is not None
341 + else None
342 )
343 + has_any_error = any(r.get("error") for _, r in local_results) or bool(remote_error)
344 failed = sum(1 for r in all_results if r.get("error"))
345 + error_message = None
346 + if has_any_error:
347 + error_message = (
348 + remote_error
349 + if remote_error is not None and failed == 0
350 + else f"{failed} out of {len(all_results)} tools failed"
351 + )
352
353 return {
354 "data": {**remote_data, "results": all_results},
343 - "error": f"{failed} out of {len(all_results)} tools failed"
344 - if has_any_error
345 - else None,
355 + "error": error_message,
356 "successful": not has_any_error,
357 }
358
python/tests/test_custom_tools.py
+41
@@ -700,3 +700,44 @@ class TestMultiExecuteRouting:
700 )
701 assert result["successful"] is False
702 assert "1 out of 2" in result["error"]
703 +
704 + def test_remote_null_data_does_not_crash(self, grep_tool):
705 + s = self._make_session(grep_tool)
706 + tm = MagicMock()
707 + remote = {"data": None, "error": None, "successful": True}
708 + tm._wrap_execute_tool_for_tool_router.return_value = lambda slug, args: remote
709 + result = s._route_multi_execute(
710 + {
711 + "tools": [
712 + {"tool_slug": "GREP", "arguments": {"pattern": "x"}},
713 + {"tool_slug": "REMOTE", "arguments": {}},
714 + ]
715 + },
716 + tm,
717 + )
718 + assert result["successful"] is True
719 + assert result["data"]["results"][0]["tool_slug"] == "GREP"
720 +
721 + def test_remote_batch_error_without_item_errors_uses_batch_message(self, grep_tool):
722 + s = self._make_session(grep_tool)
723 + tm = MagicMock()
724 + remote = {
725 + "data": None,
726 + "error": "Remote batch failed before per-tool results were produced",
727 + "successful": False,
728 + }
729 + tm._wrap_execute_tool_for_tool_router.return_value = lambda slug, args: remote
730 + result = s._route_multi_execute(
731 + {
732 + "tools": [
733 + {"tool_slug": "GREP", "arguments": {"pattern": "x"}},
734 + {"tool_slug": "REMOTE", "arguments": {}},
735 + ]
736 + },
737 + tm,
738 + )
739 + assert result["successful"] is False
740 + assert (
741 + result["error"]
742 + == "Remote batch failed before per-tool results were produced"
743 + )
ts/packages/core/src/models/ToolRouterSession.ts
+36 -19
@@ -115,7 +115,7 @@ export class ToolRouterSession<
115 if (!this.config?.provider) {
116 throw new Error(
117 'A provider is required when using custom tools with session.tools(). ' +
118 - 'Pass a provider in the Composio constructor.'
118 + 'Pass a provider in the Composio constructor.'
119 );
120 }
121 return this.config.provider.wrapTools(tools, routingExecuteFn) as ReturnType<
@@ -313,7 +313,9 @@ export class ToolRouterSession<
313 * @param params - Proxy request parameters (toolkit, endpoint, method, body, headers/query params)
314 * @returns The proxied API response with status, data, headers
315 */
316 - async proxyExecute(params: SessionProxyExecuteParams): Promise<ToolRouterSessionProxyExecuteResponse> {
316 + async proxyExecute(
317 + params: SessionProxyExecuteParams
318 + ): Promise<ToolRouterSessionProxyExecuteResponse> {
319 const validated = SessionProxyExecuteParamsSchema.safeParse(params);
320 if (!validated.success) {
321 throw new ValidationError('Invalid proxy execute parameters', { cause: validated.error });
@@ -329,14 +331,16 @@ export class ToolRouterSession<
331 status: response.status,
332 data: response.data,
333 headers: response.headers,
332 - ...(response.binary_data ? {
333 - binaryData: {
334 - contentType: response.binary_data.content_type,
335 - size: response.binary_data.size,
336 - url: response.binary_data.url,
337 - expiresAt: response.binary_data.expires_at,
338 - },
339 - } : {}),
334 + ...(response.binary_data
335 + ? {
336 + binaryData: {
337 + contentType: response.binary_data.content_type,
338 + size: response.binary_data.size,
339 + url: response.binary_data.url,
340 + expiresAt: response.binary_data.expires_at,
341 + },
342 + }
343 + : {}),
344 };
345 }
346
@@ -432,8 +436,16 @@ export class ToolRouterSession<
436 }
437
438 // Merge results into the backend's results[] format.
435 - const remoteData = (remoteResult?.data ?? {}) as Record<string, unknown>;
436 - const remoteResults = (Array.isArray(remoteData.results) ? remoteData.results : []) as unknown[];
439 + const remoteData = (
440 + remoteResult?.data &&
441 + typeof remoteResult.data === 'object' &&
442 + !Array.isArray(remoteResult.data)
443 + ? remoteResult.data
444 + : {}
445 + ) as Record<string, unknown>;
446 + const remoteResults = (
447 + Array.isArray(remoteData.results) ? remoteData.results : []
448 + ) as unknown[];
449
450 // Build local result entries matching backend format
451 const localEntries = localResults.map(({ index, result }) => ({
@@ -451,19 +463,24 @@ export class ToolRouterSession<
463 ...(remoteResults as Array<Record<string, unknown>>),
464 ...localEntries,
465 ];
454 - const allResults = merged.map((entry, i): Record<string, unknown> => ({
455 - ...entry,
456 - index: i,
457 - }));
458 - const hasAnyError = localResults.some(r => r.result.error) || !!remoteResult?.error;
466 + const allResults = merged.map(
467 + (entry, i): Record<string, unknown> => ({
468 + ...entry,
469 + index: i,
470 + })
471 + );
472 + const remoteError = typeof remoteResult?.error === 'string' ? remoteResult.error : null;
473 + const hasAnyError = localResults.some(r => r.result.error) || !!remoteError;
474 + const failedCount = allResults.filter(r => r.error).length;
475
476 return {
477 data: { ...remoteData, results: allResults },
478 error: hasAnyError
463 - ? `${allResults.filter(r => r.error).length} out of ${allResults.length} tools failed`
479 + ? remoteError && failedCount === 0
480 + ? remoteError
481 + : `${failedCount} out of ${allResults.length} tools failed`
482 : null,
483 successful: !hasAnyError,
484 };
485 }
468 -
486 }
ts/packages/core/test/models/customToolRouting.test.ts
+43
@@ -671,6 +671,28 @@ describe('ToolRouterSession execution routing', () => {
671 expect(result.successful).toBe(true);
672 });
673
674 + it('should handle remote null data without crashing', async () => {
675 + const { executeFn, toolsInstance } = await setupMultiExecute(mockClient, [customToolHandle]);
676 +
677 + toolsInstance.executeMetaTool.mockResolvedValueOnce({
678 + data: null,
679 + error: null,
680 + successful: true,
681 + });
682 +
683 + const result = await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
684 + tools: [
685 + { tool_slug: 'LOCAL_GET_USER_CONTEXT', arguments: { category: 'x' } },
686 + { tool_slug: 'GMAIL_SEND_EMAIL', arguments: { to: 'a@b.com' } },
687 + ],
688 + sync_response_to_workbench: false,
689 + });
690 +
691 + expect(result.successful).toBe(true);
692 + expect(result.data.results).toHaveLength(1);
693 + expect(findResult(result.data.results, 'LOCAL_GET_USER_CONTEXT')).toBeDefined();
694 + });
695 +
696 it('should surface errors from both local and remote tools', async () => {
697 const throwingHandle = createCustomTool('MIXED_THROWER', {
698 name: 'Throws in batch',
@@ -725,6 +747,27 @@ describe('ToolRouterSession execution routing', () => {
747 expect(result.error).toContain('2 out of 4');
748 });
749
750 + it('should preserve batch-level remote errors when no per-tool remote results exist', async () => {
751 + const { executeFn, toolsInstance } = await setupMultiExecute(mockClient, [customToolHandle]);
752 +
753 + toolsInstance.executeMetaTool.mockResolvedValueOnce({
754 + data: null,
755 + error: 'Remote batch failed before per-tool results were produced',
756 + successful: false,
757 + });
758 +
759 + const result = await executeFn('COMPOSIO_MULTI_EXECUTE_TOOL', {
760 + tools: [
761 + { tool_slug: 'LOCAL_GET_USER_CONTEXT', arguments: { category: 'x' } },
762 + { tool_slug: 'GMAIL_SEND_EMAIL', arguments: { to: 'a@b.com' } },
763 + ],
764 + sync_response_to_workbench: false,
765 + });
766 +
767 + expect(result.successful).toBe(false);
768 + expect(result.error).toBe('Remote batch failed before per-tool results were produced');
769 + });
770 +
771 it('should forward to backend when tools array is empty', async () => {
772 const { executeFn, toolsInstance } = await setupMultiExecute(mockClient, [customToolHandle]);
773