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

Openapi.json does not support pydantic v1 and pydantic v2 at the same time #9363

Closed
1 task done
ellyx13 opened this issue May 1, 2024 · 1 comment
Closed
1 task done
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation

Comments

@ellyx13
Copy link

ellyx13 commented May 1, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I am using FastAPI 0.110.3 and pydantic v2.7.1. Previously I used pydantic v1 and am planning to migrate to pydantic v2. But I have a problem that FastAPI cannot declare 2 api at the same time using BaseModel of pydantic v1 and pydantic v2.

I did some research and found out that pydantic v1 uses openapi.json 3.0 and pydantic v2 uses openapi.json 3.1, so I think the two versions of openapi.json conflict with each other.

I can't migrate the entire pydantic v1 code to v2 at once because it would take a lot of time and I can't release new features of the project, so I want to use pydantic v1 and v2 in parallel, and migrate slowly. pydantic code v1 to v2.

Error received

raise PydanticInvalidForJsonSchema(f'Cannot generate a JsonSchema for {error_info}')
pydantic.errors.PydanticInvalidForJsonSchema: Cannot generate a JsonSchema for core_schema.PlainValidatorFunctionSchema ({'type': 'with-info', 'function': <bound method BaseModel.validate of <class 'main.Foo'>>})

Example Code

from typing import Union
from fastapi import FastAPI
from pydantic import v1 as pydantic_v1
from pydantic import BaseModel

app = FastAPI()

class Foo(pydantic_v1.BaseModel):
    message: str

class Bar(BaseModel):
    message: str



@app.post("/foo")
def read_item(data: Foo):
    data = data.dict()
    return {"message": data['message']}

@app.post("/bar")
def read_item(data: Bar):
    data = data.dict()
    return {"message": data['message']}

# Run the server with: uvicorn main:app --reload

Python, Pydantic & OS Version

pydantic version: 2.7.1
pydantic-core version: 2.18.2
pydantic-core build: profile=release pgo=true
install path: /home/kietpham/python/pydantic/venv/lib/python3.10/site-packages/pydantic
python version: 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]
platform: Linux-5.15.146.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
related packages: fastapi-0.110.3 typing_extensions-4.11.0
commit: unknown

Library version from pip freeze:
annotated-types==0.6.0
anyio==4.3.0
click==8.1.7
exceptiongroup==1.2.1
fastapi==0.110.3
h11==0.14.0
httptools==0.6.1
idna==3.7
pydantic==2.7.1
pydantic_core==2.18.2
python-dotenv==1.0.1
PyYAML==6.0.1
sniffio==1.3.1
starlette==0.37.2
typing_extensions==4.11.0
uvicorn==0.29.0
uvloop==0.19.0
watchfiles==0.21.0
websockets==12.0
@ellyx13 ellyx13 added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels May 1, 2024
@sydney-runkle
Copy link
Member

Hi @ellyx13,

Sorry that you're experiencing this issue. I think this also relates to the issue re the fact that v1 and v2 models can't mix (ex - you can't have a nested v1 model within a v2 model).

Looks like this issue specifically is arising in the context of FastAPI usage with both versions, which isn't supported. I'd recommend opening a similar issue on that side of things to see if v1/v2 cross compatibility could potentially be reintroduced.

Closing as not planned on pydantic for now.

@sydney-runkle sydney-runkle closed this as not planned Won't fix, can't repro, duplicate, stale May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation
Projects
None yet
Development

No branches or pull requests

2 participants