[QUESTION] Am I doing something wrong with my models? Getting a validation error on my response.
See original GitHub issueDescription
I am getting a validation error on the response for my API calls that retrieve users, and I am not quite sure where to g in terms fixing it.
I am using pieces of the postgres full stack example.
Once I log into the api docs, I am able to make the request to retrieve user information, but the request does not complete. A validation exception is raised by pydantic for the response object.
I have included the error details below and I setup a repo that recreates what I am experiencing. I think I am just missing something simple. Thanks in advance for any assistance!
Example Repo: https://github.com/Rehket/API-Issue
Traceback
File "C:\Users\adama\.virtualenvs\TryFast\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 375, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "C:\Users\adama\.virtualenvs\TryFast\lib\site-packages\starlette\applications.py", line 133, in __call__
await self.error_middleware(scope, receive, send)
File "C:\Users\adama\.virtualenvs\TryFast\lib\site-packages\starlette\middleware\errors.py", line 177, in __call__
raise exc from None
File "C:\Users\adama\.virtualenvs\TryFast\lib\site-packages\starlette\middleware\errors.py", line 155, in __call__
await self.app(scope, receive, _send)
File "C:\Users\adama\.virtualenvs\TryFast\lib\site-packages\starlette\middleware\base.py", line 25, in __call__
response = await self.dispatch_func(request, self.call_next)
File "C:/Users/adama/Workspace/Python/TryFast/app/main.py", line 34, in db_session_middleware
response = await call_next(request)
File "C:\Users\adama\.virtualenvs\TryFast\lib\site-packages\starlette\middleware\base.py", line 45, in call_next
task.result()
File "C:\Users\adama\.virtualenvs\TryFast\lib\site-packages\starlette\middleware\base.py", line 38, in coro
await self.app(scope, receive, send)
File "C:\Users\adama\.virtualenvs\TryFast\lib\site-packages\starlette\middleware\cors.py", line 76, in __call__
await self.app(scope, receive, send)
File "C:\Users\adama\.virtualenvs\TryFast\lib\site-packages\starlette\exceptions.py", line 73, in __call__
raise exc from None
File "C:\Users\adama\.virtualenvs\TryFast\lib\site-packages\starlette\exceptions.py", line 62, in __call__
await self.app(scope, receive, sender)
File "C:\Users\adama\.virtualenvs\TryFast\lib\site-packages\starlette\routing.py", line 585, in __call__
await route(scope, receive, send)
File "C:\Users\adama\.virtualenvs\TryFast\lib\site-packages\starlette\routing.py", line 207, in __call__
await self.app(scope, receive, send)
File "C:\Users\adama\.virtualenvs\TryFast\lib\site-packages\starlette\routing.py", line 40, in app
response = await func(request)
File "C:\Users\adama\.virtualenvs\TryFast\lib\site-packages\fastapi\routing.py", line 122, in app
skip_defaults=response_model_skip_defaults,
File "C:\Users\adama\.virtualenvs\TryFast\lib\site-packages\fastapi\routing.py", line 54, in serialize_response
raise ValidationError(errors)
pydantic.error_wrappers.ValidationError: 1 validation error
response
value is not a valid dict (type=type_error.dict)
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Model validation error and test error - Cross Validated
No. First, there is always residual variance, so even if the model with the lowest validation error has the lowest expected prediction error ......
Read more >What is .Net Core Web Api Model Validation error response ...
Hi All,. I have made an .NET core web API endpoint that takes a model in parameter from body. I have purposely generated...
Read more >Santiago on Twitter: "High validation error indicates that our ...
A high bias indicates that our model is not powerful enough to learn the data. This is why our training error is high....
Read more >Raise a validation error in a model's save method in Django
It's fine to put validation in your model by using validators or writing a clean() method. All I was saying is that the...
Read more >4.4.4. How can I tell if a model fits my data?
Often the validation of a model seems to consist of nothing more than quoting the R^2 statistic from the fit (which measures the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Rehket I don’t think you should need to do that, but I’m not sure what is causing the issue. It might be a model config issue? (Maybe if you set
orm_mode = True
on the config it would help? I’m not sure.)It looks like a problem converting the sqlalchemy model to a pydantic model. Could you just test manually converting it to the pydantic model first? So at the end, rather than returning
user
(which is actually of type DBUser in your code), create a new (pydantic)User
fromuser
and return that.Whether it works or not, it will probably shed some light on the issue; we can try to figure out how to get it working as expected after that.