FastAPI returns correct NumPy output locally but React fetch receives corrupted / inconsistent data in production
08:55 13 Dec 2025

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.score sometimes logs as null, 0, or an outdated value

  • Refreshing 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)

python reactjs json react-native