Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

792
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!