From 0525dd2bb5c13c18c92654bd7e025dc254f5e8ac Mon Sep 17 00:00:00 2001 From: Su YR Date: Wed, 28 Aug 2024 15:16:56 +0800 Subject: [PATCH 1/2] feat: support Literal type in Tools --- backend/utils/schemas.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backend/utils/schemas.py b/backend/utils/schemas.py index 452f95bc7..8fac51216 100644 --- a/backend/utils/schemas.py +++ b/backend/utils/schemas.py @@ -1,5 +1,7 @@ +from ast import literal_eval + from pydantic import BaseModel, Field, create_model -from typing import Any, Optional, Type +from typing import Any, Optional, Type, Literal def json_schema_to_model(tool_dict: dict[str, Any]) -> Type[BaseModel]: @@ -100,5 +102,7 @@ def json_schema_to_pydantic_type(json_schema: dict[str, Any]) -> Any: return dict elif type_ == "null": return Optional[Any] # Use Optional[Any] for nullable fields + elif type_ is "literal": + return Literal[literal_eval(json_schema.get("enum"))] else: raise ValueError(f"Unsupported JSON schema type: {type_}") From 45363a2abbd43bb1b0119ceea2f876b8f36cabab Mon Sep 17 00:00:00 2001 From: Su YR Date: Thu, 29 Aug 2024 15:55:12 +0800 Subject: [PATCH 2/2] fix: type_ == "literal" --- backend/utils/schemas.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/utils/schemas.py b/backend/utils/schemas.py index 8fac51216..cb029ade3 100644 --- a/backend/utils/schemas.py +++ b/backend/utils/schemas.py @@ -102,7 +102,7 @@ def json_schema_to_pydantic_type(json_schema: dict[str, Any]) -> Any: return dict elif type_ == "null": return Optional[Any] # Use Optional[Any] for nullable fields - elif type_ is "literal": + elif type_ == "literal": return Literal[literal_eval(json_schema.get("enum"))] else: raise ValueError(f"Unsupported JSON schema type: {type_}")