Replies: 3 comments 2 replies
-
|
I worked around this in my project by making a def ensure_list(value: Any) -> Any:
if value is not None and not isinstance(value, list):
return [value]
return value
class Request(BaseModel):
list: Annotated[list[str], BeforeValidator(ensure_list)] |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Yes, this is just not supported. UPD: wait, maybe I'm wrong about this being not possible. Will investigate it.. |
Beta Was this translation helpful? Give feedback.
2 replies
This comment was marked as spam.
This comment was marked as spam.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
also on Github at https://github.com/mtblanton/fastapi-form-list-repro
Description
The Form parsing, mapping, and validation runs into trouble when these conditions are combined:
listfieldlistfield only has one elementCall an endpoint with a single form value for
listcurl --request POST \ --url http://localhost:8000/test-union \ --header 'content-type: multipart/form-data' \ --form type=list \ --form list=fooI expect a response of
{ "type": "list", "list": [ "foo" ] }I get a validation error instead:
{ "detail": [ { "type": "list_type", "loc": [ "body", "list", "list" ], "msg": "Input should be a valid list", "input": "foo" } ] }This is not an issue when calling the endpoint with multiple items for
list:curl --request POST \ --url http://localhost:8000/test-union \ --header 'content-type: multipart/form-data' \ --form type=list \ --form list=foo \ --form list=foo2Or when calling an endpoint that doesn't use a union:
curl --request POST \ --url http://localhost:8000/test-no-union \ --header 'content-type: multipart/form-data' \ --form type=list \ --form list=fooOperating System
macOS
Operating System Details
M2 Macbook Pro on Tahoe 26.2
FastAPI Version
0.135.1
Pydantic Version
2.12.5
Python Version
3.14.2
Additional Context
I stepped through FastAPI code a bit in the debugger to see if it was something I could work around with different types but didn't find anything. My guess is that
utils._extract_form_bodyorutils.request_body_to_argsdon't know what to do withUniontypes! When it was parsing a non-union request it was transforming the single value into a list before sending it to the validator. When it was parsing a union type it wasn't doing that handling.Beta Was this translation helpful? Give feedback.
All reactions