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): handle 4+ option and typeless anyOf/oneOf unions in tool signatures (#3708)
Anas Khanb0ab8eacff0b
diff --git a/python/composio/utils/shared.py b/python/composio/utils/shared.pyindex 438aa40d1..9fc194383 100644--- a/python/composio/utils/shared.py+++ b/python/composio/utils/shared.py@@ -9,6 +9,7 @@ import jsonimport keywordimport typing as timport uuid+from functools import reducefrom inspect import Parameterfrom pydantic import BaseModel, Field, create_model@@ -580,21 +581,16 @@ def get_signature_format_from_schema_params(param_type = param_allOf[0].get("type", None)if param_oneOf is not None or param_anyOf is not None:param_types = [ptype.get("type") for ptype in (param_oneOf or param_anyOf)]- if len(param_types) == 1:- annotation = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_types[0]]- elif len(param_types) == 2:- # Check as redefinition and union was incompatible- # @karan to check if this is the right way to do it- t1: t.Type = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_types[0]] # type: ignore- t2: t.Type = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_types[1]] # type: ignore- annotation: t.Type = t.Union[t1, t2] # type: ignore- elif len(param_types) == 3:- t1: t.Type = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_types[0]] # type: ignore- t2: t.Type = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_types[1]] # type: ignore- t3: t.Type = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_types[2]] # type: ignore- annotation: t.Type = t.Union[t1, t2, t3] # type: ignore+ # Map each option to a Python type, falling back to t.Any for options+ # that are missing a "type" key or use an unrecognized type, then build+ # a Union for any count of members (no 1/2/3-member cap).+ mapped_types: t.List[t.Any] = [+ PYDANTIC_TYPE_TO_PYTHON_TYPE.get(ptype, t.Any) for ptype in param_types+ ]+ if len(mapped_types) == 1:+ annotation = mapped_types[0]else:- raise ValueError("Invalid 'oneOf' schema")+ annotation = reduce(lambda a, b: t.Union[a, b], mapped_types)param_default = param_schema.get("default", "")elif param_type in PYDANTIC_TYPE_TO_PYTHON_TYPE:annotation = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_type]diff --git a/python/tests/test_schema_parser.py b/python/tests/test_schema_parser.pyindex 0b31ec2c2..f751d0d6f 100644--- a/python/tests/test_schema_parser.py+++ b/python/tests/test_schema_parser.py@@ -12,6 +12,7 @@ from pydantic import BaseModelfrom pydantic.fields import PydanticUndefinedfrom composio.utils.shared import (+ get_signature_format_from_schema_params,json_schema_to_fields_dict,json_schema_to_model,json_schema_to_pydantic_field,@@ -1462,5 +1463,145 @@ class TestBooleanDefaultCoercion:)+class TestGetSignatureFormatFromSchemaParams:+ """Test cases for get_signature_format_from_schema_params union handling."""++ @staticmethod+ def _annotation(schema):+ params = get_signature_format_from_schema_params(schema)+ assert len(params) == 1+ return params[0].annotation++ @pytest.mark.unit+ @pytest.mark.schema+ def test_oneof_four_members(self):+ """oneOf with 4 options builds a Union instead of raising ValueError."""+ schema = {+ "properties": {+ "value": {+ "oneOf": [+ {"type": "string"},+ {"type": "integer"},+ {"type": "boolean"},+ {"type": "number"},+ ]+ }+ }+ }++ annotation = self._annotation(schema)+ assert t.get_origin(annotation) is t.Union+ assert set(t.get_args(annotation)) == {str, int, bool, float}++ @pytest.mark.unit+ @pytest.mark.schema+ def test_oneof_five_members(self):+ """oneOf with more than four options is also supported (no 3-member cap)."""+ schema = {+ "properties": {+ "value": {+ "oneOf": [+ {"type": "string"},+ {"type": "integer"},+ {"type": "boolean"},+ {"type": "number"},+ {"type": "array"},+ ]+ }+ }+ }++ annotation = self._annotation(schema)+ assert t.get_origin(annotation) is t.Union+ assert len(t.get_args(annotation)) == 5++ @pytest.mark.unit+ @pytest.mark.schema+ def test_anyof_option_missing_type(self):+ """An anyOf option without a 'type' key maps to Any instead of raising KeyError."""+ schema = {+ "properties": {+ "value": {+ "anyOf": [+ {"description": "free-form value"},+ {"type": "string"},+ ]+ }+ }+ }++ annotation = self._annotation(schema)+ assert t.get_origin(annotation) is t.Union+ args = t.get_args(annotation)+ assert t.Any in args+ assert str in args++ @pytest.mark.unit+ @pytest.mark.schema+ def test_anyof_all_options_missing_type(self):+ """anyOf where every option lacks a 'type' collapses to a single Any annotation."""+ schema = {+ "properties": {+ "value": {+ "anyOf": [+ {"description": "a"},+ {"description": "b"},+ ]+ }+ }+ }++ assert self._annotation(schema) is t.Any++ @pytest.mark.unit+ @pytest.mark.schema+ def test_oneof_single_member(self):+ """oneOf with a single option resolves to that type directly (unchanged)."""+ schema = {"properties": {"value": {"oneOf": [{"type": "string"}]}}}+ assert self._annotation(schema) is str++ @pytest.mark.unit+ @pytest.mark.schema+ def test_oneof_two_members(self):+ """Two-member oneOf behavior is preserved (Union of the two types)."""+ schema = {+ "properties": {+ "value": {"oneOf": [{"type": "string"}, {"type": "integer"}]}+ }+ }+ assert self._annotation(schema) == t.Union[str, int]++ @pytest.mark.unit+ @pytest.mark.schema+ def test_oneof_three_members(self):+ """Three-member oneOf behavior is preserved (Union of the three types)."""+ schema = {+ "properties": {+ "value": {+ "oneOf": [+ {"type": "string"},+ {"type": "integer"},+ {"type": "boolean"},+ ]+ }+ }+ }+ assert self._annotation(schema) == t.Union[str, int, bool]++ @pytest.mark.unit+ @pytest.mark.schema+ def test_anyof_with_null_member_still_resolves(self):+ """Nullable anyOf [type, null] continues to resolve without raising."""+ schema = {+ "properties": {"value": {"anyOf": [{"type": "string"}, {"type": "null"}]}}+ }++ annotation = self._annotation(schema)+ assert t.get_origin(annotation) is t.Union+ args = t.get_args(annotation)+ assert str in args+ assert type(None) in args++if __name__ == "__main__":pytest.main([__file__, "-v"])