File size: 1,027 Bytes
1f1b4db |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
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",
)
|