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

697
Views
Optional query parameters in FastAPI

I don't understand optional query parameters in FastAPI. How is it different from default query parameters with a default value of None?

What is the difference between arg1 and arg2 in the example below where arg2 is made an optional query parameter as described in the above link?

@app.get("/info/")
async def info(arg1: int = None, arg2: int | None = None):
    return {"arg1": arg1, "arg2": arg2}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This is covered in the reference manual, albeit just as a small note:

async def read_items(q: Optional[str] = None):

FastAPI will know that the value of q is not required because of the default value = None.

The Optional in Optional[str] is not used by FastAPI, but will allow your editor to give you better support and detect errors.

(Optional[str] is the same as str | None pre 3.10 for other readers)

Since your editor might not be aware of the context in which the parameter is populated and used by FastAPI, it might have trouble understanding the actual signature of the function when the parameter is not marked as Optional. You may or may not care about this distinction.

over 4 years ago · Santiago Trujillo Report

0

It depends on the way you see it, but my philosophy is:

Optional parameters are a bigger set that includes query parameters.

That is, query parameters take an input that is a value, while optional parameters are query parameters that can have no value (i.e. 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!