Reputation: 14701
You have a simple pydantic model with datetime attribute, like below:
from datetime import datetime
from pydantic import BaseModel
class Model(BaseModel):
dt: datetime = datetime.now
When you try to serialize this model to json, that try to return it from fastapi end point. You get following error.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:52831 - "POST /yard-manager/check_rule?id_request=1 HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\anyio\streams\memory.py", line 94, in receive
return self.receive_nowait()
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\anyio\streams\memory.py", line 89, in receive_nowait
raise WouldBlock
anyio.WouldBlock
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\starlette\middleware\base.py", line 43, in call_next
message = await recv_stream.receive()
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\anyio\streams\memory.py", line 114, in receive
raise EndOfStream
anyio.EndOfStream
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 404, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 78, in __call__
return await self.app(scope, receive, send)
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\fastapi\applications.py", line 269, in __call__
await super().__call__(scope, receive, send)
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\starlette\applications.py", line 124, in __call__
await self.middleware_stack(scope, receive, send)
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\starlette\middleware\errors.py", line 184, in __call__
raise exc
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\starlette\middleware\errors.py", line 162, in __call__
await self.app(scope, receive, _send)
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\starlette\middleware\cors.py", line 92, in __call__
await self.simple_response(scope, receive, send, request_headers=headers)
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\starlette\middleware\cors.py", line 147, in simple_response
await self.app(scope, receive, send)
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\starlette\middleware\base.py", line 68, in __call__
response = await self.dispatch_func(request, call_next)
File "c:\Users\ati_o\Projects\sms-digital\SSAB\ssab-ox-mes-pss-alg\rest_service\middleware.py", line 36, in check_header_token
response = await call_next(request)
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\starlette\middleware\base.py", line 46, in call_next
raise app_exc
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\starlette\middleware\base.py", line 36, in coro
await self.app(scope, request.receive, send_stream.send)
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\starlette\exceptions.py", line 93, in __call__
raise exc
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\starlette\exceptions.py", line 82, in __call__
await self.app(scope, receive, sender)
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 21, in __call__
raise e
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\starlette\routing.py", line 670, in __call__
await route.handle(scope, receive, send)
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\starlette\routing.py", line 266, in handle
await self.app(scope, receive, send)
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\starlette\routing.py", line 65, in app
response = await func(request)
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\fastapi\routing.py", line 249, in app
content = await serialize_response(
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\fastapi\routing.py", line 149, in serialize_response
return jsonable_encoder(response_content)
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\fastapi\encoders.py", line 157, in jsonable_encoder
return jsonable_encoder(
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\fastapi\encoders.py", line 113, in jsonable_encoder
encoded_value = jsonable_encoder(
File "WINDOWS_USERNAME\scoop\apps\python310\current\lib\site-packages\fastapi\encoders.py", line 156, in jsonable_encoder
raise ValueError(errors)
ValueError: [TypeError("'builtin_function_or_method' object is not iterable"), TypeError('vars() argument must have __dict__ attribute')]
Upvotes: 2
Views: 2228
Reputation: 14701
You are assigning a function to your field not a value. That is you are assigning datetime.now function to your field, not a current date and time value. Therefore, pydantic cannot serialize it. Change your default value like below to call now as function like now()
from datetime import datetime
from pydantic import BaseModel
class Model(BaseModel):
dt: datetime = datetime.now()
Simple, but when you do not know a framework, it could be a stopper.
Upvotes: 3