test / service /utils /validation.py
gaoqilan's picture
Upload 103 files
1f1b4db verified
import json
from fastapi import HTTPException
def validate_message_format(messages):
"""
Validate the format of messages.
"""
try:
json.dumps(messages)
except TypeError:
raise HTTPException(
status_code=400,
detail="Invalid message format - messages must be valid JSON",
)
for msg in messages:
if "role" not in msg or "content" not in msg:
raise HTTPException(
status_code=400,
detail="Messages must contain 'role' and 'content' fields",
)
if (
not isinstance(msg["role"], str)
or msg["role"] not in ["user", "assistant", "system"]
or msg["content"] is None
or not isinstance(msg["content"], (str, list))
):
raise HTTPException(
status_code=400,
detail="Invalid role or content - role must be 'user', 'assistant', or 'system', and content must be a string or list",
)