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 21, 16:00 UTC
fix(py): preserve serialized provider arguments
Co-authored-by: Aditya Chawla <aditya17.mitmpl2023@learner.manipal.edu>
jkomyno committed
Aug 7, 2026 at 18:15 UTC
5bb1e20518c69f14a864bf680fba7e8a48b7de56
5 files changed
+266
-2
python/composio/utils/shared.py
+40
-1
@@ -47,6 +47,7 @@ __all__ = [
47
"substitute_reserved_python_keywords",
48
"reinstate_reserved_python_keywords",
49
"normalize_tool_arguments",
50
+ "validate_and_serialize_tool_arguments",
51
]
52
53
reserved_names = ["validate"]
@@ -54,6 +55,7 @@ reserved_names = ["validate"]
55
_OBJ_MARKER = "-_object_-"
56
_ARR_MARKER = "-_array_-"
57
_MAX_PROVIDER_ALIAS_LENGTH = 64
58
+_EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE = "__composio_explicit_default_fields__"
59
60
61
def normalize_tool_arguments(arguments: t.Any) -> t.Dict[str, t.Any]:
@@ -102,6 +104,34 @@ def _as_dict(value: t.Any) -> t.Dict[str, t.Any]:
104
)
105
106
107
+def validate_and_serialize_tool_arguments(
108
+ args_schema: t.Type[BaseModel],
109
+ arguments: t.Dict[str, t.Any],
110
+) -> t.Dict[str, t.Any]:
111
+ """Validate provider arguments and serialize only values the backend should see.
112
+
113
+ Pydantic models use ``None`` for optional fields that were not supplied, so a
114
+ plain ``model_dump()`` changes omission into an explicit null. Conversely,
115
+ ``exclude_unset=True`` also drops defaults declared by the source JSON Schema.
116
+ Track those defaults on the generated model and combine them with
117
+ ``model_fields_set`` so aliases, explicit nulls, defaults, and dynamic extras
118
+ all survive without padding the payload.
119
+ """
120
+ validated = args_schema.model_validate(arguments)
121
+ included_fields = set(validated.model_fields_set)
122
+ included_fields.update(
123
+ getattr(type(validated), _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE, ())
124
+ )
125
+ return t.cast(
126
+ t.Dict[str, t.Any],
127
+ validated.model_dump(
128
+ mode="python",
129
+ by_alias=True,
130
+ include=included_fields,
131
+ ),
132
+ )
133
+
134
+
135
def _make_safe_name(name: str) -> str:
136
"""Append ``_rs`` to a Python keyword so it can be used as a parameter name."""
137
return f"{name}_rs"
@@ -450,6 +480,7 @@ def json_schema_to_model(
480
if model_name is None:
481
model_name = "GeneratedModel"
482
field_definitions = {}
483
+ explicit_default_fields: t.Set[str] = set()
484
for name, prop in json_schema.get("properties", {}).items():
485
updated_name, pydantic_type, pydantic_field = json_schema_to_pydantic_field(
486
name,
@@ -458,11 +489,19 @@ def json_schema_to_model(
489
skip_default=skip_default,
490
)
491
field_definitions[updated_name] = (pydantic_type, pydantic_field)
492
+ if not skip_default and "default" in prop:
493
+ explicit_default_fields.add(updated_name)
494
# The dynamic-key policy is shared with `json_schema_to_pydantic_type` so the
495
# two entry points cannot disagree about which arguments survive conversion.
496
+ base_model = create_model(model_name, **field_definitions) # type: ignore
497
+ setattr(
498
+ base_model,
499
+ _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE,
500
+ frozenset(explicit_default_fields),
501
+ )
502
return apply_object_policy(
503
json_schema,
465
- create_model(model_name, **field_definitions), # type: ignore
504
+ base_model,
505
model_name=model_name,
506
)
507
python/providers/crewai/composio_crewai/providers.py
+23
-1
@@ -6,7 +6,11 @@ from crewai.tools import BaseTool
6
from composio.core.provider import AgenticProvider, AgenticProviderExecuteFn
7
from composio.types import Tool
8
from composio.utils.pydantic import parse_pydantic_error
9
-from composio.utils.shared import json_schema_to_model, normalize_tool_arguments
9
+from composio.utils.shared import (
10
+ json_schema_to_model,
11
+ normalize_tool_arguments,
12
+ validate_and_serialize_tool_arguments,
13
+)
14
15
16
class CrewAIProvider(AgenticProvider[BaseTool, list[BaseTool]], name="crewai"):
@@ -22,6 +26,24 @@ class CrewAIProvider(AgenticProvider[BaseTool, list[BaseTool]], name="crewai"):
26
"""Wrap a tool as a CrewAI tool."""
27
28
class Wrapper(BaseTool):
29
+ def _validate_kwargs(
30
+ self, kwargs: t.Dict[str, t.Any]
31
+ ) -> t.Dict[str, t.Any]:
32
+ """Validate zero-field schemas and preserve argument presence."""
33
+ if self.args_schema is None:
34
+ return kwargs
35
+ return validate_and_serialize_tool_arguments(self.args_schema, kwargs)
36
+
37
+ def run(self, *args, **kwargs):
38
+ try:
39
+ return super().run(*args, **kwargs)
40
+ except pydantic.ValidationError as e:
41
+ return {
42
+ "successful": False,
43
+ "error": parse_pydantic_error(e),
44
+ "data": None,
45
+ }
46
+
47
def _run(self, **kwargs):
48
try:
49
# Normalize defensively so a stringified payload is coerced to a dict (issue #2406).
python/providers/langchain/composio_langchain/provider.py
+17
@@ -16,10 +16,27 @@ from composio.utils.shared import (
16
normalize_tool_arguments,
17
reinstate_reserved_python_keywords,
18
substitute_reserved_python_keywords,
19
+ validate_and_serialize_tool_arguments,
20
)
21
22
23
class StructuredTool(BaseStructuredTool): # type: ignore[misc]
24
+ def _to_args_and_kwargs(
25
+ self,
26
+ tool_input: t.Union[str, t.Dict[str, t.Any]],
27
+ tool_call_id: t.Optional[str],
28
+ ) -> t.Tuple[t.Tuple[str, ...], t.Dict[str, t.Any]]:
29
+ """Validate dynamic-only schemas before LangChain's no-field shortcut."""
30
+ args_schema = self.args_schema
31
+ if (
32
+ isinstance(tool_input, dict)
33
+ and isinstance(args_schema, type)
34
+ and issubclass(args_schema, pydantic.BaseModel)
35
+ ):
36
+ arguments = validate_and_serialize_tool_arguments(args_schema, tool_input)
37
+ return (), arguments
38
+ return super()._to_args_and_kwargs(tool_input, tool_call_id)
39
+
40
def run(self, *args, **kwargs):
41
try:
42
return super().run(*args, **kwargs)
python/providers/langgraph/composio_langgraph/provider.py
+17
@@ -16,10 +16,27 @@ from composio.utils.shared import (
16
normalize_tool_arguments,
17
reinstate_reserved_python_keywords,
18
substitute_reserved_python_keywords,
19
+ validate_and_serialize_tool_arguments,
20
)
21
22
23
class StructuredTool(BaseStructuredTool): # type: ignore[misc]
24
+ def _to_args_and_kwargs(
25
+ self,
26
+ tool_input: t.Union[str, t.Dict[str, t.Any]],
27
+ tool_call_id: t.Optional[str],
28
+ ) -> t.Tuple[t.Tuple[str, ...], t.Dict[str, t.Any]]:
29
+ """Validate dynamic-only schemas before LangChain's no-field shortcut."""
30
+ args_schema = self.args_schema
31
+ if (
32
+ isinstance(tool_input, dict)
33
+ and isinstance(args_schema, type)
34
+ and issubclass(args_schema, pydantic.BaseModel)
35
+ ):
36
+ arguments = validate_and_serialize_tool_arguments(args_schema, tool_input)
37
+ return (), arguments
38
+ return super()._to_args_and_kwargs(tool_input, tool_call_id)
39
+
40
def run(self, *args, **kwargs):
41
try:
42
return super().run(*args, **kwargs)
python/tests/test_provider_argument_serialization.py
new
+169
@@ -0,0 +1,169 @@
1
+"""Provider-boundary tests for validated tool argument serialization."""
2
+
3
+import typing as t
4
+
5
+import pytest
6
+
7
+from composio.client.types import Tool, tool_list_response
8
+
9
+
10
+PROVIDERS = ("crewai", "langchain", "langgraph")
11
+
12
+
13
+def _make_tool(input_parameters: t.Dict[str, t.Any]) -> Tool:
14
+ return Tool(
15
+ name="Test tool",
16
+ slug="TEST_TOOL",
17
+ description="Test tool for provider argument serialization",
18
+ input_parameters=input_parameters,
19
+ output_parameters={},
20
+ available_versions=["12012025_00"],
21
+ version="12012025_00",
22
+ scopes=[],
23
+ toolkit=tool_list_response.ItemToolkit(name="Test", slug="test", logo=""),
24
+ deprecated=tool_list_response.ItemDeprecated(
25
+ available_versions=["12012025_00"],
26
+ displayName="Test tool",
27
+ version="12012025_00",
28
+ toolkit=tool_list_response.ItemDeprecatedToolkit(logo=""),
29
+ is_deprecated=False,
30
+ ),
31
+ is_deprecated=False,
32
+ no_auth=True,
33
+ tags=[],
34
+ )
35
+
36
+
37
+def _provider(name: str):
38
+ module = pytest.importorskip(f"composio_{name}")
39
+ class_name = {
40
+ "crewai": "CrewAIProvider",
41
+ "langchain": "LangchainProvider",
42
+ "langgraph": "LanggraphProvider",
43
+ }[name]
44
+ return getattr(module, class_name)()
45
+
46
+
47
+def _wrap(name: str, input_parameters: t.Dict[str, t.Any]):
48
+ received: t.List[t.Dict[str, t.Any]] = []
49
+
50
+ def execute_tool(slug: str, arguments: t.Dict[str, t.Any]):
51
+ assert slug == "TEST_TOOL"
52
+ received.append(arguments)
53
+ return {"successful": True, "data": arguments, "error": None}
54
+
55
+ wrapped = _provider(name).wrap_tool(_make_tool(input_parameters), execute_tool)
56
+ return wrapped, received
57
+
58
+
59
+def _run(name: str, wrapped: t.Any, arguments: t.Dict[str, t.Any]):
60
+ if name == "crewai":
61
+ return wrapped.run(**arguments)
62
+ return wrapped.run(arguments)
63
+
64
+
65
+@pytest.mark.parametrize("name", PROVIDERS)
66
+def test_provider_serialization_preserves_argument_presence_and_aliases(name: str):
67
+ wrapped, received = _wrap(
68
+ name,
69
+ {
70
+ "type": "object",
71
+ "title": "TestArguments",
72
+ "properties": {
73
+ "query": {"type": "string"},
74
+ "limit": {"type": "integer"},
75
+ "note": {
76
+ "anyOf": [{"type": "string"}, {"type": "null"}],
77
+ },
78
+ "page": {"type": "integer", "default": 5},
79
+ "validate": {"type": "string"},
80
+ },
81
+ "required": ["query"],
82
+ },
83
+ )
84
+
85
+ _run(
86
+ name,
87
+ wrapped,
88
+ {"query": "agents", "note": None, "validate": "yes"},
89
+ )
90
+
91
+ assert received == [
92
+ {
93
+ "query": "agents",
94
+ "note": None,
95
+ "page": 5,
96
+ "validate": "yes",
97
+ }
98
+ ]
99
+
100
+
101
+@pytest.mark.parametrize("name", PROVIDERS)
102
+def test_provider_serialization_preserves_free_form_objects(name: str):
103
+ wrapped, received = _wrap(
104
+ name,
105
+ {
106
+ "type": "object",
107
+ "title": "FreeFormArguments",
108
+ "properties": {},
109
+ },
110
+ )
111
+
112
+ _run(name, wrapped, {"custom": {"nested": True}, "items": [1, "two"]})
113
+
114
+ assert received == [{"custom": {"nested": True}, "items": [1, "two"]}]
115
+
116
+
117
+@pytest.mark.parametrize("name", PROVIDERS)
118
+def test_provider_serialization_validates_pattern_only_schemas(name: str):
119
+ wrapped, received = _wrap(
120
+ name,
121
+ {
122
+ "type": "object",
123
+ "title": "PatternArguments",
124
+ "properties": {},
125
+ "patternProperties": {
126
+ "^count_": {"type": "integer", "minimum": 1},
127
+ },
128
+ "additionalProperties": False,
129
+ },
130
+ )
131
+
132
+ _run(name, wrapped, {"count_valid": 2})
133
+ invalid_result = _run(name, wrapped, {"count_invalid": "not-an-integer"})
134
+
135
+ assert received == [{"count_valid": 2}]
136
+ if name == "crewai":
137
+ assert invalid_result["successful"] is False
138
+ assert invalid_result["data"] is None
139
+
140
+
141
+@pytest.mark.parametrize(
142
+ ("arguments", "parameter"),
143
+ [
144
+ ({"query": "agents", "limit": "not-an-integer"}, "limit"),
145
+ ({"limit": 5}, "query"),
146
+ ],
147
+)
148
+def test_crewai_returns_structured_validation_errors(
149
+ arguments: t.Dict[str, t.Any], parameter: str
150
+):
151
+ wrapped, received = _wrap(
152
+ "crewai",
153
+ {
154
+ "type": "object",
155
+ "title": "RequiredArguments",
156
+ "properties": {
157
+ "query": {"type": "string"},
158
+ "limit": {"type": "integer"},
159
+ },
160
+ "required": ["query"],
161
+ },
162
+ )
163
+
164
+ result = _run("crewai", wrapped, arguments)
165
+
166
+ assert result["successful"] is False
167
+ assert result["data"] is None
168
+ assert parameter in result["error"]
169
+ assert received == []