diff options
author | Reinier van der Leer <pwuts@agpt.co> | 2024-04-30 12:23:15 +0200 |
---|---|---|
committer | Reinier van der Leer <pwuts@agpt.co> | 2024-04-30 12:23:15 +0200 |
commit | 180de0c9a9ce79939a8aae2b55d01e2bf2a5becb (patch) | |
tree | 626230ec607eddd6e004ea863856b9434157581d /autogpts | |
parent | fix(agent/core): Format parse errors for log statement in `OpenAIProvider.cre... (diff) | |
download | Auto-GPT-180de0c9a9ce79939a8aae2b55d01e2bf2a5becb.tar.gz Auto-GPT-180de0c9a9ce79939a8aae2b55d01e2bf2a5becb.tar.bz2 Auto-GPT-180de0c9a9ce79939a8aae2b55d01e2bf2a5becb.zip |
feat(agent/core): Support referenced types in `JSONSchema.from_dict`
Diffstat (limited to 'autogpts')
-rw-r--r-- | autogpts/autogpt/autogpt/core/utils/json_schema.py | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/autogpts/autogpt/autogpt/core/utils/json_schema.py b/autogpts/autogpt/autogpt/core/utils/json_schema.py index d72b509dd..ce987f5d7 100644 --- a/autogpts/autogpt/autogpt/core/utils/json_schema.py +++ b/autogpts/autogpt/autogpt/core/utils/json_schema.py @@ -57,10 +57,35 @@ class JSONSchema(BaseModel): @staticmethod def from_dict(schema: dict) -> "JSONSchema": + def resolve_references(schema: dict, definitions: dict) -> dict: + """ + Recursively resolve type $refs in the JSON schema with their definitions. + """ + if isinstance(schema, dict): + if "$ref" in schema: + ref_path = schema["$ref"].split("/")[ + 2: + ] # Split and remove '#/definitions' + ref_value = definitions + for key in ref_path: + ref_value = ref_value[key] + return resolve_references(ref_value, definitions) + else: + return { + k: resolve_references(v, definitions) for k, v in schema.items() + } + elif isinstance(schema, list): + return [resolve_references(item, definitions) for item in schema] + else: + return schema + + definitions = schema.get("definitions", {}) + schema = resolve_references(schema, definitions) + return JSONSchema( description=schema.get("description"), type=schema["type"], - enum=schema["enum"] if "enum" in schema else None, + enum=schema.get("enum"), items=JSONSchema.from_dict(schema["items"]) if "items" in schema else None, properties=JSONSchema.parse_properties(schema) if schema["type"] == "object" |