Last updated: 2026-04-05
This package contains the FastAPI backend that acts as the sole AI orchestration and service workflow layer for the STEM Learning Platform. The web app depends on this service for AI generation, chat workspace behavior, analytics, teaching briefs, and several class-level workflows.
The backend owns:
- provider orchestration across OpenAI, Gemini, and OpenRouter
- structured AI generation for blueprints, quizzes, flashcards, chat, analytics, and teaching briefs
- chat workspace session and message orchestration
- guest sandbox AI guardrails and quota enforcement
- class create and join flows
- material dispatch orchestration
- response envelope normalization
flowchart LR
Web[Next.js Web App] --> API[FastAPI Routes]
API --> Domain[Domain Modules]
Domain --> SB[(Supabase)]
Domain --> Providers[OpenAI / Gemini / OpenRouter]
| Module | Purpose |
|---|---|
app/main.py |
app bootstrap, auth middleware, route registration, exception handling |
app/providers.py |
provider fallback and embeddings generation |
app/chat.py |
grounded chat generation |
app/chat_workspace.py |
session lifecycle, pagination, compaction, persistence orchestration |
app/blueprints.py |
blueprint generation |
app/quiz.py |
quiz generation |
app/flashcards.py |
flashcard generation |
app/materials.py |
material dispatch and processing triggers |
app/analytics.py |
class intelligence, teaching brief, data query generation |
app/canvas.py |
chart and visual canvas spec validation/generation helpers |
app/classes.py |
class creation and join-by-code |
app/config.py |
environment-driven settings |
app/schemas.py |
request and response schemas |
All normal domain routes return the canonical envelope:
{
"ok": true,
"data": {},
"error": null,
"meta": {
"request_id": "..."
}
}This contract is relied on by the frontend adapters and should remain stable.
GET /healthzPOST /v1/llm/generatePOST /v1/llm/embeddings
POST /v1/materials/dispatchPOST /v1/materials/process
POST /v1/classes/createPOST /v1/classes/join
POST /v1/blueprints/generatePOST /v1/quiz/generatePOST /v1/flashcards/generate
POST /v1/chat/generatePOST /v1/chat/canvasPOST /v1/chat/workspace/participantsPOST /v1/chat/workspace/sessions/listPOST /v1/chat/workspace/sessions/createPOST /v1/chat/workspace/sessions/renamePOST /v1/chat/workspace/sessions/archivePOST /v1/chat/workspace/messages/listPOST /v1/chat/workspace/messages/send
POST /v1/analytics/class-insightsPOST /v1/analytics/class-teaching-briefPOST /v1/analytics/data-query
- The web app authenticates to the backend with
PYTHON_BACKEND_API_KEY. - In hosted environments,
PYTHON_BACKEND_ALLOW_UNAUTHENTICATED_REQUESTSshould remainfalse.
- Some routes also require a valid Supabase user bearer token.
- The backend verifies that bearer token against Supabase Auth.
- Class create/join and guest-aware AI flows rely on authenticated user identity, not just the service key.
Guest AI requests are not just a frontend concern. The backend enforces:
- sandbox ownership verification
- per-feature guest rate limits
- guest concurrent AI slot limits
- guest-specific
sandbox_idrequirements where applicable
Guest session creation caps are not owned by the Python service. Those are enforced in Supabase by the guest_session_quota model and the acquire_guest_session_service / release_guest_session_slot_service RPCs, which replaced the older per-IP entry rate limit.
Current guest guardrail env vars:
GUEST_MAX_CONCURRENT_AI_REQUESTSGUEST_CHAT_LIMITGUEST_QUIZ_LIMITGUEST_FLASHCARDS_LIMITGUEST_BLUEPRINT_LIMITGUEST_EMBEDDING_LIMIT
The backend supports:
direct_v1langgraph_v1
- chat is grounded in published blueprint data and retrieved material chunks
- workspace chat persists sessions and paginated message history
- long conversations use compaction rather than replaying the full transcript
langgraph_v1can use tool-aware orchestration while preserving the frontend contract
The backend also powers teacher-facing intelligence flows.
- aggregates class quiz, engagement, and topic data
- produces structured class insight payloads
- persists snapshot-friendly results through Supabase-backed workflows
- generates concise teacher-oriented summaries and actions
- normalizes mixed-shape AI payloads into stable frontend-safe data
- converts natural language analytics questions into structured chart specifications
- supports chat-side canvas generation through
/v1/chat/canvas
- Python 3.11+
- project dependencies from
backend/requirements.txt
pip install -r backend/requirements.txtuvicorn app.main:app --app-dir backend --host 0.0.0.0 --port 8001 --reloadbackend/.env.example is a checklist, not an automatically loaded config file. Export the variables you need into your shell or use your own env loader before running the command above.
PYTHON_BACKEND_API_KEYPYTHON_BACKEND_ALLOW_UNAUTHENTICATED_REQUESTSPYTHON_BACKEND_LOG_PROVIDER_FAILURES
AI_PROVIDER_DEFAULTAI_REQUEST_TIMEOUT_MSAI_EMBEDDING_TIMEOUT_MS
OPENROUTER_API_KEYOPENROUTER_MODELOPENROUTER_EMBEDDING_MODELOPENROUTER_BASE_URLOPENROUTER_SITE_URLOPENROUTER_APP_NAME
OPENAI_API_KEYOPENAI_MODELOPENAI_EMBEDDING_MODEL
GEMINI_API_KEYGEMINI_MODELGEMINI_EMBEDDING_MODEL
SUPABASE_URLSUPABASE_SERVICE_ROLE_KEY
Optional compatibility fallbacks:
NEXT_PUBLIC_SUPABASE_URLNEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEYNEXT_PUBLIC_SUPABASE_ANON_KEYSUPABASE_SECRET_KEY
MATERIAL_WORKER_TOKENMATERIAL_WORKER_BATCHMATERIAL_WORKER_FUNCTION_URL
- All
httpxcalls should continue usingtrust_env=Falseto avoid proxy-related production failures. - The backend is designed to fail closed if service auth is misconfigured.
- Invalid user bearer tokens should resolve to JSON envelope errors, not HTML error pages.
- The backend is a required runtime dependency, not an optional optimization layer.
Run all backend unit tests:
python3 -m unittest discover -s backend/tests -p 'test_*.py'- startup and exception handling
- provider fallback and config
- blueprint, quiz, flashcards, chat, materials, and class domains
- workspace pagination and compaction
- analytics and guest-rate-limit behavior
../README.md— project overview../ARCHITECTURE.md— deep technical architecture../DESIGN.md— product and technical design../DEPLOYMENT.md— deployment runbook../UIUX.md— frontend system and UI language../web/README.md— frontend behavior and envs../supabase/README.md— schema, functions, and queueing