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): resolve mypy type errors in custom tools

Dhawal Upadhyay9dbda5844670
diff --git a/python/composio/core/models/custom_tool.py b/python/composio/core/models/custom_tool.py
index cfcb586fe..318d89057 100644
--- a/python/composio/core/models/custom_tool.py
+++ b/python/composio/core/models/custom_tool.py
@@ -320,9 +320,7 @@ def experimental_create_toolkit(
f"they inherit the toolkit identity instead."
)
# Early length validation with toolkit slug
- _validate_slug_length(
- tool.slug, slug, f'{context}("{slug}")'
- )
+ _validate_slug_length(tool.slug, slug, f'{context}("{slug}")')
return CustomToolkit(
slug=slug,
@@ -442,9 +440,7 @@ def build_custom_tools_map(
if toolkits:
for tk in toolkits:
for handle in tk.tools:
- add_entry(
- handle, _build_final_slug(handle.slug, tk.slug), tk.slug
- )
+ add_entry(handle, _build_final_slug(handle.slug, tk.slug), tk.slug)
return CustomToolsMap(
by_final_slug=by_final_slug,
@@ -467,9 +463,7 @@ def build_custom_tools_map_from_response(
# Build lookup from original slug → handle + toolkit
# Detect duplicate original slugs across standalone tools and toolkit tools
- handles_by_original: t.Dict[
- str, t.Tuple[CustomTool, t.Optional[str]]
- ] = {}
+ handles_by_original: t.Dict[str, t.Tuple[CustomTool, t.Optional[str]]] = {}
for handle in tools:
key = handle.slug.upper()
if key in handles_by_original:
@@ -512,8 +506,8 @@ def build_custom_tools_map_from_response(
# Map toolkit custom tools from response
if experimental and experimental.custom_toolkits:
for ctk in experimental.custom_toolkits:
- for ct in ctk.tools:
- add_entry(ct.slug, ct.original_slug, ctk.slug)
+ for ctk_tool in ctk.tools:
+ add_entry(ctk_tool.slug, ctk_tool.original_slug, ctk.slug)
return CustomToolsMap(
by_final_slug=by_final_slug,
diff --git a/python/composio/core/models/session_context.py b/python/composio/core/models/session_context.py
index 329e4863e..7abfbf466 100644
--- a/python/composio/core/models/session_context.py
+++ b/python/composio/core/models/session_context.py
@@ -33,16 +33,18 @@ def proxy_execute_impl(
parameters: t.Optional[t.List[t.Dict[str, t.Any]]] = None,
) -> ProxyExecuteResponse:
"""Shared proxy execute implementation used by SessionContextImpl and ToolRouterSession."""
- # Transform parameters to API format
- api_params: t.List[t.Dict[str, t.Any]] = []
+ # Transform parameters to API format using the composio_client Parameter type
+ from composio_client.types.tool_router.session_proxy_execute_params import Parameter
+
+ api_params: t.List[Parameter] = []
if parameters:
for p in parameters:
api_params.append(
- {
- "name": p["name"],
- "type": p.get("in", p.get("type", "header")),
- "value": str(p["value"]),
- }
+ Parameter(
+ name=p["name"],
+ type=p.get("in", p.get("type", "header")), # type: ignore[typeddict-item]
+ value=str(p["value"]),
+ )
)
response = client.tool_router.session.proxy_execute(
diff --git a/python/composio/core/models/tool_router_session.py b/python/composio/core/models/tool_router_session.py
index 4845331e8..708be12c9 100644
--- a/python/composio/core/models/tool_router_session.py
+++ b/python/composio/core/models/tool_router_session.py
@@ -148,9 +148,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
return t.cast(
TToolCollection,
- t.cast(
- AgenticProvider[TTool, TToolCollection], self._provider
- ).wrap_tools(
+ t.cast(AgenticProvider[TTool, TToolCollection], self._provider).wrap_tools(
tools=router_tools,
execute_tool=execute_fn,
),
@@ -175,12 +173,13 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
from composio.core.models._modifiers import apply_modifier_by_type
from composio.core.models.tools import ToolExecuteParams
+ type_before: t.Literal["before_execute"] = "before_execute"
params: ToolExecuteParams = {"arguments": arguments}
modified = apply_modifier_by_type(
modifiers=modifiers,
toolkit="composio",
tool=slug,
- type="before_execute",
+ type=type_before,
request=params,
)
processed_arguments = modified.get("arguments", arguments)
@@ -191,11 +190,12 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
# Apply after_execute modifiers
if modifiers is not None:
+ type_after: t.Literal["after_execute"] = "after_execute"
result = apply_modifier_by_type(
modifiers=modifiers,
toolkit="composio",
tool=slug,
- type="after_execute",
+ type=type_after,
response=result,
)
@@ -208,9 +208,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
return routing_execute
- def _parse_tool_item(
- self, item: t.Any
- ) -> t.Dict[str, t.Any]:
+ def _parse_tool_item(self, item: t.Any) -> t.Dict[str, t.Any]:
"""Parse an individual tool item from COMPOSIO_MULTI_EXECUTE_TOOL's tools array."""
if not isinstance(item, dict):
return {"tool_slug": "", "arguments": {}}
@@ -308,9 +306,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
# Merge results into the backend's results[] format
remote_data = (remote_result or {}).get("data", {})
remote_results_list: t.List[t.Any] = (
- remote_data.get("results", [])
- if isinstance(remote_data, dict)
- else []
+ remote_data.get("results", []) if isinstance(remote_data, dict) else []
)
# Build local result entries matching backend format
@@ -358,9 +354,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
merged_data["results"] = merged
# Build error message — include remote batch error if present
- remote_batch_error = (
- remote_result.get("error") if remote_result else None
- )
+ remote_batch_error = remote_result.get("error") if remote_result else None
if has_any_error:
parts = []
if failed_count > 0:
@@ -517,9 +511,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
# Check if this is a local tool (by original or final slug)
entry = find_custom_tool(self._custom_tools_map, tool_slug)
if entry and self._session_context:
- result = execute_custom_tool(
- entry, arguments or {}, self._session_context
- )
+ result = execute_custom_tool(entry, arguments or {}, self._session_context)
# Normalize to match SessionExecuteResponse shape (data, error, log_id)
return {
"data": result["data"],
@@ -551,9 +543,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
entries = list(self._custom_tools_map.by_final_slug.values())
if toolkit:
entries = [
- e
- for e in entries
- if e.toolkit and e.toolkit.lower() == toolkit.lower()
+ e for e in entries if e.toolkit and e.toolkit.lower() == toolkit.lower()
]
return [
@@ -580,9 +570,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
for tk in self._custom_tools_map.toolkits:
tools = []
for tool in tk.tools:
- entry = self._custom_tools_map.by_original_slug.get(
- tool.slug.upper()
- )
+ entry = self._custom_tools_map.by_original_slug.get(tool.slug.upper())
tools.append(
RegisteredCustomTool(
slug=entry.final_slug if entry else tool.slug,