Spaces:
Build error
Build error
File size: 2,416 Bytes
1280bcc | 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #!/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()
|