Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.5K
Vistas
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?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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)
over 4 years ago · Santiago Trujillo Denunciar

0

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)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda