OpenAPI docs: PydanticSerializationError on IPv4Network model field #11885
-
First Check
Commit to Help
Example Codefrom fastapi import FastAPI
from ipaddress import IPv4Network
from pydantic import BaseModel, conint
from typing import List
app = FastAPI()
class PrefixListEntry(BaseModel):
prefix: IPv4Network # this standard library type will cause the error with Pydantic V2
ge: conint(ge=1, le=32) | None = None
le: conint(ge=1, le=32) | None = None
class PrefixListAdd(BaseModel):
name: str
entries: List[PrefixListEntry]
model_config = {
'json_schema_extra': {
'examples': [
{
'name': 'my-prefixlist',
'entries': [
PrefixListEntry(prefix='10.0.1.1').model_dump(), # note that I have to use model_dump()
PrefixListEntry(prefix='10.0.2.0/24', ge=26, le=31).model_dump() # note that I have to use model_dump()
]
}
]
}
}
@app.post('/prefixlist')
def post_entry(entry: PrefixListAdd):
passDescriptionThe issue happens when opening the It works on Pydantic V1 but fails on Pydantic V2. It may be related to:
Working example on Pydantic V1from fastapi import FastAPI
from ipaddress import IPv4Network
from pydantic import BaseModel, conint
from typing import List
app = FastAPI()
class PrefixListEntry(BaseModel):
prefix: IPv4Network # this standard library type will cause the error with Pydantic V2
ge: conint(ge=1, le=32) | None = None
le: conint(ge=1, le=32) | None = None
class PrefixListAdd(BaseModel):
name: str
entries: List[PrefixListEntry]
class Config:
schema_extra = {
'example': {
'name': 'my-prefixlist',
'entries': [
PrefixListEntry(prefix='10.0.1.1'),
PrefixListEntry(prefix='10.0.2.0/24', ge=26, le=31)
]
}
}
@app.post('/prefixlist')
def post_entry(entry: PrefixListAdd):
pass$ python -c "import pydantic.version; print(pydantic.version.version_info())"
pydantic version: 1.10.0
pydantic compiled: True
install path: /tmp/wksp/venv/lib/python3.10/site-packages/pydantic
python version: 3.10.12 (main, Mar 22 2024, 16:50:05) [GCC 11.4.0]
platform: Linux-6.5.0-41-generic-x86_64-with-glibc2.35
optional deps. installed: ['dotenv', 'email-validator', 'typing-extensions']$ uvicorn --reload api:app
INFO: Will watch for changes in these directories: ['/tmp/wksp']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [194811] using WatchFiles
INFO: Started server process [194813]
INFO: Waiting for application startup.
INFO: Application startup complete.Now go to: http://localhost:8000/openapi.json Log: Same (failing) example on Pydantic V2from fastapi import FastAPI
from ipaddress import IPv4Network
from pydantic import BaseModel, conint
from typing import List
app = FastAPI()
class PrefixListEntry(BaseModel):
prefix: IPv4Network # this standard library type will cause the error with Pydantic V2
ge: conint(ge=1, le=32) | None = None
le: conint(ge=1, le=32) | None = None
class PrefixListAdd(BaseModel):
name: str
entries: List[PrefixListEntry]
model_config = {
'json_schema_extra': {
'examples': [
{
'name': 'my-prefixlist',
'entries': [
PrefixListEntry(prefix='10.0.1.1').model_dump(), # note that I have to use model_dump()
PrefixListEntry(prefix='10.0.2.0/24', ge=26, le=31).model_dump() # note that I have to use model_dump()
]
}
]
}
}
@app.post('/prefixlist')
def post_entry(entry: PrefixListAdd):
pass$ python -c "import pydantic.version; print(pydantic.version.version_info())"
pydantic version: 2.8.2
pydantic-core version: 2.20.1
pydantic-core build: profile=release pgo=true
install path: /tmp/wksp/venv/lib/python3.10/site-packages/pydantic
python version: 3.10.12 (main, Mar 22 2024, 16:50:05) [GCC 11.4.0]
platform: Linux-6.5.0-41-generic-x86_64-with-glibc2.35
related packages: typing_extensions-4.12.2 fastapi-0.111.1
commit: unknown$ uvicorn --reload api:app
INFO: Will watch for changes in these directories: ['/tmp/wksp']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [194455] using WatchFiles
INFO: Started server process [194457]
INFO: Waiting for application startup.
INFO: Application startup complete.Now go to: http://localhost:8000/openapi.json Log and error: Operating SystemLinux Operating System DetailsNo response FastAPI Version0.111.1 Pydantic Version2.8.2 Python Version3.10.8 Additional ContextIn pydantic/pydantic#9957 (comment), @sydney-runkle suggested to use |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
|
@angely-dev I played a little with this and in the end I make a litle workoround with import json
...
model_config = {
'json_schema_extra': {
'examples': [
{
'name': 'my-prefixlist',
'entries': [
json.loads(PrefixListEntry(prefix='10.0.1.1').model_dump_json()), # <- changes
json.loads(PrefixListEntry(prefix='10.0.2.0/24', ge=26, le=31).model_dump_json()) # <- changes
]
}
]
}
}The result is this and it's pretty similar to your Pydantic V1 version. json_loads.webmSorry if I miss your point or this is not good enough. |
Beta Was this translation helpful? Give feedback.
-
|
I just found a better solution as per Pydantic doc:
Code: class PrefixListAdd(BaseModel):
name: str
entries: List[PrefixListEntry]
model_config = {
'json_schema_extra': {
'examples': [
{
'name': 'my-prefixlist',
'entries': [
PrefixListEntry(prefix='10.0.1.1').model_dump(mode="json"),
PrefixListEntry(prefix='10.0.2.0/24', ge=26, le=31).model_dump(mode="json")
]
}
]
}
}I hope it'll help others. |
Beta Was this translation helpful? Give feedback.

I just found a better solution as per Pydantic doc:
Code: