diff --git a/backend/open_webui/routers/ollama.py b/backend/open_webui/routers/ollama.py index 4fca10e1f..9314c8bfc 100644 --- a/backend/open_webui/routers/ollama.py +++ b/backend/open_webui/routers/ollama.py @@ -26,7 +26,7 @@ from fastapi import ( ) from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import StreamingResponse -from pydantic import BaseModel, ConfigDict +from pydantic import BaseModel, ConfigDict, validator from starlette.background import BackgroundTask @@ -936,10 +936,19 @@ async def generate_completion( class ChatMessage(BaseModel): role: str - content: str + content: Optional[str] = None tool_calls: Optional[list[dict]] = None images: Optional[list[str]] = None + @validator("content", pre=True) + @classmethod + def check_at_least_one_field(cls, field_value, values, **kwargs): + # Raise an error if both 'content' and 'tool_calls' are None + if field_value is None and ("tool_calls" not in values or values["tool_calls"] is None): + raise ValueError("At least one of 'content' or 'tool_calls' must be provided") + + return field_value + class GenerateChatCompletionForm(BaseModel): model: str