@devries-cameron20 / composio Mirror · read-only / commits / b0ab8eacf
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 21, 19:45 UTC

fix(py): handle 4+ option and typeless anyOf/oneOf unions in tool signatures (#3708)

## Summary `get_signature_format_from_schema_params` in `python/composio/utils/shared.py` builds the `__signature__` for the tool wrappers used by the langchain, langgraph, llamaindex, and autogen providers. Its `oneOf`/`anyOf` handling hardcoded a ladder for exactly 1, 2, or 3 union members and raised `ValueError("Invalid 'oneOf' schema")` for any union with four or more options. It also indexed `PYDANTIC_TYPE_TO_PYTHON_TYPE[ptype.get("type")]` directly, so any combiner option missing a `type` key produced `None` and raised `KeyError(None)`. Both shapes are valid JSON Schema and appear in real tool input schemas, so a tool with a 4-way union, or a combiner option without an explicit `type`, makes those four providers raise at tool-wrap time. This replaces the ladder with a single map + `functools.reduce` path that mirrors the existing `_build_union_from_options` in `schema_converter.py`, keeping the 1/2/3-member and nullable `[type, null]` outputs identical to before. Fixes # ## Changes - `python/composio/utils/shared.py`: in `get_signature_format_from_schema_params`, replace the 1/2/3-member `oneOf`/`anyOf` ladder (which raised `ValueError` for 4+ members) with a map-each-option-to-a-Python-type + `functools.reduce` Union build that supports any member count. Unknown/missing option types map to `typing.Any` instead of raising `KeyError`. Adds `from functools import reduce` and removes three now-unnecessary `# type: ignore` comments. - `python/tests/test_schema_parser.py`: add `TestGetSignatureFormatFromSchemaParams` (8 cases) covering 4- and 5-member unions, an `anyOf` option missing `type`, an all-typeless `anyOf`, and regressions for the previously supported single/2/3-member and nullable `[type, null]` shapes. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactor/Chore - [ ] Documentation - [ ] Breaking change ## How Has This Been Tested? From `python/`: ``` uv run pytest tests/test_schema_parser.py::TestGetSignatureFormatFromSchemaParams -v # 8 passed uv run pytest tests/test_schema_parser.py -q # 75 passed make chk # ruff + mypy -> clean, no new ignores ``` To confirm the tests guard the fix, the new test class was also run against the unmodified `shared.py`: 4 of the 8 fail there (`ValueError`/`KeyError` on the 4+/typeless cases), and all 8 pass with this change. The nullable `anyOf [type, null]` case still resolves to `Union[str, Any, NoneType]` (its pre-existing behavior); collapsing `null` into `Optional` lives in `schema_converter` and is intentionally left out of this minimal fix, with the test documenting current behavior. ## Screenshots (if applicable) N/A ## Checklist - [x] I have read the Code of Conduct and this PR adheres to it - [x] I ran linters/tests locally and they passed - [ ] I updated documentation as needed - [x] I added tests or explain why not applicable - [ ] I added a changeset if this change affects published packages ## Additional context No linked issue: this is a self-identified, reproduced bug in the older schema->signature path. The fix deliberately mirrors `schema_converter._build_union_from_options` so the two schema paths agree on N-ary union handling. The same bug family exists in `python/composio/utils/openapi.py` (`function_signature_from_jsonschema`, used by the google_adk provider) but is kept out of this PR to stay single-purpose; happy to follow up on it separately.

Anas Khan committed Jul 2, 2026 at 13:09 UTC b0ab8eacff0b76b853bd6e608226516e34710382
2 files changed +151 -14
python/composio/utils/shared.py
+10 -14
@@ -9,6 +9,7 @@ import json
9 import keyword
10 import typing as t
11 import uuid
12 +from functools import reduce
13 from inspect import Parameter
14
15 from pydantic import BaseModel, Field, create_model
@@ -580,21 +581,16 @@ def get_signature_format_from_schema_params(
581 param_type = param_allOf[0].get("type", None)
582 if param_oneOf is not None or param_anyOf is not None:
583 param_types = [ptype.get("type") for ptype in (param_oneOf or param_anyOf)]
583 - if len(param_types) == 1:
584 - annotation = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_types[0]]
585 - elif len(param_types) == 2:
586 - # Check as redefinition and union was incompatible
587 - # @karan to check if this is the right way to do it
588 - t1: t.Type = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_types[0]] # type: ignore
589 - t2: t.Type = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_types[1]] # type: ignore
590 - annotation: t.Type = t.Union[t1, t2] # type: ignore
591 - elif len(param_types) == 3:
592 - t1: t.Type = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_types[0]] # type: ignore
593 - t2: t.Type = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_types[1]] # type: ignore
594 - t3: t.Type = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_types[2]] # type: ignore
595 - annotation: t.Type = t.Union[t1, t2, t3] # type: ignore
584 + # Map each option to a Python type, falling back to t.Any for options
585 + # that are missing a "type" key or use an unrecognized type, then build
586 + # a Union for any count of members (no 1/2/3-member cap).
587 + mapped_types: t.List[t.Any] = [
588 + PYDANTIC_TYPE_TO_PYTHON_TYPE.get(ptype, t.Any) for ptype in param_types
589 + ]
590 + if len(mapped_types) == 1:
591 + annotation = mapped_types[0]
592 else:
597 - raise ValueError("Invalid 'oneOf' schema")
593 + annotation = reduce(lambda a, b: t.Union[a, b], mapped_types)
594 param_default = param_schema.get("default", "")
595 elif param_type in PYDANTIC_TYPE_TO_PYTHON_TYPE:
596 annotation = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_type]
python/tests/test_schema_parser.py
+141
@@ -12,6 +12,7 @@ from pydantic import BaseModel
12 from pydantic.fields import PydanticUndefined
13
14 from composio.utils.shared import (
15 + get_signature_format_from_schema_params,
16 json_schema_to_fields_dict,
17 json_schema_to_model,
18 json_schema_to_pydantic_field,
@@ -1462,5 +1463,145 @@ class TestBooleanDefaultCoercion:
1463 )
1464
1465
1466 +class TestGetSignatureFormatFromSchemaParams:
1467 + """Test cases for get_signature_format_from_schema_params union handling."""
1468 +
1469 + @staticmethod
1470 + def _annotation(schema):
1471 + params = get_signature_format_from_schema_params(schema)
1472 + assert len(params) == 1
1473 + return params[0].annotation
1474 +
1475 + @pytest.mark.unit
1476 + @pytest.mark.schema
1477 + def test_oneof_four_members(self):
1478 + """oneOf with 4 options builds a Union instead of raising ValueError."""
1479 + schema = {
1480 + "properties": {
1481 + "value": {
1482 + "oneOf": [
1483 + {"type": "string"},
1484 + {"type": "integer"},
1485 + {"type": "boolean"},
1486 + {"type": "number"},
1487 + ]
1488 + }
1489 + }
1490 + }
1491 +
1492 + annotation = self._annotation(schema)
1493 + assert t.get_origin(annotation) is t.Union
1494 + assert set(t.get_args(annotation)) == {str, int, bool, float}
1495 +
1496 + @pytest.mark.unit
1497 + @pytest.mark.schema
1498 + def test_oneof_five_members(self):
1499 + """oneOf with more than four options is also supported (no 3-member cap)."""
1500 + schema = {
1501 + "properties": {
1502 + "value": {
1503 + "oneOf": [
1504 + {"type": "string"},
1505 + {"type": "integer"},
1506 + {"type": "boolean"},
1507 + {"type": "number"},
1508 + {"type": "array"},
1509 + ]
1510 + }
1511 + }
1512 + }
1513 +
1514 + annotation = self._annotation(schema)
1515 + assert t.get_origin(annotation) is t.Union
1516 + assert len(t.get_args(annotation)) == 5
1517 +
1518 + @pytest.mark.unit
1519 + @pytest.mark.schema
1520 + def test_anyof_option_missing_type(self):
1521 + """An anyOf option without a 'type' key maps to Any instead of raising KeyError."""
1522 + schema = {
1523 + "properties": {
1524 + "value": {
1525 + "anyOf": [
1526 + {"description": "free-form value"},
1527 + {"type": "string"},
1528 + ]
1529 + }
1530 + }
1531 + }
1532 +
1533 + annotation = self._annotation(schema)
1534 + assert t.get_origin(annotation) is t.Union
1535 + args = t.get_args(annotation)
1536 + assert t.Any in args
1537 + assert str in args
1538 +
1539 + @pytest.mark.unit
1540 + @pytest.mark.schema
1541 + def test_anyof_all_options_missing_type(self):
1542 + """anyOf where every option lacks a 'type' collapses to a single Any annotation."""
1543 + schema = {
1544 + "properties": {
1545 + "value": {
1546 + "anyOf": [
1547 + {"description": "a"},
1548 + {"description": "b"},
1549 + ]
1550 + }
1551 + }
1552 + }
1553 +
1554 + assert self._annotation(schema) is t.Any
1555 +
1556 + @pytest.mark.unit
1557 + @pytest.mark.schema
1558 + def test_oneof_single_member(self):
1559 + """oneOf with a single option resolves to that type directly (unchanged)."""
1560 + schema = {"properties": {"value": {"oneOf": [{"type": "string"}]}}}
1561 + assert self._annotation(schema) is str
1562 +
1563 + @pytest.mark.unit
1564 + @pytest.mark.schema
1565 + def test_oneof_two_members(self):
1566 + """Two-member oneOf behavior is preserved (Union of the two types)."""
1567 + schema = {
1568 + "properties": {
1569 + "value": {"oneOf": [{"type": "string"}, {"type": "integer"}]}
1570 + }
1571 + }
1572 + assert self._annotation(schema) == t.Union[str, int]
1573 +
1574 + @pytest.mark.unit
1575 + @pytest.mark.schema
1576 + def test_oneof_three_members(self):
1577 + """Three-member oneOf behavior is preserved (Union of the three types)."""
1578 + schema = {
1579 + "properties": {
1580 + "value": {
1581 + "oneOf": [
1582 + {"type": "string"},
1583 + {"type": "integer"},
1584 + {"type": "boolean"},
1585 + ]
1586 + }
1587 + }
1588 + }
1589 + assert self._annotation(schema) == t.Union[str, int, bool]
1590 +
1591 + @pytest.mark.unit
1592 + @pytest.mark.schema
1593 + def test_anyof_with_null_member_still_resolves(self):
1594 + """Nullable anyOf [type, null] continues to resolve without raising."""
1595 + schema = {
1596 + "properties": {"value": {"anyOf": [{"type": "string"}, {"type": "null"}]}}
1597 + }
1598 +
1599 + annotation = self._annotation(schema)
1600 + assert t.get_origin(annotation) is t.Union
1601 + args = t.get_args(annotation)
1602 + assert str in args
1603 + assert type(None) in args
1604 +
1605 +
1606 if __name__ == "__main__":
1607 pytest.main([__file__, "-v"])