Skip to content

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): unify ctx.execute() to return SessionExecuteResponse

Dhawal Upadhyaycce496cc7948
diff --git a/python/composio/core/models/custom_tool_types.py b/python/composio/core/models/custom_tool_types.py
index 39a8dbe26..81bf391d4 100644
--- a/python/composio/core/models/custom_tool_types.py
+++ b/python/composio/core/models/custom_tool_types.py
@@ -12,12 +12,13 @@ from dataclasses import dataclass, field
import typing_extensions as te
from pydantic import BaseModel
+from composio_client.types.tool_router.session_execute_response import (
+ SessionExecuteResponse,
+)
from composio_client.types.tool_router.session_proxy_execute_response import (
SessionProxyExecuteResponse,
)
-from composio.core.models.tools import ToolExecutionResponse
-
# ────────────────────────────────────────────────────────────────
# Constants
# ────────────────────────────────────────────────────────────────
@@ -61,7 +62,7 @@ class SessionContext(te.Protocol):
self,
tool_slug: str,
arguments: t.Dict[str, t.Any],
- ) -> ToolExecutionResponse:
+ ) -> SessionExecuteResponse:
"""Execute any Composio tool from within a custom tool."""
...
diff --git a/python/composio/core/models/session_context.py b/python/composio/core/models/session_context.py
index 5ab47e63a..b0c0dcd3e 100644
--- a/python/composio/core/models/session_context.py
+++ b/python/composio/core/models/session_context.py
@@ -10,6 +10,9 @@ import typing as t
from composio_client import omit
from composio_client.types.tool_router.session_proxy_execute_params import Parameter
+from composio_client.types.tool_router.session_execute_response import (
+ SessionExecuteResponse,
+)
from composio_client.types.tool_router.session_proxy_execute_response import (
SessionProxyExecuteResponse,
)
@@ -20,7 +23,7 @@ from composio.core.models.custom_tool_execution import (
find_custom_tool,
)
from composio.core.models.custom_tool_types import CustomToolsMap
-from composio.core.models.tools import ToolExecutionResponse, _serialize_arguments
+from composio.core.models.tools import _serialize_arguments
from composio.exceptions import ValidationError
@@ -110,7 +113,7 @@ class SessionContextImpl:
self,
tool_slug: str,
arguments: t.Dict[str, t.Any],
- ) -> ToolExecutionResponse:
+ ) -> SessionExecuteResponse:
"""Execute any tool from within a custom tool.
Routes to sibling local tools in-process when available,
@@ -119,22 +122,22 @@ class SessionContextImpl:
# Try local tool first (sibling routing)
entry = find_custom_tool(self._custom_tools_map, tool_slug)
if entry:
- return execute_custom_tool(entry, arguments, self)
+ result = execute_custom_tool(entry, arguments, self)
+ return SessionExecuteResponse(
+ data=result["data"],
+ error=result["error"],
+ log_id="",
+ )
# Serialize any Pydantic model instances before sending to remote API
serialized = _serialize_arguments(arguments)
# Fall back to remote execution
- response = self._client.tool_router.session.execute(
+ return self._client.tool_router.session.execute(
session_id=self._session_id,
tool_slug=tool_slug,
arguments=serialized,
)
- return {
- "data": response.data if hasattr(response, "data") else {},
- "error": response.error if hasattr(response, "error") else None,
- "successful": not (hasattr(response, "error") and response.error),
- }
def proxy_execute(
self,
diff --git a/python/tests/test_custom_tools.py b/python/tests/test_custom_tools.py
index 13135c6c8..ec14615fe 100644
--- a/python/tests/test_custom_tools.py
+++ b/python/tests/test_custom_tools.py
@@ -478,7 +478,8 @@ class TestSessionContextImpl:
client=MagicMock(), user_id="u", session_id="s", custom_tools_map=m
)
result = ctx.execute("GREP", {"pattern": "test"})
- assert result["successful"] is True
+ assert result.error is None
+ assert result.data["matches"] == ["test"]
def test_remote_fallback(self, grep_tool):
m = build_custom_tools_map([grep_tool])