[Python] Call to FastAPI "/openapi.json" endpoint failing. Cannot see swagger-ui docs

Discussão em 'Python' iniciado por Stack, Abril 19, 2025.

  1. Stack

    Stack Membro Participativo

    I am starting a tiny little Python project to refamiliarize myself with FastAPI and I am running into a problem accessing the swagger ui docs. The endpoint itself, which I stripped down to the barest possible, works fine. When I try to hit /docs though, the subsequent call to /openapi.json fails. I swear I must be missing some incredibly stupid mistake and would appreciate any help.

    My minimal "program":

    import uvicorn
    from fastapi import FastAPI

    app = FastAPI()

    @app.get("/")
    def root():
    return {"message": "This is the root of the project"}

    if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)


    When I try to hit /docs that call itself is a 200, but when it tries to get /openapi.json it returns a 500 and the following error in the console. Obviously the docs thus do not show up in the browser.

    INFO: 127.0.0.1:51857 - "GET /openapi.json HTTP/1.1" 500 Internal Server Error
    ERROR: Exception in ASGI application
    Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
    result = await app( # type: ignore[func-returns-value]
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/fastapi/applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/middleware/errors.py", line 187, in __call__
    raise exc
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/middleware/errors.py", line 165, in __call__
    await self.app(scope, receive, _send)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    raise exc
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/routing.py", line 714, in __call__
    await self.middleware_stack(scope, receive, send)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/routing.py", line 734, in app
    await route.handle(scope, receive, send)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/routing.py", line 288, in handle
    await self.app(scope, receive, send)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/routing.py", line 76, in app
    await wrap_app_handling_exceptions(app, request)(scope, receive, send)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/_exception_handler.py", line 53, in wrapped_app
    raise exc
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/_exception_handler.py", line 42, in wrapped_app
    await app(scope, receive, sender)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/starlette/routing.py", line 73, in app
    response = await f(request)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/fastapi/applications.py", line 1009, in openapi
    return JSONResponse(self.openapi())
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/fastapi/applications.py", line 981, in openapi
    self.openapi_schema = get_openapi(
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/fastapi/openapi/utils.py", line 513, in get_openapi
    schema_generator = GenerateJsonSchema(ref_template=REF_TEMPLATE)
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pydantic/json_schema.py", line 278, in __init__
    self._schema_type_to_method = self.build_schema_type_to_method()
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pydantic/json_schema.py", line 317, in build_schema_type_to_method
    method_name = f'{key.replace("-", "_")}_schema'
    File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 647, in __getattr__
    return getattr(self.__origin__, attr)
    AttributeError: '_SpecialForm' object has no attribute 'replace'

    Continue reading...

Compartilhe esta Página