✨ Allow to have multiple Query parameter models#12944
✨ Allow to have multiple Query parameter models#12944uriyyo wants to merge 20 commits intofastapi:masterfrom
Conversation
|
This looks similar to #12481 |
|
I agree, It does solve the same issue. I didn't see it when I started working on this PR( |
|
Hope this gets approved and released soon ! |
| fields_to_extract = [] | ||
| for f in fields: | ||
| if lenient_issubclass(f.type_, BaseModel): | ||
| fields_to_extract.extend(get_cached_model_fields(f.type_)) | ||
| else: | ||
| fields_to_extract.append(f) | ||
| return fields_to_extract |
There was a problem hiding this comment.
I think that this could be simplified a little bit:
fields_to_extract = [get_cached_model_fields(f.type_) for f in fields if lenient_issubclass(f.type_, BaseModel)]
fields.extend(fields_to_extract)
return fieldsThere was a problem hiding this comment.
I'm not sure it will work, because we will have both base model and all fields, we want to remove based model and keep only it fields.
|
This looks awesome! Really looking forward to it! |
|
Do we know when this will be published? It would make things easier. |
|
Just merged with master and fixed tests, @Kludex any chance you can take a look on this PR? 🙏 |
|
@Ale-Cas can you please take a look at this ? |
|
is this merging soon? would be nice to have pagination and query on models in FastAPI, which is blocked on merging this PR .... |
This comment was marked as resolved.
This comment was marked as resolved.
|
Just chiming in that my team are also interested in this feature. 🙏 |
|
I've just noticed that the same limitation seems to affect |
There was a problem hiding this comment.
Pull request overview
This PR enables FastAPI to support multiple Pydantic model parameters for Query, Header, and Cookie annotations. Previously, only a single model parameter was allowed per endpoint. The changes update the parameter flattening and validation logic to handle multiple models or a mix of model and scalar parameters.
Key changes:
- Modified
_get_flat_fields_from_paramsto iterate through all fields and extract model fields for each - Refactored
request_params_to_argsto process multiple BaseModel parameters by building a comprehensive parameter dictionary and validating each field appropriately - Added comprehensive test coverage for multiple models and mixed scenarios across Query, Header, and Cookie parameters
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
fastapi/dependencies/utils.py |
Updated parameter processing logic to handle multiple Pydantic models in _get_flat_fields_from_params and request_params_to_args functions |
tests/test_multiple_params_models.py |
Added comprehensive tests validating multiple parameter models and mixed scenarios for Query, Header, and Cookie parameters, including OpenAPI schema verification |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
This pull request has a merge conflict that needs to be resolved. |
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
|
Hi @tiangolo, Any chance you can take a look at this PR? 🙏 |
CodSpeed Performance ReportMerging this PR will not alter performanceComparing Summary
Footnotes |
|
The maintenance team seems to be moving forward with #12481, but still have to wait for a response from tiangolo. |
Currently, it's allowed to have only one model as a query/headers/etc model parameter, in this PR I propose to remove this restriction. This PR should be backward-compatible.
For instance, example below will not work:
And same here:
WIth this PR both examples above will work as expected. Also, it will generate the correct OpenAPI schema:

Discussion - #12212