@devries-cameron20 / composio Mirror · read-only / commits / 3a5e62feb
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, 18:30 UTC

fix(py): support list-valued schema types in tool signatures (#3815)

This PR: - builds on top of https://github.com/ComposioHQ/composio/pull/3754 - normalizes list-valued JSON Schema `type` fields before scalar type lookups - applies the handling to direct fields and `anyOf`/`oneOf` options - adds regressions for nullable, single, mixed, unknown, and combiner-nested type lists - credits @anxkhn as a Git co-author ## Context JSON Schema Draft 2020-12 and OpenAPI 3.1 allow `type` to be an array. The previous parser passed that list to a dictionary lookup, raising `TypeError: unhashable type: 'list'`. This replacement covers the direct case from #3754 and the same shape nested in a combiner. --------- Co-authored-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>

Alberto Schiabel committed Jul 13, 2026 at 17:00 UTC 3a5e62feb4e3a9d1d50ffe40daf1ec8a2e16cba2
2 files changed +112 -3
python/composio/utils/shared.py
+31 -1
@@ -587,13 +587,22 @@ def get_signature_format_from_schema_params(
587 # that are missing a "type" key or use an unrecognized type, then build
588 # a Union for any count of members (no 1/2/3-member cap).
589 mapped_types: t.List[t.Any] = [
590 - PYDANTIC_TYPE_TO_PYTHON_TYPE.get(ptype, t.Any) for ptype in param_types
590 + _annotation_from_json_schema_type(ptype) for ptype in param_types
591 ]
592 if len(mapped_types) == 1:
593 annotation = mapped_types[0]
594 else:
595 annotation = reduce(lambda a, b: t.Union[a, b], mapped_types)
596 param_default = param_schema.get("default", "")
597 + elif isinstance(param_type, list):
598 + annotation = _annotation_from_json_schema_type(param_type)
599 + scalar_type = param_type[0] if len(param_type) == 1 else None
600 + if isinstance(scalar_type, str) and scalar_type in FALLBACK_VALUES:
601 + param_default = param_schema.get(
602 + "default", FALLBACK_VALUES[scalar_type]
603 + )
604 + else:
605 + param_default = param_schema.get("default", "")
606 elif param_type in PYDANTIC_TYPE_TO_PYTHON_TYPE:
607 annotation = PYDANTIC_TYPE_TO_PYTHON_TYPE[param_type]
608 param_default = param_schema.get("default", FALLBACK_VALUES[param_type])
@@ -625,6 +634,27 @@ def get_signature_format_from_schema_params(
634 return default_parameters + none_default_parameters
635
636
637 +def _annotation_from_json_schema_type(schema_type: t.Any) -> t.Any:
638 + """Convert a JSON Schema ``type`` value to a Python annotation.
639 +
640 + JSON Schema Draft 2020-12 and OpenAPI 3.1 allow ``type`` to be an array,
641 + including inside ``anyOf`` and ``oneOf`` branches. Keep that form from
642 + reaching the scalar dictionary lookup, where a list would be unhashable.
643 + """
644 + if isinstance(schema_type, list):
645 + mapped_types = [_annotation_from_json_schema_type(item) for item in schema_type]
646 + if not mapped_types:
647 + return t.Any
648 + if len(mapped_types) == 1:
649 + return mapped_types[0]
650 + return reduce(lambda left, right: t.Union[left, right], mapped_types)
651 +
652 + if isinstance(schema_type, str):
653 + return PYDANTIC_TYPE_TO_PYTHON_TYPE.get(schema_type, t.Any)
654 +
655 + return t.Any
656 +
657 +
658 def get_pydantic_signature_format_from_schema_params(
659 schema_params: t.Dict,
660 skip_default: bool = False,
python/tests/test_schema_parser.py
+81 -2
@@ -1496,10 +1496,14 @@ class TestGetSignatureFormatFromSchemaParams:
1496 """Test cases for get_signature_format_from_schema_params union handling."""
1497
1498 @staticmethod
1499 - def _annotation(schema):
1499 + def _parameter(schema):
1500 params = get_signature_format_from_schema_params(schema)
1501 assert len(params) == 1
1502 - return params[0].annotation
1502 + return params[0]
1503 +
1504 + @classmethod
1505 + def _annotation(cls, schema):
1506 + return cls._parameter(schema).annotation
1507
1508 @pytest.mark.unit
1509 @pytest.mark.schema
@@ -1631,6 +1635,81 @@ class TestGetSignatureFormatFromSchemaParams:
1635 assert str in args
1636 assert type(None) in args
1637
1638 + @pytest.mark.unit
1639 + @pytest.mark.schema
1640 + @pytest.mark.parametrize(
1641 + ("schema_type", "expected_members"),
1642 + [
1643 + (["string", "null"], {str, type(None)}),
1644 + (["integer"], {int}),
1645 + (["string", "integer"], {str, int}),
1646 + (["file", "null"], {t.Any, type(None)}),
1647 + ],
1648 + )
1649 + def test_type_list_resolves(self, schema_type, expected_members):
1650 + """List-valued JSON Schema types produce an annotation without raising."""
1651 + annotation = self._annotation({"properties": {"value": {"type": schema_type}}})
1652 + actual = set(t.get_args(annotation)) if t.get_args(annotation) else {annotation}
1653 + assert expected_members <= actual
1654 +
1655 + @pytest.mark.unit
1656 + @pytest.mark.schema
1657 + @pytest.mark.parametrize(
1658 + ("schema_type", "expected_annotation", "expected_default"),
1659 + [
1660 + (["integer"], int, 0),
1661 + (["boolean"], bool, False),
1662 + (["array"], t.List, []),
1663 + ],
1664 + )
1665 + def test_single_type_list_uses_scalar_fallback(
1666 + self, schema_type, expected_annotation, expected_default
1667 + ):
1668 + """A one-item type list keeps its scalar type's implicit default."""
1669 + parameter = self._parameter({"properties": {"value": {"type": schema_type}}})
1670 +
1671 + assert parameter.annotation is expected_annotation
1672 + assert parameter.default == expected_default
1673 +
1674 + @pytest.mark.unit
1675 + @pytest.mark.schema
1676 + def test_single_type_list_preserves_explicit_default(self):
1677 + """An explicit default overrides the fallback for a one-item type list."""
1678 + parameter = self._parameter(
1679 + {"properties": {"value": {"type": ["integer"], "default": 42}}}
1680 + )
1681 +
1682 + assert parameter.default == 42
1683 +
1684 + @pytest.mark.unit
1685 + @pytest.mark.schema
1686 + def test_multi_type_list_keeps_union_fallback(self):
1687 + """A multi-type list remains a union with the existing empty-string fallback."""
1688 + parameter = self._parameter(
1689 + {"properties": {"value": {"type": ["integer", "null"]}}}
1690 + )
1691 +
1692 + assert {int, type(None)} <= set(t.get_args(parameter.annotation))
1693 + assert parameter.default == ""
1694 +
1695 + @pytest.mark.unit
1696 + @pytest.mark.schema
1697 + def test_combiner_option_with_type_list_resolves(self):
1698 + """List-valued types inside combiners do not reach a scalar dict lookup."""
1699 + schema = {
1700 + "properties": {
1701 + "value": {
1702 + "anyOf": [
1703 + {"type": ["string", "null"]},
1704 + {"type": "integer"},
1705 + ]
1706 + }
1707 + }
1708 + }
1709 +
1710 + annotation = self._annotation(schema)
1711 + assert {str, int, type(None)} <= set(t.get_args(annotation))
1712 +
1713
1714 if __name__ == "__main__":
1715 pytest.main([__file__, "-v"])