'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]}


Solution 1:[1]

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.

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