Fix on ChatMessage validator: content can be null when using tools

This commit is contained in:
Simone 2025-02-19 21:03:23 +01:00
parent 8f72f8def9
commit 8662108344

View File

@ -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