Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Discussion options

You must be logged in to vote

Using Pydantic BaseModel with Depends was never officially supported in FastAPI. It partially works, but not always as expected.

Since 0.115.0 FastAPI you can officially use Pydantic models to declare Query parameters.
To do this, you should replace params: Params = Depends() with params: Params = Query().
You should also modify your validator to expect list of strings, not just string - since parameter is annotated as list, FastAPI will extract it as list and pass to your validator.

from fastapi import Query, FastAPI, Request
from pydantic import BaseModel, field_validator


class Params(BaseModel):
    ids: list[str] = []

    @field_validator("ids", mode="before")
    @classmethod
    def

Replies: 3 comments 6 replies

Comment options

You must be logged in to vote
4 replies
@michaelspori
Comment options

@ahmedabdou14
Comment options

@michaelspori
Comment options

@ahmedabdou14
Comment options

Comment options

You must be logged in to vote
2 replies
@michaelspori
Comment options

@sarangj
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
4 participants