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.
fromastimportliteral_evaldefjson_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")iftype_=="string"ortype_=="str":returnstreliftype_=="integer"ortype_=="int":returninteliftype_=="number"ortype_=="float":returnfloateliftype_=="boolean"ortype_=="bool":returnbooleliftype_=="array"ortype_=="list":items_schema=json_schema.get("items")ifitems_schema:item_type=json_schema_to_pydantic_type(items_schema)returnlist[item_type]else:returnlisteliftype_=="object":# Handle nested models.properties=json_schema.get("properties")ifproperties:nested_model=json_schema_to_model(json_schema)returnnested_modelelse:returndicteliftype_=="null":returnOptional[Any]# Use Optional[Any] for nullable fieldseliftype_is"literal":# Added support for Literal typereturnLiteral[literal_eval(json_schema.get("enum"))]else:raiseValueError(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.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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:
Raise error:
Describe the solution you'd like
A clear and concise description of what you want to happen.
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.