'FastAPI: How to return a dictionary that includes numpy arrays?
I get the following error when I try to access the 'data' variable from the endpoint '/'.
ValueError: [ValueError('dictionary update sequence element #0 has length 1; 2 is required'), TypeError('vars() argument must have __dict__ attribute')]
Code:
from fastapi import FastAPI
app = FastAPI()
data = {}
@app.on_event("startup")
def startup_event():
data[1] = [...] ...(numpy array)
data[2] = [...] ...(numpy array)
return data
@app.get("/")
def home():
return {'Data': data}
When I launch the endpoint I see 'Internal Server Error'. Nothing would display at the endpoint '/'. However, if I add this line -> 'print(data)' just above the return in home function for endpoint '/', it does print the values stored in data dictionary as specified in startup function. How can I fix the issue that the data variable having values be visible at endpoint '/' for use?
Solution 1:[1]
FastAPI has no idea how it should decode numpy arrays to JSON; instead, do it explicitly yourself and then return the structure as native Python datatypes or as a raw JSON string. You can do this by using the custom Response objects in FastAPI.
return Response(content=numpy_as_json, media_type="application/json")
Or if you have it formatted as native datatypes:
return JSONResponse(content=numpy_as_native_datatypes)
Solution 2:[2]
You should convert (serialise) any numpy arrays into JSON before returning the data. For example:
data[1] = json.dumps(np.array([1, 2, 3, 4]).tolist())
data[2] = json.dumps(np.array([5, 6, 7, 8]).tolist())
Alternatively, you could serialise the whole dictionary object and return it in a custom Response, such as below:
json_data = json.dumps({k: v.tolist() for k, v in data.items()})
return Response(content=json_data, media_type="application/json")
or you could even use jsonable_encoder - which is used internally by FastAPI to convert a return value into JSON - and return a JSONResponse directly.
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder
json_data = jsonable_encoder({k: v.tolist() for k, v in data.items()})
return JSONResponse(content=json_data)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MatsLindh |
| Solution 2 |
