Fastapi: Python 3.8.2 UnicodeDecodeError when running /redoc

Created on 3 Oct 2020  路  2Comments  路  Source: tiangolo/fastapi

First check

  • [x] I added a very descriptive title to this issue.
  • [x] I used the GitHub search to find a similar issue and didn't find it.
  • [x] I searched the FastAPI documentation, with the integrated search.
  • [x] I already searched in Google "How to X in FastAPI" and didn't find any information.
  • [x] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [x] I already checked if it is not related to FastAPI but to Pydantic.
  • [x] I already checked if it is not related to FastAPI but to Swagger UI.
  • [x] I already checked if it is not related to FastAPI but to ReDoc.
  • [x] After submitting this, I commit to one of:

    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.

    • I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.

    • Implement a Pull Request for a confirmed bug.

Error

INFO:     127.0.0.1:59446 - "GET /redoc HTTP/1.1" 200 OK
INFO:     127.0.0.1:59446 - "GET /openapi.json HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/ubuntu/git/rowmate/api/venv/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 390, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/home/ubuntu/git/rowmate/api/venv/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "/home/ubuntu/git/rowmate/api/venv/lib/python3.8/site-packages/fastapi/applications.py", line 179, in __call__
    await super().__call__(scope, receive, send)
  File "/home/ubuntu/git/rowmate/api/venv/lib/python3.8/site-packages/starlette/applications.py", line 111, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/home/ubuntu/git/rowmate/api/venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None
  File "/home/ubuntu/git/rowmate/api/venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/home/ubuntu/git/rowmate/api/venv/lib/python3.8/site-packages/starlette/middleware/cors.py", line 78, in __call__
    await self.app(scope, receive, send)
  File "/home/ubuntu/git/rowmate/api/venv/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc from None
  File "/home/ubuntu/git/rowmate/api/venv/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/home/ubuntu/git/rowmate/api/venv/lib/python3.8/site-packages/starlette/routing.py", line 566, in __call__
    await route.handle(scope, receive, send)
  File "/home/ubuntu/git/rowmate/api/venv/lib/python3.8/site-packages/starlette/routing.py", line 227, in handle
    await self.app(scope, receive, send)
  File "/home/ubuntu/git/rowmate/api/venv/lib/python3.8/site-packages/starlette/routing.py", line 41, in app
    response = await func(request)
  File "/home/ubuntu/git/rowmate/api/venv/lib/python3.8/site-packages/fastapi/applications.py", line 128, in openapi
    return JSONResponse(self.openapi())
  File "/home/ubuntu/git/rowmate/api/venv/lib/python3.8/site-packages/fastapi/applications.py", line 106, in openapi
    self.openapi_schema = get_openapi(
  File "/home/ubuntu/git/rowmate/api/venv/lib/python3.8/site-packages/fastapi/openapi/utils.py", line 343, in get_openapi
    definitions = get_model_definitions(
  File "/home/ubuntu/git/rowmate/api/venv/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 467, in pydantic.schema.model_process_schema
  File "pydantic/schema.py", line 503, in pydantic.schema.model_type_schema
  File "pydantic/schema.py", line 184, in pydantic.schema.field_schema
  File "pydantic/schema.py", line 767, in pydantic.schema.encode_default
  File "pydantic/json.py", line 60, in pydantic.json.pydantic_encoder
  File "pydantic/json.py", line 21, in pydantic.json.lambda
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 0: invalid start byte

Description

Basically I run http://localhost:8000/redoc and I get the above error in the terminal. I googled and searched in Github issues on FastAPI project and looked in Redoc and Pydantic issues but could not find any similar issue. I have no idea how to fix this and even nor if this really comes from FastAPI. But if one could have a look would be great.

Repository

Environment

  • Ubuntu 20.04.1 LTS x86_64
  • Kernel: 5.4.0-48-generic
  • FastAPI Version 0.61.1
  • Python Version: 3.8.2
  • Virtualenv Version 20.0.31
  • Pip Version 20.2.3
question

All 2 comments

Your issue is here: https://github.com/maurosbicego/rowmate/blob/master/api/models/boat.py#L14

```python
class Boat(BaseModel):
id: Binary = Binary(bytes(bytearray(uuid4().bytes)), UUID_SUBTYPE)
````

remove the default, and that should fix it (because pydantic tries to encode the binary uuid as a string for the openapi.json)

Thank you soooooo much, removing the default did help to run /redoc or /docs without issues.

I changed class Boat and others from

class Boat(BaseModel):
    id: Binary = Binary(bytes(bytearray(uuid4().bytes)), UUID_SUBTYPE)
class Boat(BaseModel):
    id: Binary

I'm going to close this issue.

Was this page helpful?
0 / 5 - 0 ratings