feat: harden public OpenAI API contracts
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user