A liveness endpoint (for example /health) answers quickly if the process is up, and a readiness endpoint (for example /ready) checks dependencies such as the database or model loading before the instance receives traffic; orchestrators like Kubernetes use them to restart or route around unhealthy instances.
from sqlalchemy import text
@app.get("/health")
def health():
return {"status": "ok"}
@app.get("/ready")
async def ready(session: AsyncSession = Depends(get_session)):
await session.execute(text("SELECT 1"))
return {"status": "ready"}