You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
raisePydanticInvalidForJsonSchema(f'Cannot generate a JsonSchema for {error_info}')
pydantic.errors.PydanticInvalidForJsonSchema: CannotgenerateaJsonSchemaforcore_schema.PlainValidatorFunctionSchema ({'type': 'with-info', 'function': <boundmethodBaseModel.validateof<class'main.Foo'>>})
Example Code
fromtypingimportUnionfromfastapiimportFastAPIfrompydanticimportv1aspydantic_v1frompydanticimportBaseModelapp=FastAPI()
classFoo(pydantic_v1.BaseModel):
message: strclassBar(BaseModel):
message: str@app.post("/foo")defread_item(data: Foo):
data=data.dict()
return {"message": data['message']}
@app.post("/bar")defread_item(data: Bar):
data=data.dict()
return {"message": data['message']}
# Run the server with: uvicorn main:app --reload
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.
Initial Checks
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
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: