From 4ba5b8313776fb5896f3202f9fcce6decb3cc8f2 Mon Sep 17 00:00:00 2001 From: Evan Lin Date: Sun, 1 Mar 2026 08:19:28 +0000 Subject: [PATCH] fix(openai): allow null message content for tool_calls --- app/api/v1/chat.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/api/v1/chat.py b/app/api/v1/chat.py index 7a79009a..f3d1801c 100644 --- a/app/api/v1/chat.py +++ b/app/api/v1/chat.py @@ -18,14 +18,15 @@ router = APIRouter(tags=["Chat"]) -VALID_ROLES = ["developer", "system", "user", "assistant"] +VALID_ROLES = ["developer", "system", "user", "assistant", "tool"] USER_CONTENT_TYPES = ["text", "image_url", "input_audio", "file"] class MessageItem(BaseModel): """消息项""" role: str - content: Union[str, List[Dict[str, Any]]] + # OpenAI 兼容:assistant 在携带 tool_calls 时,content 可能为 null + content: Union[str, List[Dict[str, Any]], None] = None @field_validator("role") @classmethod @@ -122,6 +123,16 @@ def validate_request(request: ChatCompletionRequest): # 验证消息 for idx, msg in enumerate(request.messages): content = msg.content + + # 允许 assistant/tool 的空内容(常见于 tool_calls / tool 结果消息) + if content is None: + if msg.role in ("assistant", "tool"): + continue + raise ValidationException( + message="Message content cannot be empty", + param=f"messages.{idx}.content", + code="empty_content" + ) # 字符串内容 if isinstance(content, str):