I’m building a small ML-backed API using FastAPI (Python) and consuming it from a React frontend.
Locally, everything works as expected.
However, in production (Docker + reverse proxy), the same endpoint returns inconsistent or corrupted data when fetched from React, even though the Python side logs show correct values.
This is not a serialization error (no exceptions thrown).
Backend (FastAPI)
import numpy as np
from fastapi import FastAPI
from fastapi.responses import JSONResponse
app = FastAPI()
@app.get("/predict")
def predict():
arr = np.random.rand(1, 512).astype("float32")
result = arr.mean(axis=1)
return JSONResponse(content={"score": float(result[0])})
Logs show correct values every request.
Frontend (React)
useEffect(() => {
fetch("/predict")
.then(res => res.json())
.then(data => {
console.log("Received:", data.score);
});
}, []);
The Problem
In production only:
data.scoresometimes logs asnull,0, or an outdated valueRefreshing the page may fix or change the value
Backend logs always show the correct value
No errors in browser console or network tab
Response headers show
200 OK
Environment Details
Backend: FastAPI + Uvicorn
Frontend: React (Vite)
Deployed using Docker
Reverse proxy: Nginx
No caching intentionally enabled
What I’ve Ruled Out
JSON serialization issues
Floating point precision problems
React state bugs
Backend exceptions
Browser cache (hard refresh tested)