@devries-cameron20 / composio-1 / commits / 3b28dc756
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.

fix(py): handle list-valued JSON Schema type in openapi signatures (#3822)

`_type_to_parameter` in `python/composio/utils/openapi.py` assumed a property's `type` was a scalar string. JSON Schema Draft 2020-12 and OpenAPI 3.1 can express a nullable field as a list of types instead, for example `{"type": ["string", "null"]}`. Passing that list to the scalar membership check raised `TypeError: unhashable type: 'list'`, which could break tool signature generation in providers such as `google_adk`. ## Changes - Handle list-valued `type` definitions as unions. - Re-run every member through `_type_to_parameter` with a copy of the full property schema and only `type` replaced. This preserves sibling fields such as an array's `items`, so `{"type": ["array", "null"], "items": ...}` resolves to `Optional[List[...]]`. - Preserve scalar error behavior for invalid member types: an unknown type in a list raises `InvalidSchemaError` instead of silently becoming `Any`. - Keep the existing empty-list fallback to `Any`. - Add regression coverage for nullable arrays and scalar/list unknown-type parity alongside the existing list-valued type cases. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactor/Chore - [ ] Documentation - [ ] Breaking change ## Verification From `python/`: - `uv run pytest tests/test_openapi.py -q` — 18 passed. - `make chk` — Ruff and mypy passed across the Python SDK, providers, tests, examples, and scripts. ## 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 (not applicable: internal helper) - [x] I added regression tests - [ ] I added a changeset (not applicable: Python-only change) --------- Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com> Signed-off-by: jkomyno <alberto@composio.dev> Co-authored-by: jkomyno <alberto@composio.dev>

Anas Khan committed Jul 16, 2026 at 01:04 UTC 3b28dc756ecd6f0bd6780a3b8f8f33d61ea38cb1
2 files changed +58
python/composio/utils/openapi.py
+12
@@ -39,6 +39,18 @@ def _type_to_parameter(schema: t.Dict[str, t.Any]) -> t.Any:
39 return _handle_enum_type(schema=schema)
40
41 p_type = schema.get("type")
42 + if isinstance(p_type, list):
43 + # JSON Schema Draft 2020-12 / OpenAPI 3.1 express a nullable field as a
44 + # list of types, e.g. {"type": ["string", "null"]}, rather than an anyOf.
45 + if not p_type:
46 + return t.Any
47 + return t.Union[
48 + tuple(
49 + _type_to_parameter(schema={**schema, "type": member})
50 + for member in p_type
51 + )
52 + ]
53 +
54 if p_type in OPENAPI_TO_PYTHON:
55 return OPENAPI_TO_PYTHON[p_type]
56
python/tests/test_openapi.py
+46
@@ -10,6 +10,7 @@ import typing as t
10
11 import pytest
12
13 +from composio.exceptions import InvalidSchemaError
14 from composio.utils.openapi import function_signature_from_jsonschema
15
16
@@ -106,3 +107,48 @@ class TestExistingShapesUnchanged:
107 @pytest.mark.schema
108 def test_typeless_property_is_any(self):
109 assert _annotation({"description": "free form"}) is t.Any
110 +
111 +
112 +class TestListValuedType:
113 + """A list-valued ``type`` (e.g. ``["string", "null"]``) resolves to a Union.
114 +
115 + JSON Schema Draft 2020-12 / OpenAPI 3.1 express a nullable field as a list of
116 + types rather than an ``anyOf``. A list is unhashable, so the older
117 + ``p_type in OPENAPI_TO_PYTHON`` membership test raised
118 + ``TypeError: unhashable type: 'list'`` and crashed tool wrapping.
119 + """
120 +
121 + @pytest.mark.unit
122 + @pytest.mark.schema
123 + def test_nullable_list_is_optional(self):
124 + assert _annotation({"type": ["string", "null"]}) == t.Optional[str]
125 +
126 + @pytest.mark.unit
127 + @pytest.mark.schema
128 + def test_single_member_list_resolves_to_that_type(self):
129 + assert _annotation({"type": ["integer"]}) is int
130 +
131 + @pytest.mark.unit
132 + @pytest.mark.schema
133 + def test_multiple_non_null_members_build_a_union(self):
134 + assert _annotation({"type": ["string", "integer"]}) == t.Union[str, int]
135 +
136 + @pytest.mark.unit
137 + @pytest.mark.schema
138 + def test_nullable_array_preserves_items_schema(self):
139 + assert (
140 + _annotation({"type": ["array", "null"], "items": {"type": "string"}})
141 + == t.Optional[t.List[str]]
142 + )
143 +
144 + @pytest.mark.unit
145 + @pytest.mark.schema
146 + @pytest.mark.parametrize("property_type", ["file", ["file", "null"]])
147 + def test_unrecognized_member_raises_invalid_schema(self, property_type: t.Any):
148 + with pytest.raises(InvalidSchemaError, match="Invalid property type file"):
149 + _annotation({"type": property_type})
150 +
151 + @pytest.mark.unit
152 + @pytest.mark.schema
153 + def test_empty_list_resolves_to_any(self):
154 + assert _annotation({"type": []}) is t.Any