feat: add parsing for Literal types (#1872)

This commit is contained in:
Sarah Wooders
2025-04-23 21:41:01 -07:00
committed by GitHub
parent 923af54d0b
commit 9b6c266f17
2 changed files with 7 additions and 1 deletions

View File

@@ -2,7 +2,7 @@ import importlib
import inspect
from textwrap import dedent # remove indentation
from types import ModuleType
from typing import Dict, List, Optional
from typing import Dict, List, Literal, Optional
from letta.errors import LettaToolCreateError
from letta.functions.schema_generator import generate_schema
@@ -20,6 +20,7 @@ def derive_openai_json_schema(source_code: str, name: Optional[str] = None) -> d
"Optional": Optional,
"List": List,
"Dict": Dict,
"Literal": Literal,
# To support Pydantic models
# "BaseModel": BaseModel,
# "Field": Field,

View File

@@ -5,6 +5,7 @@ from typing import Any, Dict, List, Optional, Type, Union, get_args, get_origin
from composio.client.collections import ActionParametersModel
from docstring_parser import parse
from pydantic import BaseModel
from typing_extensions import Literal
from letta.functions.mcp_client.types import MCPTool
@@ -70,6 +71,10 @@ def type_to_json_schema_type(py_type) -> dict:
"items": type_to_json_schema_type(args[0]),
}
# Handle literals
if get_origin(py_type) is Literal:
return {"type": "string", "enum": get_args(py_type)}
# Handle object types
if py_type == dict or origin in (dict, Dict):
args = get_args(py_type)