0

FastAPI throws TypeError: Object of type 'type' is not JSON serializable error on creating openapi.json when using a BaseModel which includes Field and a constr object as default.

A MRE:

from fastapi import FastAPI
from pydantic import BaseModel, Field, constr

app = FastAPI()


class CoolModel(BaseModel):
    this: str = Field(constr(strip_whitespace=True), min_length=10, max_length=20)


@app.get("/", response_model=CoolModel)
async def something(some_model: CoolModel):
    return some_model

Going to http://localhost:8000/openapi.json throws this:

INFO:     127.0.0.1:59046 - "GET /docs HTTP/1.1" 200 OK
INFO:     127.0.0.1:59046 - "GET /openapi.json HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py", line 373, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/fastapi/applications.py", line 208, in __call__
    await super().__call__(scope, receive, send)
  File "/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None
  File "/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc from None
  File "/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/routing.py", line 580, in __call__
    await route.handle(scope, receive, send)
  File "/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/routing.py", line 241, in handle
    await self.app(scope, receive, send)
  File "/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/starlette/routing.py", line 52, in app
    response = await func(request)
  File "/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/fastapi/applications.py", line 161, in openapi
    return JSONResponse(self.openapi())
  File "/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/fastapi/applications.py", line 136, in openapi
    self.openapi_schema = get_openapi(
  File "/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/fastapi/openapi/utils.py", line 387, in get_openapi
    definitions = get_model_definitions(
  File "/home/yash/.cache/pypoetry/virtualenvs/projects-api-qgm49pqx-py3.8/lib/python3.8/site-packages/fastapi/utils.py", line 24, in get_model_definitions
    m_schema, m_definitions, m_nested_models = model_process_schema(
  File "pydantic/schema.py", line 548, in pydantic.schema.model_process_schema
  File "pydantic/schema.py", line 589, in pydantic.schema.model_type_schema
  File "pydantic/schema.py", line 234, in pydantic.schema.field_schema
  File "pydantic/schema.py", line 202, in pydantic.schema.get_field_info_schema
  File "pydantic/schema.py", line 899, in pydantic.schema.encode_default
  File "pydantic/json.py", line 95, in pydantic.json.pydantic_encoder
TypeError: Object of type 'type' is not JSON serializable
Yash Nag
  • 1,096
  • 12
  • 16
  • I got around this by directly using `constr` and letting go of the `Field` completely. But would really like some input on what I'm doing wrong. – Yash Nag Nov 02 '21 at 14:39
  • 2
    The parameter to Field os the _default_ value of the Field. Since you're giving it a _type_ (the constr), it gets a type as its default value. A type is not a string and is not JSON serializable. – MatsLindh Nov 02 '21 at 14:45
  • @MatsLindh Thank you, that was helpful. – Yash Nag Nov 02 '21 at 18:13

0 Answers0