@devries-cameron20 / composio-1 / commits / 4f009baa6
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(python): resolve nested dynamic refs from document root

jkomyno committed Aug 7, 2026 at 19:11 UTC 4f009baa699d7210bb47c4461841346017ba8d65
3 files changed +91 -19
python/composio/utils/schema_converter.py
+62 -18
@@ -379,7 +379,7 @@ def _dynamic_key_validator(
379
380 materializer = None
381 if _contains_default(schema):
382 - annotation = json_schema_to_pydantic_type(schema)
382 + annotation = json_schema_to_pydantic_type(schema, root_schema=root_schema)
383 materializer = TypeAdapter(annotation)
384
385 return _DynamicKeyValidator(
@@ -388,14 +388,18 @@ def _dynamic_key_validator(
388 )
389
390
391 -def _compile_object_policy(schema: t.Dict[str, t.Any]) -> _ObjectPolicy:
391 +def _compile_object_policy(
392 + schema: t.Dict[str, t.Any],
393 + root_schema: t.Optional[t.Dict[str, t.Any]] = None,
394 +) -> _ObjectPolicy:
395 + document_root = root_schema if root_schema is not None else schema
396 declared = frozenset(schema.get("properties") or {})
397 additional = schema.get("additionalProperties", _MISSING)
398 validator_type = jsonschema_validators.validator_for(
395 - schema,
399 + document_root,
400 default=jsonschema_validators.Draft7Validator,
401 )
398 - root_validator = validator_type(schema)
402 + root_validator = validator_type(document_root)
403
404 patterns = []
405 for pattern, pattern_schema in (schema.get("patternProperties") or {}).items():
@@ -403,7 +407,11 @@ def _compile_object_policy(schema: t.Dict[str, t.Any]) -> _ObjectPolicy:
407 patterns.append(
408 (
409 re.compile(pattern),
406 - _dynamic_key_validator(pattern_schema, schema, root_validator),
410 + _dynamic_key_validator(
411 + pattern_schema,
412 + document_root,
413 + root_validator,
414 + ),
415 )
416 )
417 except re.error as exc:
@@ -415,7 +423,7 @@ def _compile_object_policy(schema: t.Dict[str, t.Any]) -> _ObjectPolicy:
423 declared=declared,
424 patterns=tuple(patterns),
425 additional=(
418 - _dynamic_key_validator(additional, schema, root_validator)
426 + _dynamic_key_validator(additional, document_root, root_validator)
427 if isinstance(additional, dict)
428 else None
429 ),
@@ -500,9 +508,11 @@ def apply_object_policy(
508 schema: t.Dict[str, t.Any],
509 base_model: t.Type[BaseModel],
510 model_name: t.Optional[str] = None,
511 + *,
512 + root_schema: t.Optional[t.Dict[str, t.Any]] = None,
513 ) -> t.Type[BaseModel]:
514 """Wrap ``base_model`` with the covered dynamic-key policy for ``schema``."""
505 - policy = _compile_object_policy(schema)
515 + policy = _compile_object_policy(schema, root_schema)
516 name = model_name or base_model.__name__
517 json_schema_extra: t.Dict[str, t.Any] = {}
518 if "patternProperties" in schema:
@@ -532,6 +542,8 @@ def apply_object_policy(
542
543 def json_schema_to_pydantic_type(
544 json_schema: t.Union[t.Dict[str, t.Any], bool],
545 + *,
546 + root_schema: t.Optional[t.Dict[str, t.Any]] = None,
547 ) -> t.Union[t.Type, t.Optional[t.Any]]:
548 """
549 Converts a JSON schema type to a Pydantic type.
@@ -540,6 +552,7 @@ def json_schema_to_pydantic_type(
552 falls back to simple type mapping for primitive types.
553
554 :param json_schema: The JSON schema to convert (can be dict or boolean).
555 + :param root_schema: Full schema document used to resolve nested local references.
556 :return: A Pydantic type.
557 """
558 # Handle boolean schemas (JSON Schema draft-06+)
@@ -552,10 +565,18 @@ def json_schema_to_pydantic_type(
565 # Pre-filter boolean schemas from combiners
566 filtered_schema = _filter_boolean_schemas(json_schema)
567 filtered_schema = _resolve_unsatisfiable_references(filtered_schema)
555 - return _filtered_schema_to_pydantic_type(filtered_schema)
568 + document_root = root_schema if root_schema is not None else json_schema
569 + return _filtered_schema_to_pydantic_type(
570 + filtered_schema,
571 + root_schema=document_root,
572 + )
573
574
558 -def _filtered_schema_to_pydantic_type(schema: t.Any) -> t.Type[t.Any]:
575 +def _filtered_schema_to_pydantic_type(
576 + schema: t.Any,
577 + *,
578 + root_schema: t.Optional[t.Dict[str, t.Any]] = None,
579 +) -> t.Type[t.Any]:
580 """Convert a schema after boolean-schema normalization."""
581 if schema is None or _is_unsatisfiable_schema(schema):
582 return _UnsatisfiableSchema
@@ -565,7 +586,7 @@ def _filtered_schema_to_pydantic_type(schema: t.Any) -> t.Type[t.Any]:
586 return _convert_simple_type(schema)
587
588 # Use library for complex schemas (anyOf, allOf, oneOf, nested objects)
568 - return _convert_with_library(schema)
589 + return _convert_with_library(schema, root_schema=root_schema)
590
591
592 def _is_simple_primitive(schema: t.Dict[str, t.Any]) -> bool:
@@ -590,6 +611,8 @@ def _convert_simple_type(schema: t.Dict[str, t.Any]) -> t.Type[t.Any]:
611
612 def _convert_object_with_unsatisfiable_properties(
613 schema: t.Dict[str, t.Any],
614 + *,
615 + root_schema: t.Optional[t.Dict[str, t.Any]] = None,
616 ) -> t.Type[t.Any]:
617 """Build an object model while retaining always-rejecting properties."""
618 properties = schema.get("properties", {})
@@ -622,7 +645,10 @@ def _convert_object_with_unsatisfiable_properties(
645
646 field_definitions = {}
647 for name, prop_schema in rejecting_properties.items():
625 - annotation = _filtered_schema_to_pydantic_type(prop_schema)
648 + annotation = _filtered_schema_to_pydantic_type(
649 + prop_schema,
650 + root_schema=root_schema,
651 + )
652 default = (
653 ...
654 if name in required
@@ -649,6 +675,8 @@ def _convert_object_with_unsatisfiable_properties(
675
676 def _convert_with_library(
677 schema: t.Dict[str, t.Any],
678 + *,
679 + root_schema: t.Optional[t.Dict[str, t.Any]] = None,
680 ) -> t.Union[t.Type, t.Any]:
681 """Use json-schema-to-pydantic for complex schema conversion."""
682 try:
@@ -657,12 +685,15 @@ def _convert_with_library(
685 any(k in schema for k in ("anyOf", "allOf", "oneOf"))
686 and "type" not in schema
687 ):
660 - return _handle_toplevel_combiner(schema)
688 + return _handle_toplevel_combiner(schema, root_schema=root_schema)
689
690 # For object schemas, create model directly
691 if schema.get("type") == "object":
692 if _contains_unsatisfiable_schema(schema.get("properties", {})):
665 - return _convert_object_with_unsatisfiable_properties(schema)
693 + return _convert_object_with_unsatisfiable_properties(
694 + schema,
695 + root_schema=root_schema,
696 + )
697 # A property-less object with no dynamic-key constraints accepts and
698 # preserves arbitrary content. Modelling it as an empty Pydantic
699 # model instead silently discarded every key (issue #4064).
@@ -677,6 +708,7 @@ def _convert_with_library(
708 allow_undefined_array_items=True,
709 allow_undefined_type=True,
710 ),
711 + root_schema=root_schema,
712 )
713
714 # For array schemas
@@ -685,7 +717,10 @@ def _convert_with_library(
717 if _is_unsatisfiable_schema(items):
718 return t.List[_UnsatisfiableSchema] # type: ignore[return-value]
719 if items:
688 - item_type = _filtered_schema_to_pydantic_type(items)
720 + item_type = _filtered_schema_to_pydantic_type(
721 + items,
722 + root_schema=root_schema,
723 + )
724 return t.List[t.cast(t.Type, item_type)] # type: ignore
725 return t.List
726
@@ -704,6 +739,8 @@ def _convert_with_library(
739
740 def _handle_toplevel_combiner(
741 schema: t.Dict[str, t.Any],
742 + *,
743 + root_schema: t.Optional[t.Dict[str, t.Any]] = None,
744 ) -> t.Union[t.Type, t.Any]:
745 """
746 Handle top-level combiner schemas (anyOf, allOf, oneOf without "type").
@@ -730,23 +767,30 @@ def _handle_toplevel_combiner(
767 # Fallback: manually build union type for anyOf/oneOf
768 if "anyOf" in schema or "oneOf" in schema:
769 options = schema.get("anyOf", schema.get("oneOf", []))
733 - return _build_union_from_options(options)
770 + return _build_union_from_options(options, root_schema=root_schema)
771
772 # Fallback: use first option for allOf
773 if "allOf" in schema and schema["allOf"]:
737 - return json_schema_to_pydantic_type(schema["allOf"][0])
774 + return json_schema_to_pydantic_type(
775 + schema["allOf"][0],
776 + root_schema=root_schema,
777 + )
778
779 return t.Any
780
781
742 -def _build_union_from_options(options: t.List[t.Dict[str, t.Any]]) -> t.Type:
782 +def _build_union_from_options(
783 + options: t.List[t.Dict[str, t.Any]],
784 + *,
785 + root_schema: t.Optional[t.Dict[str, t.Any]] = None,
786 +) -> t.Type:
787 """Build a Union type from a list of schema options."""
788 pydantic_types = []
789 null_type = PYDANTIC_TYPE_TO_PYTHON_TYPE.get("null")
790 has_null = False
791
792 for option in options:
749 - ptype = json_schema_to_pydantic_type(option)
793 + ptype = json_schema_to_pydantic_type(option, root_schema=root_schema)
794 if ptype is None:
795 continue
796 if ptype == null_type or ptype is type(None):
python/composio/utils/shared.py
+9 -1
@@ -359,6 +359,8 @@ def json_schema_to_pydantic_field(
359 json_schema: t.Dict[str, t.Any],
360 required: t.List[str],
361 skip_default: bool = False,
362 + *,
363 + root_schema: t.Optional[t.Dict[str, t.Any]] = None,
364 ) -> t.Tuple[str, t.Type, FieldInfo]:
365 """
366 Converts a JSON schema property to a Pydantic field definition.
@@ -366,6 +368,7 @@ def json_schema_to_pydantic_field(
368 :param name: The field name.
369 :param json_schema: The JSON schema property.
370 :param required: List of required properties.
371 + :param root_schema: Full schema document used to resolve local references.
372 :return: A Pydantic field definition.
373 """
374 description = json_schema.get("description")
@@ -404,6 +407,7 @@ def json_schema_to_pydantic_field(
407 t.Type,
408 json_schema_to_pydantic_type(
409 json_schema=json_schema,
410 + root_schema=root_schema,
411 ),
412 ),
413 Field(**field), # type: ignore
@@ -429,7 +433,10 @@ def json_schema_to_fields_dict(json_schema: t.Dict[str, t.Any]) -> t.Dict[str, t
433 field_definitions = {}
434 for name, prop in json_schema.get("properties", {}).items():
435 updated_name, pydantic_type, pydantic_field = json_schema_to_pydantic_field(
432 - name, prop, json_schema.get("required", [])
436 + name,
437 + prop,
438 + json_schema.get("required", []),
439 + root_schema=json_schema,
440 )
441 field_definitions[updated_name] = (pydantic_type, pydantic_field)
442 return field_definitions # type: ignore
@@ -456,6 +463,7 @@ def json_schema_to_model(
463 prop,
464 json_schema.get("required", []),
465 skip_default=skip_default,
466 + root_schema=json_schema,
467 )
468 field_definitions[updated_name] = (pydantic_type, pydantic_field)
469 # The dynamic-key policy is shared with `json_schema_to_pydantic_type` so the
python/tests/test_schema_parser.py
+20
@@ -1802,6 +1802,26 @@ class TestSharedObjectCorpusThroughJsonSchemaToModel:
1802 with pytest.raises(ValidationError):
1803 model.model_validate({"count_a": 0})
1804
1805 + def test_nested_dynamic_schema_resolves_against_document_root(self) -> None:
1806 + schema = {
1807 + "$defs": {"positive": {"type": "integer", "minimum": 1}},
1808 + "type": "object",
1809 + "properties": {
1810 + "payload": {
1811 + "type": "object",
1812 + "patternProperties": {"^count_": {"$ref": "#/$defs/positive"}},
1813 + "additionalProperties": False,
1814 + }
1815 + },
1816 + "required": ["payload"],
1817 + }
1818 + model = json_schema_to_model(schema)
1819 +
1820 + result = model.model_validate({"payload": {"count_a": 1}})
1821 + assert result.model_dump() == {"payload": {"count_a": 1}}
1822 + with pytest.raises(ValidationError):
1823 + model.model_validate({"payload": {"count_a": 0}})
1824 +
1825 def test_dynamic_schema_materialization_cannot_reject_valid_input(self) -> None:
1826 schema = {
1827 "type": "object",