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: handle python reserved keywords when building a langchain tool
angrybayblade committed
Jul 24, 2025 at 16:38 UTC
edc4c4cf953da2d4f4ef7495b46aa2ca973a2f28
1 file changed
+70
-22
python/providers/langchain/composio_langchain/provider.py
+70
-22
@@ -15,6 +15,55 @@ from composio.utils.shared import (
15
json_schema_to_model,
16
)
17
18
+_python_reserved = {"for", "async"}
19
+_obj_marker = "-_object_-"
20
+
21
+
22
+def _clean_reserved_keyword(keyword: str):
23
+ return f"{keyword}_rs"
24
+
25
+
26
+def _substitute_reserved_python_keywords(schema: t.Dict) -> t.Tuple[dict, dict]:
27
+ if "properties" not in schema:
28
+ return schema, {}
29
+
30
+ keywords = {}
31
+ for p_name in list(schema["properties"]):
32
+ if p_name not in _python_reserved:
33
+ continue
34
+
35
+ _keywords = {}
36
+ p_val = schema["properties"].pop(p_name)
37
+ if p_val.get("type") == "object":
38
+ p_val, _keywords = _substitute_reserved_python_keywords(schema=p_val)
39
+
40
+ p_name_clean = _clean_reserved_keyword(keyword=p_name)
41
+ schema["properties"][p_name_clean] = p_val
42
+ keywords[p_name_clean] = p_name
43
+ keywords[f"{p_name_clean}{_obj_marker}"] = _keywords
44
+
45
+ return schema, keywords
46
+
47
+
48
+def _reinstate_reserved_python_keywords(request: dict, keywords: dict) -> dict:
49
+ for clean_key in sorted(list(keywords), reverse=True):
50
+ subkeys = None
51
+ if clean_key.endswith(_obj_marker):
52
+ subkeys = keywords[clean_key]
53
+ clean_key, _ = clean_key.split(_obj_marker, maxsplit=1)
54
+
55
+ if clean_key not in request:
56
+ continue
57
+
58
+ orginal_value = request.pop(clean_key)
59
+ if subkeys is not None:
60
+ orginal_value = _reinstate_reserved_python_keywords(
61
+ request=orginal_value,
62
+ keywords=subkeys,
63
+ )
64
+ request[keywords[clean_key]] = orginal_value
65
+ return request
66
+
67
68
class StructuredTool(BaseStructuredTool):
69
def run(self, *args, **kwargs):
@@ -34,21 +83,30 @@ class LangchainProvider(
83
84
runtime = "langchain"
85
37
- def _wrap_action(
38
- self,
39
- tool: str,
40
- description: str,
41
- schema_params: t.Dict,
42
- execute_tool: AgenticProviderExecuteFn,
43
- ):
86
+ def wrap_tool(
87
+ self, tool: Tool, execute_tool: AgenticProviderExecuteFn
88
+ ) -> StructuredTool:
89
+ """Wraps composio tool as Langchain StructuredTool object."""
90
+ # Replace reserved python keywords
91
+ schema_params, keywords = _substitute_reserved_python_keywords(
92
+ schema=tool.input_parameters
93
+ )
94
+
95
def function(**kwargs: t.Any) -> t.Dict:
96
"""Wrapper function for composio action."""
46
- return execute_tool(tool, kwargs)
97
+ print(kwargs)
98
+ kwargs = _reinstate_reserved_python_keywords(
99
+ request=kwargs,
100
+ keywords=keywords,
101
+ )
102
+ print(kwargs)
103
+ exit()
104
+ return execute_tool(tool.slug, kwargs)
105
106
action_func = types.FunctionType(
107
function.__code__,
108
globals=globals(),
51
- name=tool,
109
+ name=tool.slug,
110
closure=function.__closure__,
111
)
112
action_func.__signature__ = Signature( # type: ignore
@@ -56,29 +114,19 @@ class LangchainProvider(
114
schema_params=schema_params
115
)
116
)
59
- action_func.__doc__ = description
60
- return action_func
117
+ action_func.__doc__ = tool.description
118
62
- def wrap_tool(
63
- self, tool: Tool, execute_tool: AgenticProviderExecuteFn
64
- ) -> StructuredTool:
65
- """Wraps composio tool as Langchain StructuredTool object."""
119
return t.cast(
120
StructuredTool,
121
StructuredTool.from_function(
122
name=tool.slug,
123
description=tool.description,
124
args_schema=json_schema_to_model(
72
- json_schema=tool.input_parameters,
125
+ json_schema=schema_params,
126
skip_default=self.skip_default,
127
),
128
return_schema=True,
76
- func=self._wrap_action(
77
- tool=tool.slug,
78
- description=tool.description,
79
- schema_params=tool.input_parameters,
80
- execute_tool=execute_tool,
81
- ),
129
+ func=action_func,
130
handle_tool_error=True,
131
handle_validation_error=True,
132
),