Skip to content

V1 models for response and body#10223

Closed
chbndrhnns wants to merge 13 commits intofastapi:masterfrom
chbndrhnns:v1-response-model
Closed

V1 models for response and body#10223
chbndrhnns wants to merge 13 commits intofastapi:masterfrom
chbndrhnns:v1-response-model

Conversation

@chbndrhnns
Copy link
Copy Markdown

No description provided.

Comment thread fastapi/_compat.py
exclude_none=exclude_none,
)
except AttributeError:
return [
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if there is a better way to handle the case where there is a container of model instances.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isinstance()?

if isinstance(value, dict):
    ...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least for Python 3.11+, I expect its zero-overhead exception handling to be a bit more efficient. In case that's neglectable, an if/else branch with isinstance might be more readable.

@mjspeck
Copy link
Copy Markdown

mjspeck commented Oct 4, 2023

Any progress on this? Looks like it's been unchanged for almost a month.

@mokeyish

This comment was marked as outdated.

@conglei
Copy link
Copy Markdown

conglei commented Dec 23, 2023

Any update on this PR? I'm also blocked by the same issue.

@tiangolo tiangolo added feature New feature or request p2 labels Jan 14, 2024
@aviau
Copy link
Copy Markdown

aviau commented Mar 21, 2024

This would really help large projects migrate to pydantic v2!

@YanSte
Copy link
Copy Markdown

YanSte commented Apr 5, 2024

Any update ?

@zouyi100
Copy link
Copy Markdown

Any update?

1 similar comment
@jabbapen
Copy link
Copy Markdown

jabbapen commented May 5, 2024

Any update?

@fraser-langton
Copy link
Copy Markdown

Any update on this? FastAPI still supports Pydantic v1 is a fundamentally incorrect statement while this remains open, and we will not upgrade to pydantic v2 or fastapi 0.100 while this remains open. Otherwise there is no path to a smooth transition to v2, the entire repo must be upgraded in one go, defeating the purpose behind pydantic.v1 usage.

https://fastapi.tiangolo.com/release-notes/#pydantic-v1

This version [0.100.0] of FastAPI still supports Pydantic v1. And although Pydantic v1 will be deprecated at some point, it will still be supported for a while.

This means that you can install the new Pydantic v2, and if something fails, you can install Pydantic v1 while you fix any problems you might have, but having the latest FastAPI.

@shanumas
Copy link
Copy Markdown

shanumas commented Jul 6, 2024

Any update ?

@vfazio
Copy link
Copy Markdown

vfazio commented Jul 18, 2024

Any update on this? FastAPI still supports Pydantic v1 is a fundamentally incorrect statement while this remains open, and we will not upgrade to pydantic v2 or fastapi 0.100 while this remains open. Otherwise there is no path to a smooth transition to v2, the entire repo must be upgraded in one go, defeating the purpose behind pydantic.v1 usage.

https://fastapi.tiangolo.com/release-notes/#pydantic-v1

This version [0.100.0] of FastAPI still supports Pydantic v1. And although Pydantic v1 will be deprecated at some point, it will still be supported for a while.
This means that you can install the new Pydantic v2, and if something fails, you can install Pydantic v1 while you fix any problems you might have, but having the latest FastAPI.

@tiangolo thoughts on this?

@phillipuniverse
Copy link
Copy Markdown

Looks like this will work for serialization/deserialization but needs additional work for schema generation:

../../../Library/Caches/pypoetry/virtualenvs/shipwell-common-python-zjZyLK7l-py3.11/lib/python3.11/site-packages/fastapi/openapi/utils.py:475: in get_openapi
    field_mapping, definitions = get_definitions(
../../../Library/Caches/pypoetry/virtualenvs/shipwell-common-python-zjZyLK7l-py3.11/lib/python3.11/site-packages/fastapi/_compat.py:230: in get_definitions
    field_mapping, definitions = schema_generator.generate_definitions(
../../../Library/Caches/pypoetry/virtualenvs/shipwell-common-python-zjZyLK7l-py3.11/lib/python3.11/site-packages/pydantic/json_schema.py:379: in generate_definitions
    self.generate_inner(schema)
../../../Library/Caches/pypoetry/virtualenvs/shipwell-common-python-zjZyLK7l-py3.11/lib/python3.11/site-packages/pydantic/json_schema.py:552: in generate_inner
    json_schema = current_handler(schema)
../../../Library/Caches/pypoetry/virtualenvs/shipwell-common-python-zjZyLK7l-py3.11/lib/python3.11/site-packages/pydantic/_internal/_schema_generation_shared.py:37: in __call__
    return self.handler(core_schema)
../../../Library/Caches/pypoetry/virtualenvs/shipwell-common-python-zjZyLK7l-py3.11/lib/python3.11/site-packages/pydantic/json_schema.py:511: in handler_func
    json_schema = generate_for_schema_type(schema_or_field)
../../../Library/Caches/pypoetry/virtualenvs/shipwell-common-python-zjZyLK7l-py3.11/lib/python3.11/site-packages/pydantic/json_schema.py:1229: in chain_schema
    return self.generate_inner(schema['steps'][step_index])
../../../Library/Caches/pypoetry/virtualenvs/shipwell-common-python-zjZyLK7l-py3.11/lib/python3.11/site-packages/pydantic/json_schema.py:552: in generate_inner
    json_schema = current_handler(schema)
../../../Library/Caches/pypoetry/virtualenvs/shipwell-common-python-zjZyLK7l-py3.11/lib/python3.11/site-packages/pydantic/_internal/_schema_generation_shared.py:37: in __call__
    return self.handler(core_schema)
../../../Library/Caches/pypoetry/virtualenvs/shipwell-common-python-zjZyLK7l-py3.11/lib/python3.11/site-packages/pydantic/json_schema.py:511: in handler_func
    json_schema = generate_for_schema_type(schema_or_field)
../../../Library/Caches/pypoetry/virtualenvs/shipwell-common-python-zjZyLK7l-py3.11/lib/python3.11/site-packages/pydantic/json_schema.py:1014: in function_plain_schema
    return self.handle_invalid_for_json_schema(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
self = <pydantic.json_schema.GenerateJsonSchema object at 0x113e3f0d0>
schema = {'function': {'function': <bound method BaseModel.validate of <class 'tests.contrib.fastapi.test_shipwell_fastapi_extension.shipwell_fast_api.<locals>.Price'>>, 'type': 'with-info'}, 'metadata': {}, 'type': 'function-plain'}
error_info = "core_schema.PlainValidatorFunctionSchema ({'type': 'with-info', 'function': <bound method BaseModel.validate of <class 'tests.contrib.fastapi.test_shipwell_fastapi_extension.shipwell_fast_api.<locals>.Price'>>})"
    def handle_invalid_for_json_schema(self, schema: CoreSchemaOrField, error_info: str) -> JsonSchemaValue:
>       raise PydanticInvalidForJsonSchema(f'Cannot generate a JsonSchema for {error_info}')
E       pydantic.errors.PydanticInvalidForJsonSchema: Cannot generate a JsonSchema for core_schema.PlainValidatorFunctionSchema ({'type': 'with-info', 'function': <bound method BaseModel.validate of <class 'tests.contrib.fastapi.test_shipwell_fastapi_extension.shipwell_fast_api.<locals>.Price'>>})
E       
E       For further information visit https://errors.pydantic.dev/2.9/u/invalid-for-json-schema

In this case the v2 schema generation is looking at the validate method on pydantic.v1.BaseModel and trying to generate a schema for it.

@YuriiMotov
Copy link
Copy Markdown
Member

@chbndrhnns, thanks for you contribution!
Are you still ready to work on it?

Or, is there anybody who is willing to take this over and work on it?

@chbndrhnns
Copy link
Copy Markdown
Author

For now, I updated the code to have passing tests.
Looking into schema generation now.

@github-actions github-actions Bot removed the waiting label Jul 18, 2025
@chbndrhnns
Copy link
Copy Markdown
Author

Schema generation seems a bit more tricky:

FastAPI now needs to handle both Pydantic v1 and v2 models simultaneously when running with Pydantic v2 installed. This is fundamentally different from the previous situation:

Before: FastAPI only needed to handle one version of Pydantic models (v1)
Now: FastAPI needs to handle both v1 and v2 models in the same application

@chbndrhnns
Copy link
Copy Markdown
Author

@YuriiMotov How do you suggest to proceed here?

@github-actions github-actions Bot added the conflicts Automatically generated when a PR has a merge conflict label Oct 8, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Oct 8, 2025

This pull request has a merge conflict that needs to be resolved.

@chbndrhnns
Copy link
Copy Markdown
Author

@YuriiMotov I am willing to rebase if we have a clear path to merging

@YuriiMotov
Copy link
Copy Markdown
Member

@YuriiMotov I am willing to rebase if we have a clear path to merging

I think there is no need for this now.
Let's wait for Sebastian to review what we have here and decide if it's the right way to go

@tiangolo
Copy link
Copy Markdown
Member

Thanks for the interest and effort here! This was covered by #14168, which was just released in FastAPI 0.119.0. 🎉

That adds support for pydantic.v1 models at the same time as v2 models. The new docs are here: https://fastapi.tiangolo.com/how-to/migrate-from-pydantic-v1-to-pydantic-v2/

Given that, I'll close this one now. Thanks for the effort and interest and thanks all for the discussions! 🍰

@tiangolo tiangolo closed this Oct 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conflicts Automatically generated when a PR has a merge conflict feature New feature or request p2

Projects

None yet

Development

Successfully merging this pull request may close these issues.