Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Pydantic v2 #51

Closed
samikhailov opened this issue Nov 18, 2023 · 6 comments
Closed

Support Pydantic v2 #51

samikhailov opened this issue Nov 18, 2023 · 6 comments

Comments

@samikhailov
Copy link

An example doesn't work with Pydantic v2.

An error occurred:

/Users/soul/Documents/projects/fastapi-json-api/venv/bin/python /Users/soul/Documents/projects/fastapi-json-api/main.py 
Traceback (most recent call last):
  File "/Users/soul/Documents/projects/fastapi-json-api/main.py", line 12, in <module>
    from fastapi_jsonapi import RoutersJSONAPI, init
  File "/Users/soul/Documents/projects/fastapi-json-api/venv/lib/python3.11/site-packages/fastapi_jsonapi/__init__.py", line 5, in <module>
    from fastapi_jsonapi.api import RoutersJSONAPI
  File "/Users/soul/Documents/projects/fastapi-json-api/venv/lib/python3.11/site-packages/fastapi_jsonapi/api.py", line 23, in <module>
    from fastapi_jsonapi.schema_builder import SchemaBuilder
  File "/Users/soul/Documents/projects/fastapi-json-api/venv/lib/python3.11/site-packages/fastapi_jsonapi/schema_builder.py", line 19, in <module>
    from pydantic.fields import FieldInfo, ModelField
ImportError: cannot import name 'ModelField' from 'pydantic.fields' (/Users/soul/Documents/projects/fastapi-json-api/venv/lib/python3.11/site-packages/pydantic/fields.py)

Dependencies:

annotated-types==0.6.0
anyio==3.7.1
click==8.1.7
fastapi==0.104.1
FastAPI-JSONAPI==2.3.2
greenlet==3.0.1
h11==0.14.0
idna==3.4
pydantic==2.5.1
pydantic_core==2.14.3
simplejson==3.19.2
sniffio==1.3.0
SQLAlchemy==2.0.23
starlette==0.27.0
typing_extensions==4.8.0
uvicorn==0.24.0.post1

Do you plan to support Pydantic v2?

@mahenzon
Copy link
Member

mahenzon commented Dec 7, 2023

Hey there! Sure, we plan to support FastAPI 0.100+ and Pydantic V2

@DavidRomanovizc
Copy link

Hi, @mahenzon, I'd like to try to take on resolving this issue. Could you provide additional information if needed, and could you assign me to this issue? Regarding the additional information, At the moment, I have a couple of questions: the current version of the library uses pydantic v1. Do you plan to add support for pydantic v2 while maintaining backward compatibility? Or is it necessary to add pydantic v2 without preserving support for pydantic v1?

@mahenzon
Copy link
Member

Hi @DavidRomanovizc, great news!

Nope, backward compatibility is not planned. There'll be a new release 3.0 with FastAPI 0.100+ and Pydantic 2+

I tried to migrate using the official migration script, but there were a lot of errors, here's the branch:
https://github.com/mts-ai/FastAPI-JSONAPI/tree/upgrade-fastapi-and-pydantic

Also there were some problems with annotations. For example atomic operations list has to be of length 1 or more, I could not find new annotation validator for this case.

And a strange one: schemas are being serialized with model_config: it's serialized to json with all other attributes. Probably it's because schemas generation is kinda overcomplicated. I could not find a quick solution.

We have decent tests coverage, so a lot of problems can be detected automatically.

Feel free to open new PRs, I've created a new branch for version 3.x https://github.com/mts-ai/FastAPI-JSONAPI/tree/dev-3.x

@kmbhm1
Copy link

kmbhm1 commented Jun 25, 2024

Hello there! This upgrade would be extremely valuable for a project I'm currently working on. Any updates on when it might be ready or how to contribute? Thanks!

@mahenzon
Copy link
Member

mahenzon commented Jun 25, 2024

@kmbhm1 hello there!
Technically, Pydantic V2 support is ready, but I need to make sure it works on older Python versions, I need to update examples and all docs, and also I'd like to update some legacy stuff

Here it is: #88

It'll be cool, if you can check if it works for you in this state. Feel free to report any issues

@m-antonov
Copy link
Collaborator

m-antonov commented Mar 13, 2025

@samikhailov @kmbhm1 thank you for your patience. I invite you to test the updated version of the library #97. The update was applied not only to the Pydantic library but also included version upgrades for SQLAlchemy and FastAPI. Performance has more than doubled; for example, with 50 users, the average response time decreased from 302 ms to 138 ms. Feel free to open a new issue if you encounter any problems with the updated version. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants