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): preserve nested provider argument presence
jkomyno committed
Aug 10, 2026 at 21:42 UTC
64fbc4ee039330ecb07c36a732ba96119b71e2ec
4 files changed
+741
-22
python/composio/utils/schema_converter.py
+233
-2
@@ -10,6 +10,7 @@ We handle those by pre-filtering them before passing to the library.
10
"""
11
12
import re
13
+import types
14
import typing as t
15
from functools import reduce
16
from urllib.parse import unquote
@@ -40,6 +41,7 @@ from composio.utils.logging import get as get_logger
41
logger = get_logger(__name__)
42
43
_MISSING = object()
44
+_EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE = "__composio_explicit_default_fields__"
45
46
47
class _UnsatisfiableSchema:
@@ -253,8 +255,8 @@ class _DynamicKeyValidator(t.NamedTuple):
255
if self.materializer is None:
256
return value
257
try:
256
- return self.materializer.dump_python(
257
- self.materializer.validate_python(value)
258
+ return _materialized_value_to_python(
259
+ self.materializer.validate_python(_materialized_value_to_python(value))
260
)
261
except (TypeError, ValueError):
262
# JSON Schema alone decides acceptance. Pydantic is only retained
@@ -264,6 +266,30 @@ class _DynamicKeyValidator(t.NamedTuple):
266
return value
267
268
269
+def _materialized_value_to_python(value: t.Any) -> t.Any:
270
+ """Convert a previous materializer result back into validation input."""
271
+ if isinstance(value, BaseModel):
272
+ included_fields = set(value.model_fields_set)
273
+ included_fields.update(
274
+ getattr(type(value), _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE, ())
275
+ )
276
+ result = {}
277
+ for name, field in type(value).model_fields.items():
278
+ if name not in included_fields:
279
+ continue
280
+ alias = field.serialization_alias or field.alias
281
+ output_name = alias if isinstance(alias, str) else name
282
+ result[output_name] = _materialized_value_to_python(getattr(value, name))
283
+ for name, item in (value.model_extra or {}).items():
284
+ result[name] = _materialized_value_to_python(item)
285
+ return result
286
+ if isinstance(value, dict):
287
+ return {key: _materialized_value_to_python(item) for key, item in value.items()}
288
+ if isinstance(value, (list, tuple)):
289
+ return [_materialized_value_to_python(item) for item in value]
290
+ return value
291
+
292
+
293
class _ObjectPolicy(t.NamedTuple):
294
"""Compiled dynamic-key policy for one JSON Schema object node.
295
@@ -335,6 +361,210 @@ def _resolve_local_json_pointer(
361
return current
362
363
364
+def _resolve_default_metadata_schema(
365
+ schema: t.Any,
366
+ root_schema: t.Dict[str, t.Any],
367
+ visited_refs: t.Optional[t.Set[str]] = None,
368
+) -> t.Any:
369
+ """Resolve a schema node without traversing instance-valued annotations."""
370
+ if not isinstance(schema, dict):
371
+ return schema
372
+ reference = schema.get("$ref")
373
+ if not isinstance(reference, str) or not reference.startswith("#/"):
374
+ return schema
375
+ if visited_refs is None:
376
+ visited_refs = set()
377
+ if reference in visited_refs:
378
+ return {key: value for key, value in schema.items() if key != "$ref"}
379
+ visited_refs.add(reference)
380
+
381
+ target = _resolve_local_json_pointer(reference, root_schema)
382
+ if not isinstance(target, dict):
383
+ return schema
384
+ resolved = _resolve_default_metadata_schema(target, root_schema, visited_refs)
385
+ if not isinstance(resolved, dict):
386
+ return schema
387
+ return {
388
+ **resolved,
389
+ **{key: value for key, value in schema.items() if key != "$ref"},
390
+ }
391
+
392
+
393
+def _default_metadata_properties(
394
+ schema: t.Any,
395
+ root_schema: t.Dict[str, t.Any],
396
+) -> t.Dict[str, t.Any]:
397
+ schema = _resolve_default_metadata_schema(schema, root_schema)
398
+ if not isinstance(schema, dict) or "anyOf" in schema or "oneOf" in schema:
399
+ return {}
400
+ properties = schema.get("properties")
401
+ result = dict(properties) if isinstance(properties, dict) else {}
402
+ for option in schema.get("allOf", []):
403
+ result.update(_default_metadata_properties(option, root_schema))
404
+ return result
405
+
406
+
407
+def _default_metadata_schema_for_model(
408
+ schema: t.Any,
409
+ model: t.Type[BaseModel],
410
+ root_schema: t.Dict[str, t.Any],
411
+) -> t.Any:
412
+ schema = _resolve_default_metadata_schema(schema, root_schema)
413
+ if not isinstance(schema, dict):
414
+ return None
415
+ for combiner in ("anyOf", "oneOf"):
416
+ titled = [
417
+ option
418
+ for option in schema.get(combiner, [])
419
+ if isinstance(option, dict) and option.get("title") == model.__name__
420
+ ]
421
+ if len(titled) == 1:
422
+ return titled[0]
423
+ return schema if "anyOf" not in schema and "oneOf" not in schema else None
424
+
425
+
426
+def _default_metadata_field_aliases(
427
+ model: t.Type[BaseModel],
428
+) -> t.Dict[str, t.Tuple[str, t.Any]]:
429
+ fields = {}
430
+ for name, field in model.model_fields.items():
431
+ alias = field.serialization_alias or field.alias
432
+ fields[alias if isinstance(alias, str) else name] = (name, field)
433
+ return fields
434
+
435
+
436
+def _mark_explicit_default_fields(
437
+ annotation: t.Any,
438
+ schema: t.Any,
439
+ root_schema: t.Dict[str, t.Any],
440
+ visiting: t.Optional[t.Set[t.Type[BaseModel]]] = None,
441
+) -> None:
442
+ """Attach recursive default-presence metadata to generated model classes."""
443
+ origin = t.get_origin(annotation)
444
+ arguments = t.get_args(annotation)
445
+ if origin in (t.Union, types.UnionType):
446
+ options: t.List[t.Dict[str, t.Any]] = []
447
+ resolved_schema = _resolve_default_metadata_schema(schema, root_schema)
448
+ if isinstance(resolved_schema, dict):
449
+ for combiner in ("anyOf", "oneOf"):
450
+ options.extend(
451
+ option
452
+ for option in resolved_schema.get(combiner, [])
453
+ if isinstance(option, dict)
454
+ )
455
+ for index, argument in enumerate(arguments):
456
+ argument_schema = None
457
+ if isinstance(argument, type) and issubclass(argument, BaseModel):
458
+ titled = [
459
+ option
460
+ for option in options
461
+ if option.get("title") == argument.__name__
462
+ ]
463
+ if len(titled) == 1:
464
+ argument_schema = titled[0]
465
+ if argument_schema is None and index < len(options):
466
+ argument_schema = options[index]
467
+ _mark_explicit_default_fields(
468
+ argument,
469
+ argument_schema,
470
+ root_schema,
471
+ visiting,
472
+ )
473
+ return
474
+
475
+ if origin in (list, set, frozenset):
476
+ resolved_schema = _resolve_default_metadata_schema(schema, root_schema)
477
+ item_schema = (
478
+ resolved_schema.get("items") if isinstance(resolved_schema, dict) else None
479
+ )
480
+ if arguments:
481
+ _mark_explicit_default_fields(
482
+ arguments[0],
483
+ item_schema,
484
+ root_schema,
485
+ visiting,
486
+ )
487
+ return
488
+
489
+ if origin is tuple:
490
+ resolved_schema = _resolve_default_metadata_schema(schema, root_schema)
491
+ items = (
492
+ resolved_schema.get("items") if isinstance(resolved_schema, dict) else None
493
+ )
494
+ for index, argument in enumerate(arguments):
495
+ item_schema = (
496
+ items[index]
497
+ if isinstance(items, list) and index < len(items)
498
+ else items
499
+ if isinstance(items, dict)
500
+ else None
501
+ )
502
+ _mark_explicit_default_fields(
503
+ argument,
504
+ item_schema,
505
+ root_schema,
506
+ visiting,
507
+ )
508
+ return
509
+
510
+ if origin is dict:
511
+ resolved_schema = _resolve_default_metadata_schema(schema, root_schema)
512
+ additional = (
513
+ resolved_schema.get("additionalProperties")
514
+ if isinstance(resolved_schema, dict)
515
+ else None
516
+ )
517
+ if len(arguments) == 2:
518
+ _mark_explicit_default_fields(
519
+ arguments[1],
520
+ additional,
521
+ root_schema,
522
+ visiting,
523
+ )
524
+ return
525
+
526
+ if not isinstance(annotation, type) or not issubclass(annotation, BaseModel):
527
+ return
528
+
529
+ model_schema = _default_metadata_schema_for_model(
530
+ schema,
531
+ annotation,
532
+ root_schema,
533
+ )
534
+ properties = _default_metadata_properties(model_schema, root_schema)
535
+ fields = _default_metadata_field_aliases(annotation)
536
+ explicit_defaults = {
537
+ internal_name
538
+ for alias, (internal_name, field) in fields.items()
539
+ if (not field.is_required() and field.default is not None)
540
+ or (isinstance(properties.get(alias), dict) and "default" in properties[alias])
541
+ }
542
+ explicit_defaults.update(
543
+ getattr(annotation, _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE, ())
544
+ )
545
+ setattr(
546
+ annotation,
547
+ _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE,
548
+ frozenset(explicit_defaults),
549
+ )
550
+
551
+ if visiting is None:
552
+ visiting = set()
553
+ if annotation in visiting:
554
+ return
555
+ visiting.add(annotation)
556
+ try:
557
+ for alias, (_, field) in fields.items():
558
+ _mark_explicit_default_fields(
559
+ field.annotation,
560
+ properties.get(alias),
561
+ root_schema,
562
+ visiting,
563
+ )
564
+ finally:
565
+ visiting.remove(annotation)
566
+
567
+
568
# Draft 7 keywords whose value is itself a schema. `items` and `dependencies`
569
# are handled separately because they also accept non-schema forms.
570
_SCHEMA_VALUED_KEYWORDS = frozenset(
@@ -434,6 +664,7 @@ def _dynamic_key_validator(
664
materializer = None
665
if _contains_default(schema):
666
annotation = json_schema_to_pydantic_type(schema, root_schema=root_schema)
667
+ _mark_explicit_default_fields(annotation, schema, root_schema)
668
materializer = TypeAdapter(annotation)
669
670
return _DynamicKeyValidator(
python/composio/utils/shared.py
+101
-20
@@ -22,6 +22,8 @@ from composio.utils.schema_converter import (
22
CONTAINER_TYPE,
23
FALLBACK_VALUES,
24
PYDANTIC_TYPE_TO_PYTHON_TYPE,
25
+ _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE,
26
+ _mark_explicit_default_fields,
27
apply_object_policy,
28
json_schema_to_pydantic_type,
29
)
@@ -55,7 +57,7 @@ reserved_names = ["validate"]
57
_OBJ_MARKER = "-_object_-"
58
_ARR_MARKER = "-_array_-"
59
_MAX_PROVIDER_ALIAS_LENGTH = 64
58
-_EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE = "__composio_explicit_default_fields__"
60
+_MISSING_ARGUMENT = object()
61
62
63
def normalize_tool_arguments(arguments: t.Any) -> t.Dict[str, t.Any]:
@@ -113,25 +115,108 @@ def validate_and_serialize_tool_arguments(
115
Pydantic models use ``None`` for optional fields that were not supplied, so a
116
plain ``model_dump()`` changes omission into an explicit null. Conversely,
117
``exclude_unset=True`` also drops defaults declared by the source JSON Schema.
116
- Track those defaults on the generated model and combine them with
117
- ``model_fields_set`` so aliases, explicit nulls, defaults, and dynamic extras
118
- all survive without padding the payload.
118
+ Generated models track fields with declared defaults. Combining that metadata
119
+ recursively with each selected model's ``model_fields_set`` preserves aliases,
120
+ explicit nulls, defaults, and validated dynamic extras without replaying the
121
+ source schema during execution.
122
"""
123
validated = args_schema.model_validate(arguments)
121
- included_fields = set(validated.model_fields_set)
122
- included_fields.update(
123
- getattr(type(validated), _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE, ())
124
- )
124
return t.cast(
125
t.Dict[str, t.Any],
127
- validated.model_dump(
128
- mode="python",
129
- by_alias=True,
130
- include=included_fields,
126
+ _serialize_present_arguments(
127
+ validated,
128
+ validated.model_dump(mode="python", by_alias=True),
129
+ arguments,
130
),
131
)
132
133
134
+def _model_field_aliases(
135
+ model: t.Type[BaseModel],
136
+) -> t.Dict[str, t.Tuple[str, FieldInfo]]:
137
+ fields = {}
138
+ for name, field in model.model_fields.items():
139
+ alias = field.serialization_alias or field.alias
140
+ fields[alias if isinstance(alias, str) else name] = (name, field)
141
+ return fields
142
+
143
+
144
+def _serialize_present_arguments(
145
+ validated: t.Any,
146
+ serialized: t.Any,
147
+ supplied: t.Any,
148
+) -> t.Any:
149
+ """Recursively remove values Pydantic added for omitted optional fields."""
150
+ if supplied is _MISSING_ARGUMENT:
151
+ return serialized
152
+
153
+ if isinstance(validated, BaseModel) and isinstance(serialized, dict):
154
+ fields = _model_field_aliases(type(validated))
155
+ included_fields = set(validated.model_fields_set)
156
+ included_fields.update(
157
+ getattr(type(validated), _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE, ())
158
+ )
159
+ supplied_values = supplied if isinstance(supplied, dict) else {}
160
+ result: t.Dict[str, t.Any] = {}
161
+ for key, value in serialized.items():
162
+ field = fields.get(key)
163
+ if field is not None:
164
+ name, _ = field
165
+ if name not in included_fields:
166
+ continue
167
+ child_validated = getattr(validated, name)
168
+ if key in supplied_values:
169
+ child_supplied = supplied_values[key]
170
+ elif name in supplied_values:
171
+ child_supplied = supplied_values[name]
172
+ else:
173
+ child_supplied = _MISSING_ARGUMENT
174
+ else:
175
+ extra = validated.model_extra
176
+ if extra is None or key not in extra:
177
+ continue
178
+ child_validated = extra[key]
179
+ child_supplied = supplied_values.get(key, _MISSING_ARGUMENT)
180
+ result[key] = _serialize_present_arguments(
181
+ child_validated,
182
+ value,
183
+ child_supplied,
184
+ )
185
+ return result
186
+
187
+ if isinstance(serialized, dict):
188
+ supplied_values = supplied if isinstance(supplied, dict) else {}
189
+ validated_values = validated if isinstance(validated, dict) else {}
190
+ return {
191
+ key: _serialize_present_arguments(
192
+ validated_values.get(key, _MISSING_ARGUMENT),
193
+ value,
194
+ supplied_values.get(key, _MISSING_ARGUMENT),
195
+ )
196
+ for key, value in serialized.items()
197
+ if key in supplied_values or key in validated_values
198
+ }
199
+
200
+ if isinstance(serialized, (list, tuple)):
201
+ supplied_items = supplied if isinstance(supplied, (list, tuple)) else ()
202
+ validated_items = validated if isinstance(validated, (list, tuple)) else ()
203
+ items = [
204
+ _serialize_present_arguments(
205
+ validated_items[index]
206
+ if index < len(validated_items)
207
+ else _MISSING_ARGUMENT,
208
+ value,
209
+ supplied_items[index]
210
+ if index < len(supplied_items)
211
+ else _MISSING_ARGUMENT,
212
+ )
213
+ for index, value in enumerate(serialized)
214
+ ]
215
+ return tuple(items) if isinstance(serialized, tuple) else items
216
+
217
+ return serialized
218
+
219
+
220
def _make_safe_name(name: str) -> str:
221
"""Append ``_rs`` to a Python keyword so it can be used as a parameter name."""
222
return f"{name}_rs"
@@ -487,7 +572,6 @@ def json_schema_to_model(
572
if model_name is None:
573
model_name = "GeneratedModel"
574
field_definitions = {}
490
- explicit_default_fields: t.Set[str] = set()
575
for name, prop in json_schema.get("properties", {}).items():
576
updated_name, pydantic_type, pydantic_field = json_schema_to_pydantic_field(
577
name,
@@ -497,16 +581,13 @@ def json_schema_to_model(
581
root_schema=json_schema,
582
)
583
field_definitions[updated_name] = (pydantic_type, pydantic_field)
500
- if not skip_default and "default" in prop:
501
- explicit_default_fields.add(updated_name)
584
# The dynamic-key policy is shared with `json_schema_to_pydantic_type` so the
585
# two entry points cannot disagree about which arguments survive conversion.
586
base_model = create_model(model_name, **field_definitions) # type: ignore
505
- setattr(
506
- base_model,
507
- _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE,
508
- frozenset(explicit_default_fields),
509
- )
587
+ if skip_default:
588
+ setattr(base_model, _EXPLICIT_DEFAULT_FIELDS_ATTRIBUTE, frozenset())
589
+ else:
590
+ _mark_explicit_default_fields(base_model, json_schema, json_schema)
591
return apply_object_policy(
592
json_schema,
593
base_model,
python/tests/test_provider_argument_serialization.py
+381
@@ -5,6 +5,10 @@ import typing as t
5
import pytest
6
7
from composio.client.types import Tool, tool_list_response
8
+from composio.utils.shared import (
9
+ json_schema_to_model,
10
+ validate_and_serialize_tool_arguments,
11
+)
12
13
14
PROVIDERS = ("crewai", "langchain", "langgraph")
@@ -98,6 +102,383 @@ def test_provider_serialization_preserves_argument_presence_and_aliases(name: st
102
]
103
104
105
+@pytest.mark.parametrize("name", PROVIDERS)
106
+def test_provider_serialization_preserves_nested_argument_presence(name: str):
107
+ wrapped, received = _wrap(
108
+ name,
109
+ {
110
+ "type": "object",
111
+ "title": "NestedArguments",
112
+ "properties": {
113
+ "payload": {
114
+ "type": "object",
115
+ "title": "Payload",
116
+ "properties": {
117
+ "query": {"type": "string"},
118
+ "note": {
119
+ "anyOf": [{"type": "string"}, {"type": "null"}],
120
+ },
121
+ "explicit_null": {
122
+ "anyOf": [{"type": "string"}, {"type": "null"}],
123
+ },
124
+ "null_default": {
125
+ "anyOf": [{"type": "string"}, {"type": "null"}],
126
+ "default": None,
127
+ },
128
+ "page": {"type": "integer", "default": 5},
129
+ "items": {
130
+ "type": "array",
131
+ "items": {
132
+ "type": "object",
133
+ "title": "Item",
134
+ "properties": {
135
+ "name": {"type": "string"},
136
+ "label": {
137
+ "anyOf": [
138
+ {"type": "string"},
139
+ {"type": "null"},
140
+ ],
141
+ },
142
+ "enabled": {
143
+ "type": "boolean",
144
+ "default": True,
145
+ },
146
+ },
147
+ "required": ["name"],
148
+ },
149
+ },
150
+ "mapping": {
151
+ "type": "object",
152
+ "additionalProperties": {
153
+ "type": "object",
154
+ "title": "Entry",
155
+ "properties": {
156
+ "name": {"type": "string"},
157
+ "label": {
158
+ "anyOf": [
159
+ {"type": "string"},
160
+ {"type": "null"},
161
+ ],
162
+ },
163
+ "null_default": {
164
+ "anyOf": [
165
+ {"type": "string"},
166
+ {"type": "null"},
167
+ ],
168
+ "default": None,
169
+ },
170
+ "config": {
171
+ "type": "object",
172
+ "properties": {
173
+ "implicit": {
174
+ "anyOf": [
175
+ {"type": "string"},
176
+ {"type": "null"},
177
+ ],
178
+ },
179
+ "null_default": {
180
+ "anyOf": [
181
+ {"type": "string"},
182
+ {"type": "null"},
183
+ ],
184
+ "default": None,
185
+ },
186
+ },
187
+ },
188
+ "score": {"type": "integer", "default": 2},
189
+ },
190
+ "required": ["name", "config"],
191
+ },
192
+ },
193
+ },
194
+ "required": ["query", "items", "mapping"],
195
+ },
196
+ },
197
+ "required": ["payload"],
198
+ },
199
+ )
200
+
201
+ _run(
202
+ name,
203
+ wrapped,
204
+ {
205
+ "payload": {
206
+ "query": "agents",
207
+ "explicit_null": None,
208
+ "items": [
209
+ {"name": "first"},
210
+ {"name": "second", "label": None},
211
+ ],
212
+ "mapping": {
213
+ "one": {"name": "one", "config": {}},
214
+ "two": {"name": "two", "label": None, "config": {}},
215
+ },
216
+ },
217
+ },
218
+ )
219
+
220
+ assert received == [
221
+ {
222
+ "payload": {
223
+ "query": "agents",
224
+ "explicit_null": None,
225
+ "null_default": None,
226
+ "page": 5,
227
+ "items": [
228
+ {"name": "first", "enabled": True},
229
+ {"name": "second", "label": None, "enabled": True},
230
+ ],
231
+ "mapping": {
232
+ "one": {
233
+ "name": "one",
234
+ "null_default": None,
235
+ "config": {"null_default": None},
236
+ "score": 2,
237
+ },
238
+ "two": {
239
+ "name": "two",
240
+ "label": None,
241
+ "null_default": None,
242
+ "config": {"null_default": None},
243
+ "score": 2,
244
+ },
245
+ },
246
+ },
247
+ }
248
+ ]
249
+
250
+
251
+@pytest.mark.parametrize("name", PROVIDERS)
252
+def test_provider_serialization_preserves_dynamic_array_item_defaults(name: str):
253
+ wrapped, received = _wrap(
254
+ name,
255
+ {
256
+ "type": "object",
257
+ "title": "DynamicArrayArguments",
258
+ "patternProperties": {
259
+ "^items_": {
260
+ "type": "array",
261
+ "items": {
262
+ "type": "object",
263
+ "properties": {
264
+ "implicit": {
265
+ "anyOf": [
266
+ {"type": "string"},
267
+ {"type": "null"},
268
+ ],
269
+ },
270
+ "null_default": {
271
+ "anyOf": [
272
+ {"type": "string"},
273
+ {"type": "null"},
274
+ ],
275
+ "default": None,
276
+ },
277
+ },
278
+ },
279
+ },
280
+ },
281
+ "additionalProperties": False,
282
+ },
283
+ )
284
+
285
+ _run(name, wrapped, {"items_a": [{}, {"implicit": None}]})
286
+
287
+ assert received == [
288
+ {
289
+ "items_a": [
290
+ {"null_default": None},
291
+ {"implicit": None, "null_default": None},
292
+ ]
293
+ }
294
+ ]
295
+
296
+
297
+@pytest.mark.parametrize("name", PROVIDERS)
298
+def test_provider_serialization_preserves_presence_across_overlapping_patterns(
299
+ name: str,
300
+):
301
+ object_schema = {
302
+ "type": "object",
303
+ "properties": {
304
+ "implicit": {
305
+ "anyOf": [{"type": "string"}, {"type": "null"}],
306
+ },
307
+ "defaulted": {"type": "integer", "default": 1},
308
+ },
309
+ }
310
+ wrapped, received = _wrap(
311
+ name,
312
+ {
313
+ "type": "object",
314
+ "title": "OverlappingPatternArguments",
315
+ "patternProperties": {
316
+ "^x": {**object_schema, "title": "First"},
317
+ "x$": {**object_schema, "title": "Second"},
318
+ },
319
+ "additionalProperties": False,
320
+ },
321
+ )
322
+
323
+ _run(name, wrapped, {"x": {}})
324
+
325
+ assert received == [{"x": {"defaulted": 1}}]
326
+
327
+
328
+@pytest.mark.parametrize("name", PROVIDERS)
329
+@pytest.mark.parametrize("combiner", ("anyOf", "oneOf"))
330
+def test_provider_serialization_uses_selected_composed_object_defaults(
331
+ name: str,
332
+ combiner: str,
333
+):
334
+ wrapped, received = _wrap(
335
+ name,
336
+ {
337
+ "type": "object",
338
+ "title": "ComposedArguments",
339
+ "properties": {
340
+ "payload": {
341
+ combiner: [
342
+ {
343
+ "type": "object",
344
+ "properties": {
345
+ "kind": {"type": "string", "enum": ["defaulted"]},
346
+ "value": {"type": "integer", "default": 5},
347
+ "config": {
348
+ "type": "object",
349
+ "properties": {
350
+ "nested": {
351
+ "type": "integer",
352
+ "default": 7,
353
+ },
354
+ },
355
+ },
356
+ },
357
+ "required": ["kind", "config"],
358
+ },
359
+ {
360
+ "type": "object",
361
+ "properties": {
362
+ "kind": {"type": "string", "enum": ["plain"]},
363
+ "value": {
364
+ "anyOf": [
365
+ {"type": "integer"},
366
+ {"type": "null"},
367
+ ],
368
+ },
369
+ "config": {
370
+ "type": "object",
371
+ "properties": {
372
+ "nested": {
373
+ "anyOf": [
374
+ {"type": "integer"},
375
+ {"type": "null"},
376
+ ],
377
+ },
378
+ },
379
+ },
380
+ },
381
+ "required": ["kind", "config"],
382
+ },
383
+ ],
384
+ },
385
+ },
386
+ "required": ["payload"],
387
+ },
388
+ )
389
+
390
+ _run(name, wrapped, {"payload": {"kind": "plain", "config": {}}})
391
+ _run(name, wrapped, {"payload": {"kind": "defaulted", "config": {}}})
392
+
393
+ assert received == [
394
+ {"payload": {"kind": "plain", "config": {}}},
395
+ {
396
+ "payload": {
397
+ "kind": "defaulted",
398
+ "value": 5,
399
+ "config": {"nested": 7},
400
+ }
401
+ },
402
+ ]
403
+
404
+
405
+@pytest.mark.parametrize("name", PROVIDERS)
406
+@pytest.mark.parametrize("combiner", ("anyOf", "oneOf"))
407
+def test_provider_serialization_preserves_selected_composed_null_defaults(
408
+ name: str,
409
+ combiner: str,
410
+):
411
+ nullable_string = {
412
+ "anyOf": [{"type": "string"}, {"type": "null"}],
413
+ }
414
+ wrapped, received = _wrap(
415
+ name,
416
+ {
417
+ "type": "object",
418
+ "title": "ComposedNullDefaultArguments",
419
+ "properties": {
420
+ "payload": {
421
+ combiner: [
422
+ {
423
+ "type": "object",
424
+ "properties": {
425
+ "kind": {"type": "string", "enum": ["defaulted"]},
426
+ "value": {**nullable_string, "default": None},
427
+ },
428
+ "required": ["kind"],
429
+ },
430
+ {
431
+ "type": "object",
432
+ "properties": {
433
+ "kind": {"type": "string", "enum": ["plain"]},
434
+ "value": nullable_string,
435
+ },
436
+ "required": ["kind"],
437
+ },
438
+ ],
439
+ },
440
+ },
441
+ "required": ["payload"],
442
+ },
443
+ )
444
+
445
+ _run(name, wrapped, {"payload": {"kind": "plain"}})
446
+ _run(name, wrapped, {"payload": {"kind": "defaulted"}})
447
+
448
+ assert received == [
449
+ {"payload": {"kind": "plain"}},
450
+ {"payload": {"kind": "defaulted", "value": None}},
451
+ ]
452
+
453
+
454
+def test_provider_serialization_preserves_ref_shaped_object_defaults():
455
+ schema = {
456
+ "$defs": {
457
+ "RefTarget": {
458
+ "type": "object",
459
+ "properties": {"value": {"type": "string"}},
460
+ },
461
+ },
462
+ "type": "object",
463
+ "title": "RefShapedDefaultArguments",
464
+ "properties": {
465
+ "payload": {
466
+ "type": "object",
467
+ "additionalProperties": True,
468
+ "default": {"$ref": "#/$defs/RefTarget"},
469
+ },
470
+ },
471
+ }
472
+ model = json_schema_to_model(schema)
473
+
474
+ assert model.model_validate({}).model_dump(mode="python", by_alias=True) == {
475
+ "payload": {"$ref": "#/$defs/RefTarget"}
476
+ }
477
+ assert validate_and_serialize_tool_arguments(model, {}) == {
478
+ "payload": {"$ref": "#/$defs/RefTarget"}
479
+ }
480
+
481
+
482
@pytest.mark.parametrize("name", PROVIDERS)
483
def test_provider_serialization_preserves_free_form_objects(name: str):
484
wrapped, received = _wrap(
python/tests/test_schema_converter.py
+26
@@ -225,6 +225,32 @@ def test_unsatisfiable_declared_property_keeps_root_dynamic_policy(converter):
225
adapter.validate_python({"impossible": None})
226
227
228
+def test_dynamic_default_materialization_preserves_plain_python_values():
229
+ model = json_schema_to_model(
230
+ {
231
+ "type": "object",
232
+ "patternProperties": {
233
+ "^item_": {
234
+ "type": "object",
235
+ "properties": {
236
+ "implicit": {
237
+ "anyOf": [{"type": "string"}, {"type": "null"}],
238
+ },
239
+ "defaulted": {"type": "integer", "default": 1},
240
+ },
241
+ },
242
+ },
243
+ "additionalProperties": False,
244
+ }
245
+ )
246
+
247
+ result = model.model_validate({"item_a": {}})
248
+
249
+ assert result.model_extra == {"item_a": {"defaulted": 1}}
250
+ assert isinstance(result.model_extra["item_a"], dict)
251
+ assert getattr(result, "item_a") == {"defaulted": 1}
252
+
253
+
254
@pytest.mark.parametrize(
255
"invalid_object_schema,error_type,error_match",
256
[