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

304
Views
unable to do prediction in fastapi and return response

I have a machine learning API where I am doing predictions. It's working fine before introducing task_id. But after adding task_id I am getting response as null even though it's working properly but not returning the response

@app.get('/predict')
def predict(task_id,solute: str, solvent: str):
    if task_id ==0:
        results = predictions(solute, solvent)
        response["interaction_map"] = (results[1].detach().numpy()).tolist()
        response["predictions"] = results[0].item()
        return {'result': response}
    if task_id == 1:
        return "this is second one"

enter image description here

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The reason for returning null is that your code never makes it into the if statements (which, by the way, should be if...elif, rather than having two individual if statements). This is due to your endpoint receiving task_id as a string (i.e., "0", "1", etc), but you check against integer values. Thus, you should declare task_id as int parameter, as shown below:

@app.get('/predict')
def predict(task_id: int, solute: str, solvent: str):
    if task_id == 0:
        results = predictions(solute, solvent)
        response["interaction_map"] = (results[1].detach().numpy()).tolist()
        response["predictions"] = results[0].item()
        return {'result': response}
    elif task_id == 1:
        return "this is second one"
    else:
        return "some default response"
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!