[GH-ISSUE #4969] Support Literal type in Tools #13807

Closed
opened 2026-04-19 20:24:51 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @daniel-code on GitHub (Aug 28, 2024).
Original GitHub issue: https://github.com/open-webui/open-webui/issues/4969

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

OpenWebUI raises error when there is Literal type in JSON schema. This is a valid JSON schema type and should be supported by OpenWebUI.

Example function with Literal type:

class Tools:
    def foo(self, x: Literal["x","y","z"]) -> None:
        ...

Raise error:

    File "open-webui/backend/utils/schemas.py", line 104, in json_schema_to_pydantic_type                                                                        
    raise ValueError(f"Unsupported JSON schema type: {type_} {json_schema}")
ValueError: Unsupported JSON schema type: literal

Describe the solution you'd like
A clear and concise description of what you want to happen.

from ast import literal_eval

def json_schema_to_pydantic_type(json_schema: dict[str, Any]) -> Any:
    """
    Converts a JSON schema type to a Pydantic type.

    Args:
        json_schema: The JSON schema to convert.

    Returns:
        A Pydantic type.
    """

    type_ = json_schema.get("type")

    if type_ == "string" or type_ == "str":
        return str
    elif type_ == "integer" or type_ == "int":
        return int
    elif type_ == "number" or type_ == "float":
        return float
    elif type_ == "boolean" or type_ == "bool":
        return bool
    elif type_ == "array" or type_ == "list":
        items_schema = json_schema.get("items")
        if items_schema:
            item_type = json_schema_to_pydantic_type(items_schema)
            return list[item_type]
        else:
            return list
    elif type_ == "object":
        # Handle nested models.
        properties = json_schema.get("properties")
        if properties:
            nested_model = json_schema_to_model(json_schema)
            return nested_model
        else:
            return dict
    elif type_ == "null":
        return Optional[Any]  # Use Optional[Any] for nullable fields
    elif type_ is "literal":  # Added support for Literal type
        return Literal[literal_eval(json_schema.get("enum"))]
    else:
        raise ValueError(f"Unsupported JSON schema type: {type_}")

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Originally created by @daniel-code on GitHub (Aug 28, 2024). Original GitHub issue: https://github.com/open-webui/open-webui/issues/4969 **Is your feature request related to a problem? Please describe.** A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] OpenWebUI raises error when there is Literal type in JSON schema. This is a valid JSON schema type and should be supported by OpenWebUI. Example function with Literal type: ``` class Tools: def foo(self, x: Literal["x","y","z"]) -> None: ... ``` Raise error: ``` File "open-webui/backend/utils/schemas.py", line 104, in json_schema_to_pydantic_type raise ValueError(f"Unsupported JSON schema type: {type_} {json_schema}") ValueError: Unsupported JSON schema type: literal ``` **Describe the solution you'd like** A clear and concise description of what you want to happen. ```python from ast import literal_eval def json_schema_to_pydantic_type(json_schema: dict[str, Any]) -> Any: """ Converts a JSON schema type to a Pydantic type. Args: json_schema: The JSON schema to convert. Returns: A Pydantic type. """ type_ = json_schema.get("type") if type_ == "string" or type_ == "str": return str elif type_ == "integer" or type_ == "int": return int elif type_ == "number" or type_ == "float": return float elif type_ == "boolean" or type_ == "bool": return bool elif type_ == "array" or type_ == "list": items_schema = json_schema.get("items") if items_schema: item_type = json_schema_to_pydantic_type(items_schema) return list[item_type] else: return list elif type_ == "object": # Handle nested models. properties = json_schema.get("properties") if properties: nested_model = json_schema_to_model(json_schema) return nested_model else: return dict elif type_ == "null": return Optional[Any] # Use Optional[Any] for nullable fields elif type_ is "literal": # Added support for Literal type return Literal[literal_eval(json_schema.get("enum"))] else: raise ValueError(f"Unsupported JSON schema type: {type_}") ``` **Describe alternatives you've considered** A clear and concise description of any alternative solutions or features you've considered. **Additional context** Add any other context or screenshots about the feature request here.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#13807