I'm trying to use Pydantic.BaseModel.model_dump() but when I call it AttributeError: type object 'BaseModel' has no attribute 'model_dump' raises. Also tried it instantiating the BaseModel class. The thing is that the vscode hint tool shows it as an available method to use, and when I use Pydantic.BaseModel.dict() it warns me that I should use model_dump().
I'm using Python 3.10.4
> pip freeze
annotated-types==0.6.0
anyio==3.7.1
exceptiongroup==1.1.3
fastapi==0.103.2
idna==3.4
pydantic==2.4.2
pydantic_core==2.10.1
sniffio==1.3.0
starlette==0.27.0
What's really strange is that when I do pydantic.__version__ in my FastApi web api, it returns v.1.10.2. What's going on here?
EDIT:
from typing import Optional
from fastapi import FastAPI
from fastapi.params import Body
from pydantic import BaseModel
import pydantic
app = FastAPI()
class Post(BaseModel):
title: str
content: str
published: bool = True # default value
rating: Optional[int] = None
@app.post("/createpost")
def create_posts(new_post: Post):
print('>>>>', pydantic.__version__)
print(pydantic.BaseModel.model_dump()) # => raises the error
# print(new_post.model_dump()) # => raises the error
return {"data": "new_post"}
(fastapi-course) ➜ fastapi-course uvicorn main:app --reload
INFO: Will watch for changes in these directories: ['/home/juanc/Documents/fastapi-course']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [76395] using WatchFiles
INFO: Started server process [76397]
INFO: Waiting for application startup.
INFO: Application startup complete.
'>>>>' 1.10.2
INFO: 127.0.0.1:40264 - "POST /createpost HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/home/juanc/.local/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 404, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "/home/juanc/.local/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
return await self.app(scope, receive, send)
File "/home/juanc/.local/lib/python3.8/site-packages/fastapi/applications.py", line 270, in __call__
await super().__call__(scope, receive, send)
File "/home/juanc/.local/lib/python3.8/site-packages/starlette/applications.py", line 124, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/juanc/.local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 184, in __call__
raise exc
File "/home/juanc/.local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 162, in __call__
await self.app(scope, receive, _send)
File "/home/juanc/.local/lib/python3.8/site-packages/starlette/middleware/exceptions.py", line 75, in __call__
raise exc
File "/home/juanc/.local/lib/python3.8/site-packages/starlette/middleware/exceptions.py", line 64, in __call__
await self.app(scope, receive, sender)
File "/home/juanc/.local/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
raise e
File "/home/juanc/.local/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "/home/juanc/.local/lib/python3.8/site-packages/starlette/routing.py", line 680, in __call__
await route.handle(scope, receive, send)
File "/home/juanc/.local/lib/python3.8/site-packages/starlette/routing.py", line 275, in handle
await self.app(scope, receive, send)
File "/home/juanc/.local/lib/python3.8/site-packages/starlette/routing.py", line 65, in app
response = await func(request)
File "/home/juanc/.local/lib/python3.8/site-packages/fastapi/routing.py", line 231, in app
raw_response = await run_endpoint_function(
File "/home/juanc/.local/lib/python3.8/site-packages/fastapi/routing.py", line 162, in run_endpoint_function
return await run_in_threadpool(dependant.call, **values)
File "/home/juanc/.local/lib/python3.8/site-packages/starlette/concurrency.py", line 41, in run_in_threadpool
return await anyio.to_thread.run_sync(func, *args)
File "/home/juanc/.local/lib/python3.8/site-packages/anyio/to_thread.py", line 28, in run_sync
return await get_asynclib().run_sync_in_worker_thread(func, *args, cancellable=cancellable,
File "/home/juanc/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 818, in run_sync_in_worker_thread
return await future
File "/home/juanc/.local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 754, in run
result = context.run(func, *args)
File "/home/juanc/Documents/fastapi-course/./main.py", line 26, in create_posts
print(pydantic.BaseModel.model_dump())
AttributeError: type object 'BaseModel' has no attribute 'model_dump'
You may have installed two versions of pydantic, to check you can run conda list and pip list to check the pydantic version.
How does it happen?
I get the same error with pydantic==2.5.3 installed by pip, but when I print pydantic.__version__, it shows 1.10.13. So I guess some packages may require pydantic==1.10.13, and it was automatically installed.
How to fix it?
Run pip uninstall pydantic TWICE, then run pip install -U pydantic.
Finally, let's print pydantic.__version__, now we got the right version of pydantic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With