Building a Production‑Ready FastAPI Ticket Classification Service with Sentry, Env‑Guard, and Render Deployment
TL;DR: I turned a prototype FastAPI classifier into a production‑grade service by adding Sentry, explicit env‑var validation, idempotent Docker builds, and a Render deployment pipeline. The same commit also introduced a PII‑free read‑only export view for PCView, enabling safe data sharing with the Hotel Ops team.
The Problem
Our AI Lab needed a reliable endpoint that classifies incoming support tickets against the ISO 27001 control catalog. The original prototype lived in apps/iso27001-auditor/app/main.py and called the Groq LLM directly. Two issues surfaced in production testing on Render:
-
Missing
GROQ_API_KEYcaused a crash‑loop. The service raised aKeyErrorduring startup, which Render interpreted as a fatal container failure. - No observability. When the LLM timed out or returned an unexpected payload, we had no trace of the failure, making debugging a guessing game.
At the same time, the Hotel Ops team requested a read‑only export of PCView data that stripped any PII, but the existing export view leaked customer names.
Both problems blocked our weekly sprint goals and risked violating data‑privacy policies.
What I Tried First
My first attempt was to wrap the Groq client call in a try/except inside the request handler, hoping that the container would stay alive even if the env var was missing:
# apps/iso27001-auditor/app/main.py (initial attempt)
client = Groq(api_key=os.getenv("GROQ_API_KEY")) # returns None if missing
@app.post("/classify")
async def classify(ticket: Ticket):
try:
resp = client.chat.completions.create(...)
return {"label": resp.label}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
Result: The GROQ_API_KEY lookup returned None, and the Groq SDK raised a ValueError during import, not inside the request handler. The container exited before any health check passed, and Render kept restarting it.
I also tried adding a basic health‑check endpoint (/health) that returned 200 OK, but Render’s liveness probe still failed because the process never started.
Finally, I attempted to disable Sentry and rely on plain print statements for debugging. The logs were noisy and gave no stack traces for background tasks.
All of these approaches failed to give us a stable, observable service.
The Implementation
1. Guarding Environment Variables at Startup
The fix lives in apps/iso27001-auditor/app/__init__.py. I introduced a small helper that validates required env vars before any third‑party SDK is imported:
# apps/iso27001-auditor/app/__init__.py
import os
import sys
REQUIRED_VARS = ["GROQ_API_KEY", "SENTRY_DSN"]
missing = [var for var in REQUIRED_VARS if not os.getenv(var)]
if missing:
# Render will see a non‑zero exit code and mark the container as failed,
# but we prefer a graceful 503 response for incoming traffic.
sys.stderr.write(f"Missing required env vars: {', '.join(missing)}\n")
# Export a flag that the ASGI app can read.
os.environ["ENV_NOT_READY"] = "1"
Now the container starts, but the app knows it’s not ready to serve LLM requests.
2. Adding Sentry for Full‑Stack Observability
Sentry initialization is also placed in __init__.py, after the env guard:
# apps/iso27001-auditor/app/__init__.py (continued)
if not os.getenv("ENV_NOT_READY"):
import sentry_sdk
sentry_sdk.init(
dsn=os.getenv("SENTRY_DSN"),
traces_sample_rate=0.2,
environment="production",
)
The CHANGELOG.md entry for 2026‑11‑17 notes the addition:
### Added (2026-11-17)
- Sentry live for the AI Gateway (projects ai-lab-gateway, ai-lab-iso-auditor)
3. Graceful 503 for Unconfigured LLM
In apps/iso27001-auditor/app/main.py I added a middleware that checks the ENV_NOT_READY flag and returns a static 503 response:
# apps/iso27001-auditor/app/main.py
from fastapi import FastAPI, Request, Response, status
from fastapi.responses import JSONResponse
app = FastAPI(title="ISO 27001 AI Auditor")
@app.middleware("http")
async def env_ready_middleware(request: Request, call_next):
if os.getenv("ENV_NOT_READY"):
return JSONResponse(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
content={"detail": "LLM not configured (GROQ_API_KEY missing)"},
)
return await call_next(request)
Now the service stays up, Render’s health checks pass, and callers receive a clear 503 Service Unavailable instead of a container crash.
4. Refactoring the Classification Endpoint
The endpoint now uses the validated Groq client and captures any SDK‑level errors as Sentry events:
python
# apps/iso27001-auditor/app/main.py (continued)
from groq import Groq
from pydantic import BaseModel
class Ticket(BaseModel):
---
*Part of my [Build in Public](https://dev.to/zaerohell) series — sharing the real process of building SaaS projects from Playa del Carmen, México.*
*Repo: `zaerohell/ai-enterprise-automation-lab` · 2026-11-17*
\#playadev #buildinpublic
Top comments (0)