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

791
Vistas
500 Undocumented Error: Internal Server Error when returning response in FastAPI

I am using FastAPI to make predictions using a ML model. When I give a task_id and input, the app should add it to the background task and return the response accordingly. However, I am getting Error 500 when I try to do it.

After adding task_id_globally, it started throwing errors before it worked fine.

Error

  File ".\app\main.py", line 36, in post
    return {'result': response_name[task_id_global]}
TypeError: list indices must be integers or slices, not NoneType

Code

task_id_global = None
@app.get('/predict')
async def predict(task_id:int, background_tasks: BackgroundTasks,solute,solvent):
    task_id_global = task_id
    if task_id == 0:
        background_tasks.add_task(predictions,solute,solvent)
        return {'success'}
    elif task_id == 1:
        background_tasks.add_task(predictions_two,solute)
        return {'success'}
    else:
        return "Give proper task_id"
    
response_name = [response, attach_drug_name()]

@app.get('/predict_solubility')
async def post():
    return {'result': response_name[task_id_global]}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You have set task_id_global to None, and thus, when calling /predict_solubility endpoint, it is trying to retrieve an element from the list using response_name[None]; hence, the error. So you should set task_id_global to 0, which should point to some default value in your response_name list - even if /predict endpoint has not yet been called - or perform a check inside the second endpoint to see whether task_id_global is not None and then decide whether to proceed retrieving an item from the list. Next, inside /predict endpoint declare task_id_global as global before using it (using the global keyword), as, in the way it is currently declared, it is interpreted as a local variable, and hence, the global one never gets affected by any changes occur to task_id_global inside the endpoint (have a look here).

task_id_global = None

@app.get('/predict')
async def predict(task_id:int,solute,solvent):
    global task_id_global
    task_id_global = task_id
    ...

Also, depending on your task (e.g., if you have multiple requests concurrently accessing that global variable), you might want to consider other options, such as Redis for instance. Have a look at this answer.

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