feat: harden public OpenAI API contracts
Quality gate / backend (push) Canceled after 0s
Quality gate / frontend (push) Canceled after 0s

This commit is contained in:
Xin Wang
2026-09-07 14:25:48 +08:00
parent 8025ed8aa0
commit 2649d64b55
36 changed files with 16643 additions and 236 deletions
+18 -1
View File
@@ -6,6 +6,9 @@ from dataclasses import dataclass
from typing import Any
from uuid import uuid4
from pydantic import ValidationError
from services.openai_realtime.schemas import REALTIME_CLIENT_EVENT_ADAPTER
SUPPORTED_CLIENT_EVENTS = {
"session.update",
@@ -51,7 +54,21 @@ def require_event(message: object) -> dict[str, Any]:
param="type",
event_id=str(message.get("event_id") or "") or None,
)
return message
try:
validated = REALTIME_CLIENT_EVENT_ADAPTER.validate_python(message)
except ValidationError as exc:
first = exc.errors(include_url=False)[0]
location = list(first.get("loc") or [])
if location and location[0] == event_type:
location.pop(0)
param = ".".join(str(part) for part in location) or None
raise RealtimeEventError(
str(first.get("msg") or "Event does not match the schema"),
code="invalid_event",
param=param,
event_id=str(message.get("event_id") or "") or None,
) from exc
return validated.model_dump(exclude_none=True)
def assistant_id_from_model(model: object) -> str: