Skip to content

Build and run agents you can see, understand and trust.

fix(schema): decode escaped definition reference names (#3000)

cxyyy66c13f390852e0
diff --git a/src/agentscope/_utils/_common.py b/src/agentscope/_utils/_common.py
index 1f7586a11..6045c3902 100644
--- a/src/agentscope/_utils/_common.py
+++ b/src/agentscope/_utils/_common.py
@@ -384,6 +384,8 @@ def _flatten_json_schema(schema: dict) -> dict:
return obj
def_name = ref_path.split("/")[-1]
+ # JSON Pointer tokens decode ~1 before ~0 (RFC 6901).
+ def_name = def_name.replace("~1", "/").replace("~0", "~")
if def_name in visited:
logger.warning(
"Circular reference detected for '%s' in tool schema",
diff --git a/tests/model_gemini_test.py b/tests/model_gemini_test.py
index 4e7eae268..ad7947b45 100644
--- a/tests/model_gemini_test.py
+++ b/tests/model_gemini_test.py
@@ -1328,6 +1328,60 @@ class TestGeminiSchemaUtils(unittest.TestCase):
},
)
+ def test_flatten_escaped_definition_names(self) -> None:
+ """JSON Pointer escapes are decoded once and in the correct order."""
+ for keyword in ("$defs", "definitions"):
+ for name, token in (
+ ("a/b", "a~1b"),
+ ("a~b", "a~0b"),
+ ("a~1b", "a~01b"),
+ ):
+ with self.subTest(keyword=keyword, name=name):
+ ref = {
+ "$ref": f"#/{keyword}/{token}",
+ "description": "Value",
+ }
+ schema = {
+ keyword: {name: {"type": "string"}},
+ "properties": {"value": ref},
+ }
+ original = json.dumps(schema, sort_keys=True)
+ self.assertEqual(
+ _flatten_json_schema(schema),
+ {
+ "properties": {
+ "value": {
+ "type": "string",
+ "description": "Value",
+ },
+ },
+ },
+ )
+ self.assertEqual(
+ json.dumps(schema, sort_keys=True),
+ original,
+ )
+
+ def test_flatten_escaped_recursive_definition(self) -> None:
+ """Escaped names use the same decoded key for cycle detection."""
+ result = _flatten_json_schema(
+ {
+ "$defs": {
+ "Node/Child": {
+ "type": "object",
+ "properties": {
+ "next": {"$ref": "#/$defs/Node~1Child"},
+ },
+ },
+ },
+ "properties": {"root": {"$ref": "#/$defs/Node~1Child"}},
+ },
+ )
+ self.assertEqual(
+ result["properties"]["root"]["properties"]["next"],
+ {"type": "object", "description": "(circular: Node/Child)"},
+ )
+
def test_flatten_circular_ref_returns_placeholder(self) -> None:
"""Circular $ref produces a placeholder without infinite recursion."""
self.assertEqual(