Spaces:
Build error

trackademi / scripts /export_schemas.py
anwarKXC
feat: Implement stream event models and ticket authentication for SSE
1280bcc
Raw History Blame Contribute Delete
2.42 kB
#!/usr/bin/env python3
"""Export Pydantic model JSON Schemas for cross-service contract testing.
Generates JSON Schema files in chatbot/schemas/ that the NestJS backend
validates against its own DTOs using ajv. This catches breaking changes
in the Python↔NestJS API contract at CI time.
Usage:
cd chatbot/
python scripts/export_schemas.py
"""
import json
import sys
from pathlib import Path
# Ensure the chatbot package is importable
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from app.models.schemas import (
QueryRequest,
QueryResponse,
RetrievedChunkResponse,
LLMMetrics,
IngestRequest,
IngestResponse,
HealthResponse,
)
from app.models.errors import ErrorEnvelope
from app.models.routing_schemas import RoutingEnvelopeInput
from app.models.structured_schemas import (
ExamStructured,
AssignmentStructured,
LessonPlanStructured,
)
from app.models.stream_events import (
StreamMetaEvent,
StreamTokenEvent,
StreamDoneEvent,
StreamProgressEvent,
StreamErrorEvent,
)
MODELS = {
# Core request/response contracts
"QueryRequest": QueryRequest,
"QueryResponse": QueryResponse,
"RetrievedChunkResponse": RetrievedChunkResponse,
"LLMMetrics": LLMMetrics,
"IngestRequest": IngestRequest,
"IngestResponse": IngestResponse,
"HealthResponse": HealthResponse,
"ErrorEnvelope": ErrorEnvelope,
"RoutingEnvelopeInput": RoutingEnvelopeInput,
# Structured output schemas
"ExamStructured": ExamStructured,
"AssignmentStructured": AssignmentStructured,
"LessonPlanStructured": LessonPlanStructured,
# SSE stream event payloads
"StreamMetaEvent": StreamMetaEvent,
"StreamTokenEvent": StreamTokenEvent,
"StreamDoneEvent": StreamDoneEvent,
"StreamProgressEvent": StreamProgressEvent,
"StreamErrorEvent": StreamErrorEvent,
}
OUTPUT_DIR = Path(__file__).resolve().parent.parent / "schemas"
def main() -> None:
OUTPUT_DIR.mkdir(exist_ok=True)
exported = 0
for name, model in MODELS.items():
schema = model.model_json_schema()
out_path = OUTPUT_DIR / f"{name}.schema.json"
out_path.write_text(
json.dumps(schema, indent=2, ensure_ascii=False) + "\n",
encoding="utf-8",
)
exported += 1
print(f"Exported {exported} schemas to {OUTPUT_DIR}/")
if __name__ == "__main__":
main()