Skip to content

Commit

Permalink
improve: [schema] deref deeply
Browse files Browse the repository at this point in the history
  • Loading branch information
luochen1990 committed Aug 6, 2024
1 parent 7e70e49 commit b8ce626
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/ai_powered/schema_deref.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ def deref(schema: dict[str, Any]) -> dict[str, Any]:
"required": [param.name for param in sig.parameters.values() if param.default == inspect.Parameter.empty],
}
'''
if '$ref' and '$defs' in schema:
ref_name = schema["$ref"].split('/')[-1]
return schema["$defs"][ref_name]
else:
return schema

defs = schema.get("$defs", {})

def _deref(schema: dict[str, Any]) -> dict[str, Any]:
if '$ref' in schema:
ref_name = schema["$ref"].split('/')[-1]
return _deref(defs[ref_name])
elif 'properties' in schema:
properties = {key: _deref(value) for key, value in schema['properties'].items()}
return schema | {"properties": properties}
else:
return schema

return _deref(schema)

0 comments on commit b8ce626

Please sign in to comment.