@devries-cameron20 / composio Mirror · read-only / commits / 1d10cba30
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, 08:30 UTC

fix(python): preserve nested array item schemas for Google ADK (#4986)

## Summary Fixes #4983. Google ADK tools currently expose unconstrained array items when the input item schema uses `anyOf`/`oneOf` instead of a direct `type`. The equivalent list-valued `type` already preserves nullable items. This loses model-facing constraints without raising an error. ## Changes - Resolve array items through the existing schema converter even when `items.type` is absent. - Dispatch typeless nested combiners through the existing union/allOf helpers; preserve the prior enum/explicit-type precedence, object fallback, and unconstrained-schema behavior. - Bound recursive conversion at 64 levels, falling back to `Any` rather than recursing indefinitely. This bounds this converter, not every preprocessing stage in the SDK. - Add helper regressions and an offline test through the real `GoogleAdkProvider` and ADK tool declaration. No provider API, dependencies, or execution behavior changes. ## Type of change - [x] Bug fix - [ ] New feature - [ ] Refactor/Chore - [ ] Documentation - [ ] Breaking change ## How Has This Been Tested? Windows, Python 3.12.11; editable core/provider packages; google-genai 2.31.0, pydantic 2.14.1, pytest 9.1.1, Ruff 0.17.1. From `python/`: ```sh python -m pytest tests/test_openapi.py tests/test_google_adk_provider.py -q # 39 passed. Initially 8 failed before the item-schema fix (21 passed); # review regressions for depth and sibling precedence also failed before their fixes. python -m pytest tests/test_openapi.py tests/test_google_adk_provider.py tests/test_provider.py -q # 61 passed, 11 skipped (other optional providers are not installed). python -m ruff check --config config/ruff.toml composio/utils/openapi.py tests/test_openapi.py tests/test_google_adk_provider.py python -m ruff format --check --config config/ruff.toml composio/utils/openapi.py tests/test_openapi.py tests/test_google_adk_provider.py python -m ruff check --select I composio/utils/openapi.py tests/test_openapi.py tests/test_google_adk_provider.py git diff --check ``` All listed lint/format/diff checks pass. The four ADK declaration cases pass with both google-adk **2.12.0** and the provider's minimum **2.5.0**, in separate environments. These cover nullable items and a 96-level input exceeding the converter budget. Helper tests separately exercise 400-level combiners/arrays. A 400-level full-wrapper experiment instead fails earlier in the existing `alias_tool_input_schema` deepcopy; this PR does not fix or conceal that separate preprocessing limit. DNS/socket connections are blocked during declaration generation; no tool execution or model/API call is made. ADK emits its existing experimental JSON-schema feature warning. The optional provider test skips when `composio_google_adk` is not installed; the core helper regressions do not. The existing main Python CI does not install that optional provider. Additional checks were **not fully green**: - Adding `tests/test_schema_converter.py` to the three-file run: **275 passed, 11 skipped, 1 failed**. The failure is the existing `primitive-unicode-code-point-length[0]` case (`glyph: '𝄞' is too long`). Replacing only the changed production module in memory with the exact upstream version at `2d868cf88ce485db7076222aaa81045e524dddb7`, in the same environment, reproduces this same failure. No existing assertion was changed or skipped. - `python -m mypy --config-file config/mypy.ini composio/utils/openapi.py tests/test_openapi.py tests/test_google_adk_provider.py`, using the repository's mypy **2.3.1** and pinned type stubs: one error in the imported, unchanged `composio/utils/schema_converter.py:813` (`TypeAdapter` argument versus `TypeForm[Any]`). Checking that unchanged module alone reproduces the error. No type-check configuration or ignore was added. These are local results, not a claim that full `make chk`, `make tst`, live integration tests, or GitHub CI passed. ## Screenshots (if applicable) Not applicable. ## Checklist - [x] I have read the Code of Conduct and this PR adheres to it - [x] I have read and understand every line of this diff, including any LLM-generated code (Codex-assisted review; see below) - [x] Any third-party docs links or promotional content follow the third-party links policy (none added) - [ ] I ran linters/tests locally and they passed - [x] I updated documentation as needed (internal conversion fix; no usage/API changes) - [x] I added tests or explain why not applicable - [x] I added a changeset if this change affects published packages (not applicable: Python-only) ## Additional context Prepared with OpenAI Codex under gtdd123's standing authorization. The complete diff was read and reviewed by Codex, including the recursive conversion behavior and all regression assertions. Codex ran the checks above and checked current issue/PR overlap. This describes the automated review, not a separate personal review by the account owner. The test checkbox remains unchecked because the additional checks above have known failures. No human review or live-provider validation is claimed. This is separate from the Google/Vertex work in #4751 and the existing list-valued-type fix #3822. The branch was fast-forwarded to `next` at `fc33028fa85f0c66c95ff70f45557725ab1ae4e2` before the final focused test run. --------- Co-authored-by: jkomyno <alberto@composio.dev>

gtdd123 committed Oct 21, 2026 at 00:02 UTC 1d10cba30298e2f8146472217e449bc921835872
3 files changed +332 -21
python/composio/utils/openapi.py
+92 -21
@@ -13,6 +13,19 @@ OPENAPI_TO_PYTHON = {
13 "string": str,
14 }
15
16 +# Tool schemas come from the backend and may be arbitrarily deeply nested.
17 +_MAX_SCHEMA_DEPTH = 64
18 +# Bounds the schemas converted for one parameter: a schema that branches at
19 +# every level costs exponential work well within the depth bound.
20 +_MAX_SCHEMA_NODES = 2_000
21 +
22 +
23 +class _Budget:
24 + """Schema conversions left for one parameter; past it, a schema is `Any`."""
25 +
26 + def __init__(self) -> None:
27 + self.nodes = _MAX_SCHEMA_NODES
28 +
29
30 # pylint: disable=unused-argument
31 def _handle_object_type(schema: t.Dict) -> t.Type:
@@ -20,13 +33,13 @@ def _handle_object_type(schema: t.Dict) -> t.Type:
33 return t.Dict[str, t.Any]
34
35
23 -def _handle_array_type(schema: t.Dict) -> t.Any:
36 +def _handle_array_type(
37 + schema: t.Dict, *, _depth: int = 0, _budget: t.Optional[_Budget] = None
38 +) -> t.Any:
39 # This discards the nested objects
25 - items_type = schema.get("items", {}).get("type")
26 - if items_type is None:
27 - return t.List[t.Any]
28 -
29 - SubT = _type_to_parameter(schema=schema.get("items", {}))
40 + SubT = _type_to_parameter(
41 + schema=schema.get("items", {}), _depth=_depth + 1, _budget=_budget
42 + )
43 return t.List[SubT] # type: ignore
44
45
@@ -34,22 +47,49 @@ def _handle_enum_type(schema: t.Dict) -> t.Any:
47 return t.Literal[tuple(schema["enum"])]
48
49
37 -def _type_to_parameter(schema: t.Dict[str, t.Any]) -> t.Any:
50 +def _type_to_parameter(
51 + schema: t.Dict[str, t.Any],
52 + *,
53 + _depth: int = 0,
54 + _budget: t.Optional[_Budget] = None,
55 +) -> t.Any:
56 + if _budget is None:
57 + _budget = _Budget()
58 + if _depth >= _MAX_SCHEMA_DEPTH or _budget.nodes <= 0:
59 + return t.Any
60 + _budget.nodes -= 1
61 if "enum" in schema:
62 return _handle_enum_type(schema=schema)
63
64 p_type = schema.get("type")
65 + # Preserve the existing enum/type precedence for already-typed schemas.
66 + if p_type is None:
67 + if "oneOf" in schema:
68 + return _one_of_to_parameter(schema=schema, _depth=_depth, _budget=_budget)
69 + if "anyOf" in schema:
70 + return _any_of_to_parameter(schema=schema, _depth=_depth, _budget=_budget)
71 + if "allOf" in schema:
72 + return _all_of_to_parameter(schema=schema, _depth=_depth, _budget=_budget)
73 if isinstance(p_type, list):
74 # JSON Schema Draft 2020-12 / OpenAPI 3.1 express a nullable field as a
75 # list of types, e.g. {"type": ["string", "null"]}, rather than an anyOf.
76 if not p_type:
77 return t.Any
47 - return t.Union[
48 - tuple(
49 - _type_to_parameter(schema={**schema, "type": member})
50 - for member in p_type
78 + members = []
79 + for member in p_type:
80 + # Each member copies the whole schema, so stop once nothing is left
81 + # to convert it with.
82 + if _budget.nodes <= 0:
83 + members.append(t.Any)
84 + break
85 + members.append(
86 + _type_to_parameter(
87 + schema={**schema, "type": member},
88 + _depth=_depth + 1,
89 + _budget=_budget,
90 + )
91 )
52 - ]
92 + return t.Union[tuple(members)]
93
94 if p_type in OPENAPI_TO_PYTHON:
95 return OPENAPI_TO_PYTHON[p_type]
@@ -58,7 +98,7 @@ def _type_to_parameter(schema: t.Dict[str, t.Any]) -> t.Any:
98 return _handle_object_type(schema=schema)
99
100 if p_type == "array":
61 - return _handle_array_type(schema=schema)
101 + return _handle_array_type(schema=schema, _depth=_depth, _budget=_budget)
102
103 if p_type is None:
104 # No type specified (e.g. a combiner option that is description-only or
@@ -68,26 +108,57 @@ def _type_to_parameter(schema: t.Dict[str, t.Any]) -> t.Any:
108 raise InvalidSchemaError(f"Invalid property type {p_type}: {schema!r}")
109
110
71 -def _handle_composite_type(schemas: t.List[t.Dict]) -> t.Any:
111 +def _handle_composite_type(
112 + schemas: t.List[t.Dict],
113 + *,
114 + _depth: int = 0,
115 + _budget: t.Optional[_Budget] = None,
116 +) -> t.Any:
117 if not schemas:
118 # An empty oneOf/anyOf has no options to union; fall back to Any.
119 return t.Any
75 - return t.Union[tuple(map(_type_to_parameter, schemas))]
120 + if _budget is None:
121 + _budget = _Budget()
122 + return t.Union[
123 + tuple(
124 + _type_to_parameter(schema=schema, _depth=_depth + 1, _budget=_budget)
125 + for schema in schemas
126 + )
127 + ]
128
129
78 -def _one_of_to_parameter(schema: t.Dict[str, t.Any]) -> t.Any:
79 - return _handle_composite_type(schemas=schema["oneOf"])
130 +def _one_of_to_parameter(
131 + schema: t.Dict[str, t.Any],
132 + *,
133 + _depth: int = 0,
134 + _budget: t.Optional[_Budget] = None,
135 +) -> t.Any:
136 + return _handle_composite_type(
137 + schemas=schema["oneOf"], _depth=_depth, _budget=_budget
138 + )
139
140
82 -def _any_of_to_parameter(schema: t.Dict[str, t.Any]) -> t.Any:
83 - return _handle_composite_type(schemas=schema["anyOf"])
141 +def _any_of_to_parameter(
142 + schema: t.Dict[str, t.Any],
143 + *,
144 + _depth: int = 0,
145 + _budget: t.Optional[_Budget] = None,
146 +) -> t.Any:
147 + return _handle_composite_type(
148 + schemas=schema["anyOf"], _depth=_depth, _budget=_budget
149 + )
150
151
86 -def _all_of_to_parameter(schema: t.Dict[str, t.Any]) -> t.Type:
152 +def _all_of_to_parameter(
153 + schema: t.Dict[str, t.Any],
154 + *,
155 + _depth: int = 0,
156 + _budget: t.Optional[_Budget] = None,
157 +) -> t.Type:
158 composite = {}
159 for subschema in schema["allOf"]:
160 composite.update(subschema)
90 - return _type_to_parameter(schema=composite)
161 + return _type_to_parameter(schema=composite, _depth=_depth + 1, _budget=_budget)
162
163
164 def function_signature_from_jsonschema(
python/tests/test_google_adk_provider.py new
+50
@@ -0,0 +1,50 @@
1 +"""Exercise item schemas through the real optional Google ADK provider."""
2 +
3 +import socket
4 +
5 +import pytest
6 +
7 +from composio.client.types import Tool
8 +
9 +google_adk = pytest.importorskip("composio_google_adk")
10 +
11 +
12 +@pytest.mark.unit
13 +@pytest.mark.schema
14 +@pytest.mark.parametrize("combiner", ["anyOf", "oneOf"])
15 +# Exceed the converter budget without hitting unrelated schema-copy limits.
16 +@pytest.mark.parametrize("depth", [0, 96], ids=["nullable", "depth-limit"])
17 +def test_array_items_reach_the_adk_declaration(combiner, depth, monkeypatch):
18 + def reject_network(*args, **kwargs):
19 + pytest.fail("Tool declaration generation must not use the network")
20 +
21 + monkeypatch.setattr(socket.socket, "connect", reject_network)
22 + monkeypatch.setattr(socket, "getaddrinfo", reject_network)
23 + items = {combiner: [{"type": "string"}, {"type": "null"}]}
24 + for _ in range(depth):
25 + items = {combiner: [items]}
26 + tool = Tool.model_construct(
27 + slug="offline_schema_probe",
28 + description="Offline schema probe",
29 + input_parameters={
30 + "type": "object",
31 + "properties": {
32 + "values": {
33 + "type": "array",
34 + "items": items,
35 + }
36 + },
37 + "required": ["values"],
38 + },
39 + )
40 +
41 + def execute_tool(**kwargs):
42 + pytest.fail("Generating a tool declaration must not execute the tool")
43 +
44 + wrapped = google_adk.GoogleAdkProvider().wrap_tool(tool, execute_tool=execute_tool)
45 + declaration = wrapped._get_declaration()
46 + expected_items = {} if depth else {"anyOf": [{"type": "string"}, {"type": "null"}]}
47 + assert (
48 + declaration.parameters_json_schema["properties"]["values"]["items"]
49 + == expected_items
50 + )
python/tests/test_openapi.py
+190
@@ -11,6 +11,7 @@ import typing as t
11 import pytest
12
13 from composio.exceptions import InvalidSchemaError
14 +from composio.utils import openapi
15 from composio.utils.openapi import function_signature_from_jsonschema
16
17
@@ -152,3 +153,192 @@ class TestListValuedType:
153 @pytest.mark.schema
154 def test_empty_list_resolves_to_any(self):
155 assert _annotation({"type": []}) is t.Any
156 +
157 +
158 +class TestArrayItemSchemas:
159 + @pytest.mark.unit
160 + @pytest.mark.schema
161 + @pytest.mark.parametrize("combiner", ["anyOf", "oneOf"])
162 + def test_nullable_combiner_items(self, combiner: str):
163 + assert (
164 + _annotation(
165 + {
166 + "type": "array",
167 + "items": {combiner: [{"type": "string"}, {"type": "null"}]},
168 + }
169 + )
170 + == t.List[t.Optional[str]]
171 + )
172 +
173 + @pytest.mark.unit
174 + @pytest.mark.schema
175 + def test_nested_array_combiner_items(self):
176 + assert (
177 + _annotation(
178 + {
179 + "type": "array",
180 + "items": {
181 + "type": "array",
182 + "items": {"anyOf": [{"type": "string"}, {"type": "null"}]},
183 + },
184 + }
185 + )
186 + == t.List[t.List[t.Optional[str]]]
187 + )
188 +
189 + @pytest.mark.unit
190 + @pytest.mark.schema
191 + def test_combiner_nested_in_an_item_option(self):
192 + assert (
193 + _annotation(
194 + {
195 + "type": "array",
196 + "items": {
197 + "anyOf": [
198 + {"oneOf": [{"type": "string"}, {"type": "integer"}]},
199 + {"type": "null"},
200 + ]
201 + },
202 + }
203 + )
204 + == t.List[t.Union[str, int, None]]
205 + )
206 +
207 + @pytest.mark.unit
208 + @pytest.mark.schema
209 + def test_all_of_items(self):
210 + assert (
211 + _annotation({"type": "array", "items": {"allOf": [{"type": "string"}]}})
212 + == t.List[str]
213 + )
214 +
215 + @pytest.mark.unit
216 + @pytest.mark.schema
217 + def test_enum_items_without_explicit_type(self):
218 + assert (
219 + _annotation({"type": "array", "items": {"enum": ["open", "closed"]}})
220 + == t.List[t.Literal["open", "closed"]]
221 + )
222 +
223 + @pytest.mark.unit
224 + @pytest.mark.schema
225 + @pytest.mark.parametrize("combiner", ["anyOf", "oneOf"])
226 + def test_item_enum_keeps_priority_over_combiner(self, combiner: str):
227 + assert (
228 + _annotation(
229 + {
230 + "type": "array",
231 + "items": {
232 + "type": "string",
233 + "enum": ["open", "closed"],
234 + combiner: [{"type": "string"}, {"type": "null"}],
235 + },
236 + }
237 + )
238 + == t.List[t.Literal["open", "closed"]]
239 + )
240 +
241 + @pytest.mark.unit
242 + @pytest.mark.schema
243 + @pytest.mark.parametrize("combiner", ["anyOf", "oneOf"])
244 + def test_item_type_keeps_priority_over_combiner(self, combiner: str):
245 + assert (
246 + _annotation(
247 + {
248 + "type": "array",
249 + "items": {
250 + "type": "string",
251 + combiner: [{"type": "string"}, {"type": "null"}],
252 + },
253 + }
254 + )
255 + == t.List[str]
256 + )
257 +
258 + @pytest.mark.unit
259 + @pytest.mark.schema
260 + @pytest.mark.parametrize(
261 + "prop",
262 + [
263 + {"type": "array"},
264 + {"type": "array", "items": {}},
265 + {"type": "array", "items": {"anyOf": []}},
266 + ],
267 + )
268 + def test_unconstrained_items_still_resolve_to_any(self, prop: t.Dict[str, t.Any]):
269 + assert _annotation(prop) == t.List[t.Any]
270 +
271 + @pytest.mark.unit
272 + @pytest.mark.schema
273 + @pytest.mark.parametrize("combiner", ["anyOf", "oneOf", "allOf"])
274 + def test_deeply_nested_item_combiners_are_bounded(self, combiner: str):
275 + items: t.Dict[str, t.Any] = {"type": "string"}
276 + for _ in range(400):
277 + items = {combiner: [items]}
278 + assert _annotation({"type": "array", "items": items}) == t.List[t.Any]
279 +
280 + @pytest.mark.unit
281 + @pytest.mark.schema
282 + def test_deeply_nested_array_items_are_bounded(self):
283 + prop: t.Dict[str, t.Any] = {"type": "string"}
284 + for _ in range(400):
285 + prop = {"type": "array", "items": prop}
286 + annotation = _annotation(prop)
287 + depth = 0
288 + while t.get_origin(annotation) is list:
289 + depth += 1
290 + annotation = t.get_args(annotation)[0]
291 + assert 0 < depth < 400
292 + assert annotation is t.Any
293 +
294 + @pytest.mark.unit
295 + @pytest.mark.schema
296 + @pytest.mark.parametrize(
297 + "wrap",
298 + [
299 + lambda inner: {"type": ["array"] * 8, "items": inner},
300 + lambda inner: {"type": "array", "items": {"anyOf": [inner] * 8}},
301 + ],
302 + ids=["type-list", "item-combiner"],
303 + )
304 + def test_branching_item_schemas_are_bounded_in_work(
305 + self,
306 + wrap: t.Callable[[t.Dict[str, t.Any]], t.Dict[str, t.Any]],
307 + monkeypatch: pytest.MonkeyPatch,
308 + ):
309 + # Eight branches per level make the work grow 8x per level, far below
310 + # the depth bound; without a work budget this never finishes.
311 + prop: t.Dict[str, t.Any] = {"type": "string"}
312 + for _ in range(30):
313 + prop = wrap(prop)
314 + calls = 0
315 + convert = openapi._type_to_parameter
316 +
317 + def counting(*args: t.Any, **kwargs: t.Any) -> t.Any:
318 + nonlocal calls
319 + calls += 1
320 + return convert(*args, **kwargs)
321 +
322 + monkeypatch.setattr(openapi, "_type_to_parameter", counting)
323 + _annotation(prop)
324 + assert calls < 100_000
325 +
326 + @pytest.mark.unit
327 + @pytest.mark.schema
328 + def test_long_type_list_stops_at_the_work_budget(
329 + self, monkeypatch: pytest.MonkeyPatch
330 + ):
331 + # Every member copies the whole schema, so members past the budget
332 + # must not be visited at all.
333 + prop: t.Dict[str, t.Any] = {"type": ["string"] * 10_000}
334 + calls = 0
335 + convert = openapi._type_to_parameter
336 +
337 + def counting(*args: t.Any, **kwargs: t.Any) -> t.Any:
338 + nonlocal calls
339 + calls += 1
340 + return convert(*args, **kwargs)
341 +
342 + monkeypatch.setattr(openapi, "_type_to_parameter", counting)
343 + assert _annotation(prop) == t.Union[str, t.Any]
344 + assert calls <= openapi._MAX_SCHEMA_NODES + 1