@devries-cameron20 / composio Mirror · read-only / commits / 9dbda5844
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.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 22, 02:00 UTC

fix(python): resolve mypy type errors in custom tools

- Fix variable shadowing: rename ct -> ctk_tool in toolkit loop (ExperimentalCustomToolkitTool vs ExperimentalCustomTool) - Use composio_client Parameter TypedDict for proxy_execute params - Use Literal type variables for apply_modifier_by_type overload resolution (matches existing pattern in tools.py) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Dhawal Upadhyay committed Mar 20, 2026 at 14:02 UTC 9dbda5844670dabf2e6741c2821ff654a95ad487
3 files changed +25 -41
python/composio/core/models/custom_tool.py
+5 -11
@@ -320,9 +320,7 @@ def experimental_create_toolkit(
320 f"they inherit the toolkit identity instead."
321 )
322 # Early length validation with toolkit slug
323 - _validate_slug_length(
324 - tool.slug, slug, f'{context}("{slug}")'
325 - )
323 + _validate_slug_length(tool.slug, slug, f'{context}("{slug}")')
324
325 return CustomToolkit(
326 slug=slug,
@@ -442,9 +440,7 @@ def build_custom_tools_map(
440 if toolkits:
441 for tk in toolkits:
442 for handle in tk.tools:
445 - add_entry(
446 - handle, _build_final_slug(handle.slug, tk.slug), tk.slug
447 - )
443 + add_entry(handle, _build_final_slug(handle.slug, tk.slug), tk.slug)
444
445 return CustomToolsMap(
446 by_final_slug=by_final_slug,
@@ -467,9 +463,7 @@ def build_custom_tools_map_from_response(
463
464 # Build lookup from original slug → handle + toolkit
465 # Detect duplicate original slugs across standalone tools and toolkit tools
470 - handles_by_original: t.Dict[
471 - str, t.Tuple[CustomTool, t.Optional[str]]
472 - ] = {}
466 + handles_by_original: t.Dict[str, t.Tuple[CustomTool, t.Optional[str]]] = {}
467 for handle in tools:
468 key = handle.slug.upper()
469 if key in handles_by_original:
@@ -512,8 +506,8 @@ def build_custom_tools_map_from_response(
506 # Map toolkit custom tools from response
507 if experimental and experimental.custom_toolkits:
508 for ctk in experimental.custom_toolkits:
515 - for ct in ctk.tools:
516 - add_entry(ct.slug, ct.original_slug, ctk.slug)
509 + for ctk_tool in ctk.tools:
510 + add_entry(ctk_tool.slug, ctk_tool.original_slug, ctk.slug)
511
512 return CustomToolsMap(
513 by_final_slug=by_final_slug,
python/composio/core/models/session_context.py
+9 -7
@@ -33,16 +33,18 @@ def proxy_execute_impl(
33 parameters: t.Optional[t.List[t.Dict[str, t.Any]]] = None,
34 ) -> ProxyExecuteResponse:
35 """Shared proxy execute implementation used by SessionContextImpl and ToolRouterSession."""
36 - # Transform parameters to API format
37 - api_params: t.List[t.Dict[str, t.Any]] = []
36 + # Transform parameters to API format using the composio_client Parameter type
37 + from composio_client.types.tool_router.session_proxy_execute_params import Parameter
38 +
39 + api_params: t.List[Parameter] = []
40 if parameters:
41 for p in parameters:
42 api_params.append(
41 - {
42 - "name": p["name"],
43 - "type": p.get("in", p.get("type", "header")),
44 - "value": str(p["value"]),
45 - }
43 + Parameter(
44 + name=p["name"],
45 + type=p.get("in", p.get("type", "header")), # type: ignore[typeddict-item]
46 + value=str(p["value"]),
47 + )
48 )
49
50 response = client.tool_router.session.proxy_execute(
python/composio/core/models/tool_router_session.py
+11 -23
@@ -148,9 +148,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
148
149 return t.cast(
150 TToolCollection,
151 - t.cast(
152 - AgenticProvider[TTool, TToolCollection], self._provider
153 - ).wrap_tools(
151 + t.cast(AgenticProvider[TTool, TToolCollection], self._provider).wrap_tools(
152 tools=router_tools,
153 execute_tool=execute_fn,
154 ),
@@ -175,12 +173,13 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
173 from composio.core.models._modifiers import apply_modifier_by_type
174 from composio.core.models.tools import ToolExecuteParams
175
176 + type_before: t.Literal["before_execute"] = "before_execute"
177 params: ToolExecuteParams = {"arguments": arguments}
178 modified = apply_modifier_by_type(
179 modifiers=modifiers,
180 toolkit="composio",
181 tool=slug,
183 - type="before_execute",
182 + type=type_before,
183 request=params,
184 )
185 processed_arguments = modified.get("arguments", arguments)
@@ -191,11 +190,12 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
190
191 # Apply after_execute modifiers
192 if modifiers is not None:
193 + type_after: t.Literal["after_execute"] = "after_execute"
194 result = apply_modifier_by_type(
195 modifiers=modifiers,
196 toolkit="composio",
197 tool=slug,
198 - type="after_execute",
198 + type=type_after,
199 response=result,
200 )
201
@@ -208,9 +208,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
208
209 return routing_execute
210
211 - def _parse_tool_item(
212 - self, item: t.Any
213 - ) -> t.Dict[str, t.Any]:
211 + def _parse_tool_item(self, item: t.Any) -> t.Dict[str, t.Any]:
212 """Parse an individual tool item from COMPOSIO_MULTI_EXECUTE_TOOL's tools array."""
213 if not isinstance(item, dict):
214 return {"tool_slug": "", "arguments": {}}
@@ -308,9 +306,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
306 # Merge results into the backend's results[] format
307 remote_data = (remote_result or {}).get("data", {})
308 remote_results_list: t.List[t.Any] = (
311 - remote_data.get("results", [])
312 - if isinstance(remote_data, dict)
313 - else []
309 + remote_data.get("results", []) if isinstance(remote_data, dict) else []
310 )
311
312 # Build local result entries matching backend format
@@ -358,9 +354,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
354 merged_data["results"] = merged
355
356 # Build error message — include remote batch error if present
361 - remote_batch_error = (
362 - remote_result.get("error") if remote_result else None
363 - )
357 + remote_batch_error = remote_result.get("error") if remote_result else None
358 if has_any_error:
359 parts = []
360 if failed_count > 0:
@@ -517,9 +511,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
511 # Check if this is a local tool (by original or final slug)
512 entry = find_custom_tool(self._custom_tools_map, tool_slug)
513 if entry and self._session_context:
520 - result = execute_custom_tool(
521 - entry, arguments or {}, self._session_context
522 - )
514 + result = execute_custom_tool(entry, arguments or {}, self._session_context)
515 # Normalize to match SessionExecuteResponse shape (data, error, log_id)
516 return {
517 "data": result["data"],
@@ -551,9 +543,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
543 entries = list(self._custom_tools_map.by_final_slug.values())
544 if toolkit:
545 entries = [
554 - e
555 - for e in entries
556 - if e.toolkit and e.toolkit.lower() == toolkit.lower()
546 + e for e in entries if e.toolkit and e.toolkit.lower() == toolkit.lower()
547 ]
548
549 return [
@@ -580,9 +570,7 @@ class ToolRouterSession(t.Generic[TTool, TToolCollection]):
570 for tk in self._custom_tools_map.toolkits:
571 tools = []
572 for tool in tk.tools:
583 - entry = self._custom_tools_map.by_original_slug.get(
584 - tool.slug.upper()
585 - )
573 + entry = self._custom_tools_map.by_original_slug.get(tool.slug.upper())
574 tools.append(
575 RegisteredCustomTool(
576 slug=entry.final_slug if entry else tool.slug,