A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/fastapi/fastapi/discussions/9709 below:

FastAPI with Pydantic v2 · fastapi/fastapi · Discussion #9709 · GitHub

The error message shown by a model validation error is worse in FastAPI 0.100.0 and above. For the following test code, the route will raise a 500 (Internal Server Error) since the data being returned cannot be validated by Pydantic.

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class RootSchema(BaseModel):
    foo: str

@app.get("/", response_model=RootSchema)
def get_root():
    return {}

In fastapi==0.99.1, the error message is:

INFO:     127.0.0.1:58889 - "GET / HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 408, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
    return await self.app(scope, receive, send)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/fastapi/applications.py", line 290, in __call__
    await super().__call__(scope, receive, send)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in __call__
    raise e
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in __call__
    await self.app(scope, receive, send)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/fastapi/routing.py", line 259, in app
    content = await serialize_response(
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/fastapi/routing.py", line 145, in serialize_response
    raise ValidationError(errors, field.type_)
pydantic.error_wrappers.ValidationError: 1 validation error for RootSchema
response -> foo
  field required (type=value_error.missing)

which is helpful as the error from pydantic is shown and can be used to debug the issue. However, in fastapi==0.100.0 and above, the error message is:

INFO:     127.0.0.1:58970 - "GET / HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 408, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
    return await self.app(scope, receive, send)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/fastapi/applications.py", line 289, in __call__
    await super().__call__(scope, receive, send)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/applications.py", line 122, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in __call__
    raise e
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in __call__
    await self.app(scope, receive, send)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/fastapi/routing.py", line 291, in app
    content = await serialize_response(
  File "/Users/harrylees/Documents/fastapi-test/venv/lib/python3.10/site-packages/fastapi/routing.py", line 154, in serialize_response
    raise ResponseValidationError(
fastapi.exceptions.ResponseValidationError

which doesn't show the error from Pydantic, and makes the error quite a bit harder to debug (in my opinion).


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4