Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Annotated in FastAPI ignores rules set in Query

I'm starting to learn Python and FastAPI but I encountered a weird thing. When I set

@app.get("/test")
async def test(q: str = Query(min_length=3)):
    return q

it correctly checks if q has at least 3 characters, but when I do it as suggested in docs

@app.get("/test")
async def test(q: Annotated[str, Query(min_length=3)]):
    return q

it ignores the min_length rule and only checks if q is present, is there any reason why it happens? I'm using Python 3.10.10 and FastAPI 0.94.1

like image 459
tucki Avatar asked Oct 20 '25 05:10

tucki


1 Answers

I also had this issue. The Annotate feature only works in FastAPI version 0.95+.

Just do

pip install --upgrade fastapi

to upgrade to the latest version. That should solve the problem.

like image 194
Valentin Forester Avatar answered Oct 21 '25 21:10

Valentin Forester



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!