@devries-cameron20 / composio-1 / commits / 8cb290be3
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(schema_converter): allOf containing a false member must reject every value (#3921)

### Summary Preserves JSON Schema boolean-schema semantics throughout the Python schema converter: - an unsatisfiable `allOf` now propagates through nested combiners instead of being dropped; - impossible array items, object properties, and local definition references remain rejecting; - composed impossible schemas use a Pydantic type that rejects every value, including JSON `null`; - the existing `json_schema_to_pydantic_type(False) is None` compatibility behavior remains unchanged. ### Problem The original fix handled only a literal `false` directly inside one `allOf`. Recursive filtering still interpreted the same `None` value as both "discard this union branch" and "the whole schema is impossible." That widened nested `allOf` schemas to `str`, removed impossible properties/items/definitions, and allowed JSON `null` because Pydantic treats a bare `None` annotation as `NoneType`. ### Fix - Add a private unsatisfiable marker with Pydantic core- and JSON-schema hooks. - Drop that marker only from `anyOf`/`oneOf`; propagate it through `allOf`. - Preserve rejecting semantics for nested object properties, array items, and local `$defs`/`definitions` references. - Emit `{"not": {}}` from the rejecting Pydantic type. - Correct the related empty-`allOf` and all-false-`anyOf` expectations. ### Testing - `uv run pytest tests/test_schema_converter.py tests/test_schema_parser.py -q` - 106 passed - `uv run pytest -m schema -q` - 71 passed - `make tst` - 928 passed - `make chk` - Ruff and mypy passed across core, providers, tests, and scripts Regression coverage includes direct and nested `allOf`, JSON `null`, empty arrays with impossible items, optional and required impossible properties, local definition references, generated JSON Schema, and shared model consumers. ### Note #3876 adds coverage for the same function's `anyOf` handling but does not change this `allOf` propagation path. --------- Co-authored-by: jkomyno <alberto@composio.dev>

manjunath bhaskar committed Jul 28, 2026 at 10:45 UTC 8cb290be35769b9f3746e161772cd79e2e210fe1
3 files changed +354 -36
python/composio/utils/schema_converter.py
+191 -25
@@ -17,11 +17,42 @@ from json_schema_to_pydantic import (
17 SchemaError,
18 create_model as create_model_from_schema,
19 )
20 +from pydantic import Field, create_model as create_pydantic_model
21 +from pydantic_core import core_schema
22
23 from composio.utils.logging import get as get_logger
24
25 logger = get_logger(__name__)
26
27 +
28 +class _UnsatisfiableSchema:
29 + """Pydantic type for JSON schemas that reject every value."""
30 +
31 + @staticmethod
32 + def _reject(_value: t.Any) -> t.NoReturn:
33 + raise ValueError("schema is unsatisfiable (JSON Schema `false`)")
34 +
35 + @classmethod
36 + def __get_pydantic_core_schema__(
37 + cls,
38 + _source_type: t.Any,
39 + _handler: t.Any,
40 + ) -> core_schema.CoreSchema:
41 + return core_schema.no_info_plain_validator_function(cls._reject)
42 +
43 + @classmethod
44 + def __get_pydantic_json_schema__(
45 + cls,
46 + _core_schema: core_schema.CoreSchema,
47 + _handler: t.Any,
48 + ) -> t.Dict[str, t.Any]:
49 + return {"not": {}}
50 +
51 +
52 +def _is_unsatisfiable_schema(schema: t.Any) -> bool:
53 + return schema is _UnsatisfiableSchema
54 +
55 +
56 # Type mapping for simple cases where we don't need full model creation
57 PYDANTIC_TYPE_TO_PYTHON_TYPE = {
58 "string": str,
@@ -51,7 +82,7 @@ FALLBACK_VALUES = {
82
83 def _filter_boolean_schemas(
84 schema: t.Union[t.Dict[str, t.Any], bool, t.List],
54 -) -> t.Union[t.Dict[str, t.Any], t.Any, None]:
85 +) -> t.Any:
86 """
87 Pre-filter boolean schemas from anyOf/allOf/oneOf arrays.
88
@@ -61,9 +92,12 @@ def _filter_boolean_schemas(
92
93 The json-schema-to-pydantic library doesn't handle these, so we:
94 - Replace `true` with {} (empty schema, accepts anything)
64 - - Filter out `false` (rejects everything, so no point including it)
95 + - Drop `false` from union combiners (`anyOf`/`oneOf`)
96 + - Propagate an unsatisfiable marker through conjunctions and nested schemas
97
66 - Returns None if the schema is a standalone `false` boolean.
98 + `None` remains the standalone-`false` filter sentinel for compatibility.
99 + Composed schemas use `_UnsatisfiableSchema` so callers can distinguish
100 + "drop this union branch" from "reject every value."
101 """
102 if isinstance(schema, bool):
103 if schema:
@@ -78,7 +112,7 @@ def _filter_boolean_schemas(
112 filtered = []
113 for item in schema:
114 result = _filter_boolean_schemas(item)
81 - if result is not None:
115 + if result is not None and not _is_unsatisfiable_schema(result):
116 filtered.append(result)
117 return filtered if filtered else None
118
@@ -88,35 +122,48 @@ def _filter_boolean_schemas(
122 # Make a copy to avoid mutating the original
123 result = {}
124 for key, value in schema.items():
91 - if key in ("anyOf", "allOf", "oneOf"):
92 - # Filter boolean schemas from combiner arrays
93 - filtered_value = _filter_boolean_schemas(value)
94 - if filtered_value is None or (
95 - isinstance(filtered_value, list) and len(filtered_value) == 0
125 + if key == "allOf" and isinstance(value, list):
126 + filtered_members = [_filter_boolean_schemas(member) for member in value]
127 + if any(
128 + member is None or _is_unsatisfiable_schema(member)
129 + for member in filtered_members
130 ):
97 - # All schemas were false, skip this combiner
98 - continue
99 - result[key] = filtered_value
131 + return _UnsatisfiableSchema
132 + result[key] = filtered_members
133 + elif key in ("anyOf", "oneOf") and isinstance(value, list):
134 + filtered_members = []
135 + for member in value:
136 + filtered_member = _filter_boolean_schemas(member)
137 + if filtered_member is not None and not _is_unsatisfiable_schema(
138 + filtered_member
139 + ):
140 + filtered_members.append(filtered_member)
141 + if not filtered_members:
142 + return _UnsatisfiableSchema
143 + result[key] = filtered_members
144 elif key == "items" and isinstance(value, (dict, bool)):
145 # Handle array items schema
146 filtered_items = _filter_boolean_schemas(value)
103 - if filtered_items is not None:
104 - result[key] = filtered_items
147 + result[key] = (
148 + _UnsatisfiableSchema if filtered_items is None else filtered_items
149 + )
150 elif key == "properties" and isinstance(value, dict):
151 # Recursively filter property schemas
152 filtered_props = {}
153 for prop_name, prop_schema in value.items():
154 filtered_prop = _filter_boolean_schemas(prop_schema)
110 - if filtered_prop is not None:
111 - filtered_props[prop_name] = filtered_prop
155 + filtered_props[prop_name] = (
156 + _UnsatisfiableSchema if filtered_prop is None else filtered_prop
157 + )
158 result[key] = filtered_props
159 elif key in ("$defs", "definitions") and isinstance(value, dict):
160 # Recursively filter definitions
161 filtered_defs = {}
162 for def_name, def_schema in value.items():
163 filtered_def = _filter_boolean_schemas(def_schema)
118 - if filtered_def is not None:
119 - filtered_defs[def_name] = filtered_def
164 + filtered_defs[def_name] = (
165 + _UnsatisfiableSchema if filtered_def is None else filtered_def
166 + )
167 result[key] = filtered_defs
168 else:
169 result[key] = value
@@ -124,6 +171,56 @@ def _filter_boolean_schemas(
171 return result
172
173
174 +def _resolve_unsatisfiable_references(schema: t.Any) -> t.Any:
175 + """Replace local references to unsatisfiable definitions with the marker."""
176 + if not isinstance(schema, dict):
177 + return schema
178 +
179 + unsatisfiable_refs = set()
180 + for definitions_key in ("$defs", "definitions"):
181 + definitions = schema.get(definitions_key)
182 + if not isinstance(definitions, dict):
183 + continue
184 + for name, definition in definitions.items():
185 + if _is_unsatisfiable_schema(definition):
186 + unsatisfiable_refs.add(f"#/{definitions_key}/{name}")
187 +
188 + if not unsatisfiable_refs:
189 + return schema
190 +
191 + def replace(value: t.Any) -> t.Any:
192 + if isinstance(value, list):
193 + return [replace(item) for item in value]
194 + if not isinstance(value, dict):
195 + return value
196 + if value.get("$ref") in unsatisfiable_refs:
197 + return _UnsatisfiableSchema
198 +
199 + replaced = {}
200 + for key, item in value.items():
201 + if key in ("$defs", "definitions") and isinstance(item, dict):
202 + replaced[key] = {
203 + name: replace(definition)
204 + for name, definition in item.items()
205 + if not _is_unsatisfiable_schema(definition)
206 + }
207 + else:
208 + replaced[key] = replace(item)
209 + return replaced
210 +
211 + return replace(schema)
212 +
213 +
214 +def _contains_unsatisfiable_schema(schema: t.Any) -> bool:
215 + if _is_unsatisfiable_schema(schema):
216 + return True
217 + if isinstance(schema, list):
218 + return any(_contains_unsatisfiable_schema(item) for item in schema)
219 + if isinstance(schema, dict):
220 + return any(_contains_unsatisfiable_schema(value) for value in schema.values())
221 + return False
222 +
223 +
224 def json_schema_to_pydantic_type(
225 json_schema: t.Union[t.Dict[str, t.Any], bool],
226 ) -> t.Union[t.Type, t.Optional[t.Any]]:
@@ -145,15 +242,21 @@ def json_schema_to_pydantic_type(
242
243 # Pre-filter boolean schemas from combiners
244 filtered_schema = _filter_boolean_schemas(json_schema)
148 - if filtered_schema is None:
149 - return str # Fallback if all schemas were false
245 + filtered_schema = _resolve_unsatisfiable_references(filtered_schema)
246 + return _filtered_schema_to_pydantic_type(filtered_schema)
247 +
248 +
249 +def _filtered_schema_to_pydantic_type(schema: t.Any) -> t.Type[t.Any]:
250 + """Convert a schema after boolean-schema normalization."""
251 + if schema is None or _is_unsatisfiable_schema(schema):
252 + return _UnsatisfiableSchema
253
254 # Handle simple primitive types without complex combiners
152 - if _is_simple_primitive(filtered_schema):
153 - return _convert_simple_type(filtered_schema)
255 + if _is_simple_primitive(schema):
256 + return _convert_simple_type(schema)
257
258 # Use library for complex schemas (anyOf, allOf, oneOf, nested objects)
156 - return _convert_with_library(filtered_schema)
259 + return _convert_with_library(schema)
260
261
262 def _is_simple_primitive(schema: t.Dict[str, t.Any]) -> bool:
@@ -176,6 +279,65 @@ def _convert_simple_type(schema: t.Dict[str, t.Any]) -> t.Type[t.Any]:
279 return t.cast(t.Type[t.Any], PYDANTIC_TYPE_TO_PYTHON_TYPE.get(type_, str))
280
281
282 +def _convert_object_with_unsatisfiable_properties(
283 + schema: t.Dict[str, t.Any],
284 +) -> t.Type[t.Any]:
285 + """Build an object model while retaining always-rejecting properties."""
286 + properties = schema.get("properties", {})
287 + required = set(schema.get("required", []))
288 + rejecting_properties = {
289 + name: prop_schema
290 + for name, prop_schema in properties.items()
291 + if _contains_unsatisfiable_schema(prop_schema)
292 + }
293 + ordinary_properties = {
294 + name: prop_schema
295 + for name, prop_schema in properties.items()
296 + if name not in rejecting_properties
297 + }
298 +
299 + model_name = schema.get("title", "GeneratedModel")
300 + base_schema = {
301 + **schema,
302 + "title": model_name,
303 + "properties": ordinary_properties,
304 + "required": [
305 + name for name in schema.get("required", []) if name in ordinary_properties
306 + ],
307 + }
308 + base_model = create_model_from_schema(
309 + base_schema,
310 + allow_undefined_array_items=True,
311 + allow_undefined_type=True,
312 + )
313 +
314 + field_definitions = {}
315 + for name, prop_schema in rejecting_properties.items():
316 + annotation = _filtered_schema_to_pydantic_type(prop_schema)
317 + default = (
318 + ...
319 + if name in required
320 + else prop_schema.get("default")
321 + if isinstance(prop_schema, dict)
322 + else None
323 + )
324 + field_kwargs = {}
325 + if isinstance(prop_schema, dict):
326 + for metadata_key in ("description", "examples", "title"):
327 + if metadata_key in prop_schema:
328 + field_kwargs[metadata_key] = prop_schema[metadata_key]
329 + field_definitions[name] = (
330 + annotation,
331 + Field(default, **field_kwargs),
332 + )
333 +
334 + return create_pydantic_model( # type: ignore[call-overload]
335 + model_name,
336 + __base__=base_model,
337 + **field_definitions,
338 + )
339 +
340 +
341 def _convert_with_library(
342 schema: t.Dict[str, t.Any],
343 ) -> t.Union[t.Type, t.Any]:
@@ -190,6 +352,8 @@ def _convert_with_library(
352
353 # For object schemas, create model directly
354 if schema.get("type") == "object":
355 + if _contains_unsatisfiable_schema(schema.get("properties", {})):
356 + return _convert_object_with_unsatisfiable_properties(schema)
357 if "title" not in schema:
358 schema = {**schema, "title": "GeneratedModel"}
359 return create_model_from_schema(
@@ -201,8 +365,10 @@ def _convert_with_library(
365 # For array schemas
366 if schema.get("type") == "array":
367 items = schema.get("items")
368 + if _is_unsatisfiable_schema(items):
369 + return t.List[_UnsatisfiableSchema] # type: ignore[return-value]
370 if items:
205 - item_type = json_schema_to_pydantic_type(items)
371 + item_type = _filtered_schema_to_pydantic_type(items)
372 return t.List[t.cast(t.Type, item_type)] # type: ignore
373 return t.List
374
@@ -272,7 +438,7 @@ def _build_union_from_options(options: t.List[t.Dict[str, t.Any]]) -> t.Type:
438 pydantic_types.append(ptype)
439
440 if len(pydantic_types) == 0:
275 - return t.Optional[t.Any] if has_null else str # type: ignore
441 + return t.Optional[t.Any] if has_null else _UnsatisfiableSchema # type: ignore
442
443 if len(pydantic_types) == 1:
444 base_type = pydantic_types[0]
python/tests/test_schema_converter.py
+155 -5
@@ -1,6 +1,13 @@
1 """Tests for the schema_converter boolean-schema pre-filter."""
2
3 -from composio.utils.schema_converter import _filter_boolean_schemas
3 +import pytest
4 +from pydantic import TypeAdapter, ValidationError
5 +
6 +from composio.utils.schema_converter import (
7 + _filter_boolean_schemas,
8 + json_schema_to_pydantic_type,
9 +)
10 +from composio.utils.shared import json_schema_to_model
11
12
13 def test_true_becomes_empty_schema():
@@ -31,16 +38,159 @@ def test_combiner_drops_false_entries():
38 assert _filter_boolean_schemas(schema) == {"anyOf": [{"type": "string"}]}
39
40
34 -def test_combiner_removed_when_all_entries_false():
41 +def test_combiner_with_all_false_entries_rejects_every_value():
42 schema = {"type": "object", "anyOf": [False, False]}
36 - assert _filter_boolean_schemas(schema) == {"type": "object"}
43 + adapter = TypeAdapter(json_schema_to_pydantic_type(schema))
44 + for value in (None, "some string", 123, {}, []):
45 + with pytest.raises(ValidationError):
46 + adapter.validate_python(value)
47
48
39 -def test_false_property_is_dropped():
49 +def test_false_property_is_preserved_as_rejecting():
50 schema = {"properties": {"a": False, "b": {"type": "string"}}}
41 - assert _filter_boolean_schemas(schema) == {"properties": {"b": {"type": "string"}}}
51 + filtered = _filter_boolean_schemas(schema)
52 + assert "a" in filtered["properties"]
53 + assert filtered["properties"]["b"] == {"type": "string"}
54
55
56 def test_plain_keys_are_preserved():
57 schema = {"type": "string", "title": "X"}
58 assert _filter_boolean_schemas(schema) == {"type": "string", "title": "X"}
59 +
60 +
61 +@pytest.mark.schema
62 +def test_schema_converter_allof_with_false_member_rejects():
63 + """allOf: [false, X] must reject every value (false AND X == false)."""
64 + schema_with_false = {
65 + "allOf": [
66 + False,
67 + {"type": "object", "properties": {"a": {"type": "string"}}},
68 + ]
69 + }
70 +
71 + pytype = json_schema_to_pydantic_type(schema_with_false)
72 + adapter = TypeAdapter(pytype)
73 + assert adapter.json_schema() == {"not": {}}
74 + with pytest.raises(ValidationError):
75 + adapter.validate_python({"a": "hello"})
76 +
77 +
78 +@pytest.mark.schema
79 +def test_schema_converter_allof_with_false_member_rejects_non_dict_values_too():
80 + """The unsatisfiable-schema fallback must not just reject dicts by
81 + coincidence (str previously accepted any string). It should match the
82 + bare-`false`-schema convention used elsewhere in this module."""
83 + schema = {"allOf": [False, {"type": "string"}]}
84 + pytype = json_schema_to_pydantic_type(schema)
85 + adapter = TypeAdapter(pytype)
86 + for value in (None, "some string", 123, {"a": "hello"}):
87 + with pytest.raises(ValidationError):
88 + adapter.validate_python(value)
89 +
90 +
91 +@pytest.mark.schema
92 +def test_schema_converter_allof_without_false_member_still_validates():
93 + """Control: the same allOf schema minus the `false` member must still work."""
94 + schema_without_false = {
95 + "allOf": [
96 + {"type": "object", "properties": {"a": {"type": "string"}}},
97 + ]
98 + }
99 +
100 + pytype = json_schema_to_pydantic_type(schema_without_false)
101 + adapter = TypeAdapter(pytype)
102 + result = adapter.validate_python({"a": "hello"})
103 + assert result.a == "hello"
104 +
105 +
106 +@pytest.mark.schema
107 +def test_schema_converter_nested_unsatisfiable_allof_rejects():
108 + schema = {"allOf": [{"allOf": [False, {"type": "string"}]}]}
109 + adapter = TypeAdapter(json_schema_to_pydantic_type(schema))
110 +
111 + for value in (None, "some string", 123, {}, []):
112 + with pytest.raises(ValidationError):
113 + adapter.validate_python(value)
114 +
115 +
116 +@pytest.mark.schema
117 +def test_schema_converter_unsatisfiable_array_items_allow_only_empty_arrays():
118 + schema = {
119 + "type": "array",
120 + "items": {"allOf": [False, {"type": "string"}]},
121 + }
122 + adapter = TypeAdapter(json_schema_to_pydantic_type(schema))
123 +
124 + assert adapter.validate_python([]) == []
125 + with pytest.raises(ValidationError):
126 + adapter.validate_python(["some string"])
127 +
128 +
129 +@pytest.mark.schema
130 +def test_schema_converter_unsatisfiable_property_stays_rejecting():
131 + schema = {
132 + "type": "object",
133 + "properties": {
134 + "allowed": {"type": "string"},
135 + "impossible": {"allOf": [False, {"type": "string"}]},
136 + },
137 + "required": ["allowed"],
138 + }
139 + adapter = TypeAdapter(json_schema_to_pydantic_type(schema))
140 +
141 + result = adapter.validate_python({"allowed": "yes"})
142 + assert result.allowed == "yes"
143 + for impossible_value in (None, "some string", 123):
144 + with pytest.raises(ValidationError):
145 + adapter.validate_python({"allowed": "yes", "impossible": impossible_value})
146 +
147 +
148 +@pytest.mark.schema
149 +def test_schema_converter_unsatisfiable_definition_keeps_ref_rejecting():
150 + schema = {
151 + "$defs": {"Impossible": {"allOf": [False, {"type": "string"}]}},
152 + "$ref": "#/$defs/Impossible",
153 + }
154 + adapter = TypeAdapter(json_schema_to_pydantic_type(schema))
155 +
156 + for value in (None, "some string", 123, {}, []):
157 + with pytest.raises(ValidationError):
158 + adapter.validate_python(value)
159 +
160 +
161 +@pytest.mark.schema
162 +def test_schema_converter_unsatisfiable_property_works_in_shared_model():
163 + schema = {
164 + "type": "object",
165 + "title": "ToolArguments",
166 + "properties": {
167 + "required_value": {"type": "string"},
168 + "impossible": {"allOf": [False, {"type": "string"}]},
169 + },
170 + "required": ["required_value"],
171 + }
172 + model = json_schema_to_model(schema)
173 +
174 + assert model(required_value="yes").required_value == "yes"
175 + for impossible_value in (None, "some string", 123):
176 + with pytest.raises(ValidationError):
177 + model(required_value="yes", impossible=impossible_value)
178 +
179 +
180 +@pytest.mark.schema
181 +def test_schema_converter_required_unsatisfiable_property_has_no_valid_value():
182 + schema = {
183 + "type": "object",
184 + "title": "RequiredImpossibleArgument",
185 + "properties": {
186 + "impossible": {"allOf": [False, {"type": "string"}]},
187 + },
188 + "required": ["impossible"],
189 + }
190 + model = json_schema_to_model(schema)
191 +
192 + with pytest.raises(ValidationError):
193 + model()
194 + for impossible_value in (None, "some string", 123):
195 + with pytest.raises(ValidationError):
196 + model(impossible=impossible_value)
python/tests/test_schema_parser.py
+8 -6
@@ -8,7 +8,7 @@ particularly focusing on the required field propagation bug that was fixed.
8 import typing as t
9
10 import pytest
11 -from pydantic import BaseModel, ValidationError
11 +from pydantic import BaseModel, TypeAdapter, ValidationError
12 from pydantic.fields import PydanticUndefined
13
14 from composio.utils.shared import (
@@ -702,10 +702,10 @@ class TestJsonSchemaToPydanticType:
702 )
703
704 def test_allof_empty_options(self):
705 - """Test allOf with empty options falls back to string."""
705 + """Test allOf with empty options accepts any value."""
706 json_schema = {"allOf": []}
707 result = json_schema_to_pydantic_type(json_schema)
708 - assert result is str
708 + assert result is t.Any
709
710
711 class TestJsonSchemaToFieldsDict:
@@ -1127,7 +1127,7 @@ class TestBooleanSchemas:
1127 @pytest.mark.unit
1128 @pytest.mark.schema
1129 def test_only_false_schemas_in_anyof(self):
1130 - """Test anyOf with only false schemas falls back to string."""
1130 + """Test anyOf with only false schemas rejects every value."""
1131 json_schema = {
1132 "anyOf": [
1133 False,
@@ -1135,8 +1135,10 @@ class TestBooleanSchemas:
1135 ]
1136 }
1137 result = json_schema_to_pydantic_type(json_schema)
1138 - # All false schemas filtered out, should fall back to string
1139 - assert result is str
1138 + adapter = TypeAdapter(result)
1139 + for value in (None, "some string", 123, {}, []):
1140 + with pytest.raises(ValidationError):
1141 + adapter.validate_python(value)
1142
1143 @pytest.mark.unit
1144 @pytest.mark.schema