Resolve JSON references in the given schema.
PARAMETER | DESCRIPTION |
schema | The JSON schema with references. TYPE: dict |
RETURNS | DESCRIPTION |
dict | The JSON schema with resolved references. TYPE: dict[str, Any] |
Source code in autogen/json_utils.py
| @require_optional_import("jsonschema", "gemini")
def resolve_json_references(schema: dict[str, Any]) -> dict[str, Any]:
"""
Resolve JSON references in the given schema.
Args:
schema (dict): The JSON schema with references.
Returns:
dict: The JSON schema with resolved references.
"""
resolver = RefResolver.from_schema(schema)
validator = Draft7Validator(schema, resolver=resolver)
resolved_schema = validator.schema
def resolve_refs(node: Any) -> Any:
if isinstance(node, dict):
if "$ref" in node:
ref = node["$ref"]
with resolver.resolving(ref) as resolved:
return resolve_refs(resolved)
else:
return {k: resolve_refs(v) for k, v in node.items()}
elif isinstance(node, list):
return [resolve_refs(item) for item in node]
else:
return node
return resolve_refs(resolved_schema) # type: ignore[no-any-return]
|