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, 21:00 UTC
fix: preserve nullability in null-only anyOf and fix validate param alias
Merge PR #3173 as the preferred fix for the null-only anyOf schema behavior and validate alias handling.
Yizuki_Ame committed
May 13, 2026 at 00:20 UTC
630038ab8396ac5c332ccdd392443e3fe5bc3d6d
3 files changed
+34
-4
python/composio/utils/schema_converter.py
+3
-1
@@ -234,6 +234,8 @@ def _handle_toplevel_combiner(
234
allow_undefined_array_items=True,
235
allow_undefined_type=True,
236
)
237
+ if result is type(None):
238
+ return t.Optional[t.Any]
239
# If result is a type (like a Union or Optional), return it directly
240
# If result is a model class, return it
241
return result
@@ -270,7 +272,7 @@ def _build_union_from_options(options: t.List[t.Dict[str, t.Any]]) -> t.Type:
272
pydantic_types.append(ptype)
273
274
if len(pydantic_types) == 0:
273
- return str # Fallback
275
+ return t.Optional[t.Any] if has_null else str
276
277
if len(pydantic_types) == 1:
278
base_type = pydantic_types[0]
python/composio/utils/shared.py
+3
-2
@@ -210,9 +210,10 @@ def json_schema_to_pydantic_field(
210
default = _coerce_default_value(default, json_schema)
211
212
# Check if the field name is a reserved Pydantic name
213
+ original_name = name
214
if name in reserved_names:
215
name = f"{name}_"
215
- alias = name
216
+ alias = original_name
217
else:
218
alias = None
219
@@ -222,7 +223,7 @@ def json_schema_to_pydantic_field(
223
"alias": alias,
224
}
225
if not skip_default:
225
- field["default"] = ... if name in required else default
226
+ field["default"] = ... if original_name in required else default
227
228
return (
229
name,
python/tests/test_schema_parser.py
+28
-1
@@ -121,7 +121,26 @@ class TestJsonSchemaToPydanticField:
121
)
122
123
assert field_name == "validate_" # Should be renamed
124
- assert field_info.alias == "validate_" # Should have alias
124
+ assert field_info.alias == "validate" # Should preserve original name
125
+
126
+ def test_reserved_required_field_name_preserves_required_status(self):
127
+ """Test that reserved field names keep the original alias and required status."""
128
+ name = "validate"
129
+ json_schema = {
130
+ "type": "string",
131
+ "description": "A required field with reserved name",
132
+ "title": "Validate",
133
+ }
134
+ required = ["validate"]
135
+
136
+ field_name, field_type, field_info = json_schema_to_pydantic_field(
137
+ name, json_schema, required
138
+ )
139
+
140
+ assert field_name == "validate_"
141
+ assert field_type is str
142
+ assert field_info.alias == "validate"
143
+ assert field_info.default is PydanticUndefined
144
145
def test_field_with_examples(self):
146
"""Test that examples are properly preserved in field info."""
@@ -394,6 +413,14 @@ class TestJsonSchemaToPydanticType:
413
result = json_schema_to_pydantic_type(json_schema)
414
assert result == expected_type
415
416
+ def test_anyof_null_only_preserves_nullability(self):
417
+ """Test that anyOf with only null maps to Optional[Any] instead of str."""
418
+ result = json_schema_to_pydantic_type({"anyOf": [{"type": "null"}]})
419
+
420
+ assert t.get_origin(result) is t.Union
421
+ assert t.Any in t.get_args(result)
422
+ assert type(None) in t.get_args(result)
423
+
424
def test_array_type(self):
425
"""Test array type conversion."""
426
json_schema = {"type": "array", "items": {"type": "string"}}