Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: add parsing for `Literal` types (#1872)
Sarah Woodersd48fcde3516c
diff --git a/letta/functions/functions.py b/letta/functions/functions.pyindex d5e9d0883..007d587d1 100644--- a/letta/functions/functions.py+++ b/letta/functions/functions.py@@ -2,7 +2,7 @@ import importlibimport inspectfrom textwrap import dedent # remove indentationfrom types import ModuleType-from typing import Dict, List, Optional+from typing import Dict, List, Literal, Optionalfrom letta.errors import LettaToolCreateErrorfrom 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,diff --git a/letta/functions/schema_generator.py b/letta/functions/schema_generator.pyindex 62b134aed..209394640 100644--- a/letta/functions/schema_generator.py+++ b/letta/functions/schema_generator.py@@ -5,6 +5,7 @@ from typing import Any, Dict, List, Optional, Type, Union, get_args, get_originfrom composio.client.collections import ActionParametersModelfrom docstring_parser import parsefrom pydantic import BaseModel+from typing_extensions import Literalfrom 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 typesif py_type == dict or origin in (dict, Dict):args = get_args(py_type)