Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(py): preserve serialized provider arguments
jkomyno5bb1e20518c6
diff --git a/python/composio/utils/shared.py b/python/composio/utils/shared.pyindex d43114335..2b3debb0a 100644--- a/python/composio/utils/shared.py+++ b/python/composio/utils/shared.py@@ -47,6 +47,7 @@ __all__ = ["substitute_reserved_python_keywords","reinstate_reserved_python_keywords","normalize_tool_arguments",+ "validate_and_serialize_tool_arguments",]reserved_names = ["validate"]@@ -54,6 +55,7 @@ reserved_names = ["validate"]_OBJ_MARKER = "-_object_-"_ARR_MARKER = "-_array_-"_MAX_PROVIDER_ALIAS_LENGTH = 64+_EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE = "__composio_explicit_default_fields__"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]:)+def validate_and_serialize_tool_arguments(+ args_schema: t.Type[BaseModel],+ arguments: t.Dict[str, t.Any],+) -> t.Dict[str, t.Any]:+ """Validate provider arguments and serialize only values the backend should see.++ Pydantic models use ``None`` for optional fields that were not supplied, so a+ plain ``model_dump()`` changes omission into an explicit null. Conversely,+ ``exclude_unset=True`` also drops defaults declared by the source JSON Schema.+ Track those defaults on the generated model and combine them with+ ``model_fields_set`` so aliases, explicit nulls, defaults, and dynamic extras+ all survive without padding the payload.+ """+ validated = args_schema.model_validate(arguments)+ included_fields = set(validated.model_fields_set)+ included_fields.update(+ getattr(type(validated), _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE, ())+ )+ return t.cast(+ t.Dict[str, t.Any],+ validated.model_dump(+ mode="python",+ by_alias=True,+ include=included_fields,+ ),+ )++def _make_safe_name(name: str) -> str:"""Append ``_rs`` to a Python keyword so it can be used as a parameter name."""return f"{name}_rs"@@ -450,6 +480,7 @@ def json_schema_to_model(if model_name is None:model_name = "GeneratedModel"field_definitions = {}+ explicit_default_fields: t.Set[str] = set()for name, prop in json_schema.get("properties", {}).items():updated_name, pydantic_type, pydantic_field = json_schema_to_pydantic_field(name,@@ -458,11 +489,19 @@ def json_schema_to_model(skip_default=skip_default,)field_definitions[updated_name] = (pydantic_type, pydantic_field)+ if not skip_default and "default" in prop:+ explicit_default_fields.add(updated_name)# The dynamic-key policy is shared with `json_schema_to_pydantic_type` so the# two entry points cannot disagree about which arguments survive conversion.+ base_model = create_model(model_name, **field_definitions) # type: ignore+ setattr(+ base_model,+ _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE,+ frozenset(explicit_default_fields),+ )return apply_object_policy(json_schema,- create_model(model_name, **field_definitions), # type: ignore+ base_model,model_name=model_name,)diff --git a/python/providers/crewai/composio_crewai/providers.py b/python/providers/crewai/composio_crewai/providers.pyindex d963310c2..39c237ab6 100644--- a/python/providers/crewai/composio_crewai/providers.py+++ b/python/providers/crewai/composio_crewai/providers.py@@ -6,7 +6,11 @@ from crewai.tools import BaseToolfrom composio.core.provider import AgenticProvider, AgenticProviderExecuteFnfrom composio.types import Toolfrom composio.utils.pydantic import parse_pydantic_error-from composio.utils.shared import json_schema_to_model, normalize_tool_arguments+from composio.utils.shared import (+ json_schema_to_model,+ normalize_tool_arguments,+ validate_and_serialize_tool_arguments,+)class CrewAIProvider(AgenticProvider[BaseTool, list[BaseTool]], name="crewai"):@@ -22,6 +26,24 @@ class CrewAIProvider(AgenticProvider[BaseTool, list[BaseTool]], name="crewai"):"""Wrap a tool as a CrewAI tool."""class Wrapper(BaseTool):+ def _validate_kwargs(+ self, kwargs: t.Dict[str, t.Any]+ ) -> t.Dict[str, t.Any]:+ """Validate zero-field schemas and preserve argument presence."""+ if self.args_schema is None:+ return kwargs+ return validate_and_serialize_tool_arguments(self.args_schema, kwargs)++ def run(self, *args, **kwargs):+ try:+ return super().run(*args, **kwargs)+ except pydantic.ValidationError as e:+ return {+ "successful": False,+ "error": parse_pydantic_error(e),+ "data": None,+ }+def _run(self, **kwargs):try:# Normalize defensively so a stringified payload is coerced to a dict (issue #2406).diff --git a/python/providers/langchain/composio_langchain/provider.py b/python/providers/langchain/composio_langchain/provider.pyindex 03689594b..ec5781ebf 100644--- a/python/providers/langchain/composio_langchain/provider.py+++ b/python/providers/langchain/composio_langchain/provider.py@@ -16,10 +16,27 @@ from composio.utils.shared import (normalize_tool_arguments,reinstate_reserved_python_keywords,substitute_reserved_python_keywords,+ validate_and_serialize_tool_arguments,)class StructuredTool(BaseStructuredTool): # type: ignore[misc]+ def _to_args_and_kwargs(+ self,+ tool_input: t.Union[str, t.Dict[str, t.Any]],+ tool_call_id: t.Optional[str],+ ) -> t.Tuple[t.Tuple[str, ...], t.Dict[str, t.Any]]:+ """Validate dynamic-only schemas before LangChain's no-field shortcut."""+ args_schema = self.args_schema+ if (+ isinstance(tool_input, dict)+ and isinstance(args_schema, type)+ and issubclass(args_schema, pydantic.BaseModel)+ ):+ arguments = validate_and_serialize_tool_arguments(args_schema, tool_input)+ return (), arguments+ return super()._to_args_and_kwargs(tool_input, tool_call_id)+def run(self, *args, **kwargs):try:return super().run(*args, **kwargs)diff --git a/python/providers/langgraph/composio_langgraph/provider.py b/python/providers/langgraph/composio_langgraph/provider.pyindex 72e573a60..97d96f17b 100644--- a/python/providers/langgraph/composio_langgraph/provider.py+++ b/python/providers/langgraph/composio_langgraph/provider.py@@ -16,10 +16,27 @@ from composio.utils.shared import (normalize_tool_arguments,reinstate_reserved_python_keywords,substitute_reserved_python_keywords,+ validate_and_serialize_tool_arguments,)class StructuredTool(BaseStructuredTool): # type: ignore[misc]+ def _to_args_and_kwargs(+ self,+ tool_input: t.Union[str, t.Dict[str, t.Any]],+ tool_call_id: t.Optional[str],+ ) -> t.Tuple[t.Tuple[str, ...], t.Dict[str, t.Any]]:+ """Validate dynamic-only schemas before LangChain's no-field shortcut."""+ args_schema = self.args_schema+ if (+ isinstance(tool_input, dict)+ and isinstance(args_schema, type)+ and issubclass(args_schema, pydantic.BaseModel)+ ):+ arguments = validate_and_serialize_tool_arguments(args_schema, tool_input)+ return (), arguments+ return super()._to_args_and_kwargs(tool_input, tool_call_id)+def run(self, *args, **kwargs):try:return super().run(*args, **kwargs)diff --git a/python/tests/test_provider_argument_serialization.py b/python/tests/test_provider_argument_serialization.pynew file mode 100644index 000000000..794a7e9f1--- /dev/null+++ b/python/tests/test_provider_argument_serialization.py@@ -0,0 +1,169 @@+"""Provider-boundary tests for validated tool argument serialization."""++import typing as t++import pytest++from composio.client.types import Tool, tool_list_response+++PROVIDERS = ("crewai", "langchain", "langgraph")+++def _make_tool(input_parameters: t.Dict[str, t.Any]) -> Tool:+ return Tool(+ name="Test tool",+ slug="TEST_TOOL",+ description="Test tool for provider argument serialization",+ input_parameters=input_parameters,+ output_parameters={},+ available_versions=["12012025_00"],+ version="12012025_00",+ scopes=[],+ toolkit=tool_list_response.ItemToolkit(name="Test", slug="test", logo=""),+ deprecated=tool_list_response.ItemDeprecated(+ available_versions=["12012025_00"],+ displayName="Test tool",+ version="12012025_00",+ toolkit=tool_list_response.ItemDeprecatedToolkit(logo=""),+ is_deprecated=False,+ ),+ is_deprecated=False,+ no_auth=True,+ tags=[],+ )+++def _provider(name: str):+ module = pytest.importorskip(f"composio_{name}")+ class_name = {+ "crewai": "CrewAIProvider",+ "langchain": "LangchainProvider",+ "langgraph": "LanggraphProvider",+ }[name]+ return getattr(module, class_name)()+++def _wrap(name: str, input_parameters: t.Dict[str, t.Any]):+ received: t.List[t.Dict[str, t.Any]] = []++ def execute_tool(slug: str, arguments: t.Dict[str, t.Any]):+ assert slug == "TEST_TOOL"+ received.append(arguments)+ return {"successful": True, "data": arguments, "error": None}++ wrapped = _provider(name).wrap_tool(_make_tool(input_parameters), execute_tool)+ return wrapped, received+++def _run(name: str, wrapped: t.Any, arguments: t.Dict[str, t.Any]):+ if name == "crewai":+ return wrapped.run(**arguments)+ return wrapped.run(arguments)++[email protected]("name", PROVIDERS)+def test_provider_serialization_preserves_argument_presence_and_aliases(name: str):+ wrapped, received = _wrap(+ name,+ {+ "type": "object",+ "title": "TestArguments",+ "properties": {+ "query": {"type": "string"},+ "limit": {"type": "integer"},+ "note": {+ "anyOf": [{"type": "string"}, {"type": "null"}],+ },+ "page": {"type": "integer", "default": 5},+ "validate": {"type": "string"},+ },+ "required": ["query"],+ },+ )++ _run(+ name,+ wrapped,+ {"query": "agents", "note": None, "validate": "yes"},+ )++ assert received == [+ {+ "query": "agents",+ "note": None,+ "page": 5,+ "validate": "yes",+ }+ ]++[email protected]("name", PROVIDERS)+def test_provider_serialization_preserves_free_form_objects(name: str):+ wrapped, received = _wrap(+ name,+ {+ "type": "object",+ "title": "FreeFormArguments",+ "properties": {},+ },+ )++ _run(name, wrapped, {"custom": {"nested": True}, "items": [1, "two"]})++ assert received == [{"custom": {"nested": True}, "items": [1, "two"]}]++[email protected]("name", PROVIDERS)+def test_provider_serialization_validates_pattern_only_schemas(name: str):+ wrapped, received = _wrap(+ name,+ {+ "type": "object",+ "title": "PatternArguments",+ "properties": {},+ "patternProperties": {+ "^count_": {"type": "integer", "minimum": 1},+ },+ "additionalProperties": False,+ },+ )++ _run(name, wrapped, {"count_valid": 2})+ invalid_result = _run(name, wrapped, {"count_invalid": "not-an-integer"})++ assert received == [{"count_valid": 2}]+ if name == "crewai":+ assert invalid_result["successful"] is False+ assert invalid_result["data"] is None+++ ("arguments", "parameter"),+ [+ ({"query": "agents", "limit": "not-an-integer"}, "limit"),+ ({"limit": 5}, "query"),+ ],+)+def test_crewai_returns_structured_validation_errors(+ arguments: t.Dict[str, t.Any], parameter: str+):+ wrapped, received = _wrap(+ "crewai",+ {+ "type": "object",+ "title": "RequiredArguments",+ "properties": {+ "query": {"type": "string"},+ "limit": {"type": "integer"},+ },+ "required": ["query"],+ },+ )++ result = _run("crewai", wrapped, arguments)++ assert result["successful"] is False+ assert result["data"] is None+ assert parameter in result["error"]+ assert received == []