Skip to content

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): walk only schema positions when checking dynamic refs

jkomyno1f941675951a
diff --git a/python/composio/utils/schema_converter.py b/python/composio/utils/schema_converter.py
index 294755b4f..b01e7c05d 100644
--- a/python/composio/utils/schema_converter.py
+++ b/python/composio/utils/schema_converter.py
@@ -335,12 +335,41 @@ def _resolve_local_json_pointer(
return current
+# Draft 7 keywords whose value is itself a schema. `items` and `dependencies`
+# are handled separately because they also accept non-schema forms.
+_SCHEMA_VALUED_KEYWORDS = frozenset(
+ {
+ "additionalItems",
+ "additionalProperties",
+ "contains",
+ "else",
+ "if",
+ "not",
+ "propertyNames",
+ "then",
+ }
+)
+
+# Draft 7 keywords whose value is a list of schemas.
+_SCHEMA_LIST_KEYWORDS = frozenset({"allOf", "anyOf", "oneOf"})
+
+# Draft 7 keywords whose value maps names to schemas.
+_SCHEMA_MAP_KEYWORDS = frozenset(
+ {"$defs", "definitions", "patternProperties", "properties"}
+)
+
+
def _check_dynamic_references(
schema: t.Any,
root_schema: t.Dict[str, t.Any],
checked_references: t.Optional[t.Set[str]] = None,
) -> None:
- """Reject external, anchored, and unresolved references before validation."""
+ """Reject external, anchored, and unresolved references before validation.
+
+ Only schema positions are walked. ``const``, ``default``, ``enum``, and
+ ``examples`` hold instance data, so a ``$ref``-shaped value stored there is
+ a payload rather than a reference and must not block tool wrapping.
+ """
if checked_references is None:
checked_references = set()
if isinstance(schema, list):
@@ -363,8 +392,24 @@ def _check_dynamic_references(
checked_references.add(reference)
_check_dynamic_references(resolved, root_schema, checked_references)
- for value in schema.values():
- _check_dynamic_references(value, root_schema, checked_references)
+ for keyword, value in schema.items():
+ if keyword in _SCHEMA_VALUED_KEYWORDS or keyword in _SCHEMA_LIST_KEYWORDS:
+ _check_dynamic_references(value, root_schema, checked_references)
+ elif keyword in _SCHEMA_MAP_KEYWORDS:
+ if isinstance(value, dict):
+ for entry in value.values():
+ _check_dynamic_references(entry, root_schema, checked_references)
+ elif keyword == "items":
+ # A schema, or a list of schemas for tuple validation.
+ _check_dynamic_references(value, root_schema, checked_references)
+ elif keyword == "dependencies":
+ # Each entry is either a schema or a list of required property names.
+ if isinstance(value, dict):
+ for entry in value.values():
+ if isinstance(entry, (dict, bool)):
+ _check_dynamic_references(
+ entry, root_schema, checked_references
+ )
def _dynamic_key_validator(
diff --git a/python/tests/test_schema_parser.py b/python/tests/test_schema_parser.py
index 7775fdf41..d58c676b6 100644
--- a/python/tests/test_schema_parser.py
+++ b/python/tests/test_schema_parser.py
@@ -1865,3 +1865,63 @@ class TestSharedObjectCorpusThroughJsonSchemaToModel:
with pytest.raises(ValueError, match="must be a local JSON Pointer"):
json_schema_to_model(schema)
+
+ @pytest.mark.parametrize(
+ "keyword,value,accepted",
+ [
+ (
+ "default",
+ {"$ref": "https://example.com/schema.json"},
+ {"$ref": "kept"},
+ ),
+ ("const", {"$ref": "other.json#/thing"}, {"$ref": "other.json#/thing"}),
+ ("enum", [{"$ref": "#/$defs/missing"}], {"$ref": "#/$defs/missing"}),
+ ("examples", [{"$ref": "#anchor"}], {"$ref": "kept"}),
+ ],
+ )
+ def test_dynamic_schema_ignores_reference_shaped_instance_data(
+ self,
+ keyword: str,
+ value: t.Any,
+ accepted: t.Dict[str, t.Any],
+ ) -> None:
+ """`$ref`-shaped payloads are data, not references, and must not raise."""
+ schema = {
+ "type": "object",
+ "patternProperties": {"^value_": {"type": "object", keyword: value}},
+ }
+
+ model = json_schema_to_model(schema)
+
+ assert model.model_validate({"value_a": accepted}).model_dump() == {
+ "value_a": accepted
+ }
+
+ @pytest.mark.parametrize(
+ "dynamic_schema",
+ [
+ {"type": "array", "items": {"$ref": "https://example.com/schema.json"}},
+ {"allOf": [{"$ref": "https://example.com/schema.json"}]},
+ {
+ "type": "object",
+ "properties": {"inner": {"$ref": "https://example.com/schema.json"}},
+ },
+ {
+ "type": "object",
+ "additionalProperties": {
+ "$ref": "https://example.com/schema.json",
+ },
+ },
+ ],
+ )
+ def test_dynamic_schema_checks_references_in_nested_schema_positions(
+ self,
+ dynamic_schema: t.Dict[str, t.Any],
+ ) -> None:
+ schema = {
+ "type": "object",
+ "patternProperties": {"^value_": dynamic_schema},
+ }
+
+ with pytest.raises(ValueError, match="must be a local JSON Pointer"):
+ json_schema_to_model(schema)