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

162
Views
422 Unprocessable Entity in fastapi when returning response

I am using fast API to do machine learning inference and return response. There are two tasks that are controlled by task_id. For task_id=0 both solvent and solute are required to return response when using task_id=1 I only need solute. But when giving the values for the keys it is required to give solvent value in task_id=1 where I don't need it. If not given the value I am getting 422 Unprocessable Entity. Is their a way to write so that it wont throw the error if solvent value is not provided when using task_id=1

@app.get('/predict')
async def predict(task_id:int, background_tasks: BackgroundTasks,solute,solvent):
    global task_id_global
    task_id_global = task_id
    if task_id == 0:
        background_tasks.add_task(predictions,solute,solvent)
        return {'success'}
    elif task_id == 1:
        # solvent = None
        background_tasks.add_task(predictions_two,solute)
        return {'success'}
    else:
        return "Give proper task_id"
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Declare your parameter as Optional. For instance:

from typing import Optional
...
async def predict(solvent: Optional[str] = None):
...
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!