Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

794
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda