question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyError when building openapi file

See original GitHub issue

Describe the bug I deployed a service using FastAPI and Ormar on two different servers. On the first one it works, and on the second one I get this error when accessing /docs or /openapi.json (I believe most of the traceback is not useful):

Summary: (click to unfold the whole traceback)
  File "/api/pkgs/fastapi/utils.py", line 28, in get_model_definitions
    model_name = model_name_map[model]
                 β”‚              β”” <class 'abc.Team_PJX'>
                 β”” {<class 'abc.Team_TLK'>: 'Team_TLK', <enum 'TicketStatus'>: 'TicketStatus', <class 'abc.Project_RLS'>: 'Project_RLS', <class ...

KeyError: <class 'abc.Team_PJX'>
Exception in ASGI application

Traceback (most recent call last):

  File "/api/run.py", line 70, in <module>
    server.run()
    β”‚      β”” <function Server.run at 0x7fbf689e23a0>
    β”” <uvicorn.main.Server object at 0x7fbf6ea4bd00>

  File "/api/pkgs/uvicorn/main.py", line 419, in run
    loop.run_until_complete(self.serve(sockets=sockets))
    β”‚    β”‚                  β”‚    β”‚             β”” None
    β”‚    β”‚                  β”‚    β”” <function Server.serve at 0x7fbf689e2430>
    β”‚    β”‚                  β”” <uvicorn.main.Server object at 0x7fbf6ea4bd00>
    β”‚    β”” <function BaseEventLoop.run_until_complete at 0x7fbf6b0f3700>
    β”” <_UnixSelectorEventLoop running=True closed=False debug=False>

  File "/usr/lib64/python3.8/asyncio/base_events.py", line 603, in run_until_complete
    self.run_forever()
    β”‚    β”” <function BaseEventLoop.run_forever at 0x7fbf6b0f3670>
    β”” <_UnixSelectorEventLoop running=True closed=False debug=False>
  File "/usr/lib64/python3.8/asyncio/base_events.py", line 570, in run_forever
    self._run_once()
    β”‚    β”” <function BaseEventLoop._run_once at 0x7fbf6b0f61f0>
    β”” <_UnixSelectorEventLoop running=True closed=False debug=False>
  File "/usr/lib64/python3.8/asyncio/base_events.py", line 1859, in _run_once
    handle._run()
    β”‚      β”” <function Handle._run at 0x7fbf6b574f70>
    β”” <Handle <TaskWakeupMethWrapper object at 0x7fbf5c5aaee0>(<Future finished result=None>)>
  File "/usr/lib64/python3.8/asyncio/events.py", line 81, in _run
    self._context.run(self._callback, *self._args)
    β”‚    β”‚            β”‚    β”‚           β”‚    β”” <member '_args' of 'Handle' objects>
    β”‚    β”‚            β”‚    β”‚           β”” <Handle <TaskWakeupMethWrapper object at 0x7fbf5c5aaee0>(<Future finished result=None>)>
    β”‚    β”‚            β”‚    β”” <member '_callback' of 'Handle' objects>
    β”‚    β”‚            β”” <Handle <TaskWakeupMethWrapper object at 0x7fbf5c5aaee0>(<Future finished result=None>)>
    β”‚    β”” <member '_context' of 'Handle' objects>
    β”” <Handle <TaskWakeupMethWrapper object at 0x7fbf5c5aaee0>(<Future finished result=None>)>

  File "/api/pkgs/uvicorn/protocols/http/h11_impl.py", line 389, in run_asgi
    result = await app(self.scope, self.receive, self.send)
                   β”‚   β”‚    β”‚      β”‚    β”‚        β”‚    β”” <function RequestResponseCycle.send at 0x7fbf68768790>
                   β”‚   β”‚    β”‚      β”‚    β”‚        β”” <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x7fbf5c5aad90>
                   β”‚   β”‚    β”‚      β”‚    β”” <function RequestResponseCycle.receive at 0x7fbf68768820>
                   β”‚   β”‚    β”‚      β”” <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x7fbf5c5aad90>
                   β”‚   β”‚    β”” {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('172.42.56.164', 8080),...
                   β”‚   β”” <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x7fbf5c5aad90>
                   β”” <uvicorn.middleware.proxy_headers.ProxyHeadersMiddleware object at 0x7fbf687941f0>

  File "/api/pkgs/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
                 β”‚    β”‚   β”‚      β”‚        β”” <bound method RequestResponseCycle.send of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x7fbf5c5aad90>>
                 β”‚    β”‚   β”‚      β”” <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x7fbf5c5aad90>>
                 β”‚    β”‚   β”” {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('172.42.56.164', 8080),...
                 β”‚    β”” <fastapi.applications.FastAPI object at 0x7fbf66e39850>
                 β”” <uvicorn.middleware.proxy_headers.ProxyHeadersMiddleware object at 0x7fbf687941f0>

  File "/api/pkgs/fastapi/applications.py", line 199, in __call__
    await super().__call__(scope, receive, send)
                           β”‚      β”‚        β”” <bound method RequestResponseCycle.send of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x7fbf5c5aad90>>
                           β”‚      β”” <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x7fbf5c5aad90>>
                           β”” {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('172.42.56.164', 8080),...

  File "/api/pkgs/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
          β”‚    β”‚                β”‚      β”‚        β”” <bound method RequestResponseCycle.send of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x7fbf5c5aad90>>
          β”‚    β”‚                β”‚      β”” <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x7fbf5c5aad90>>
          β”‚    β”‚                β”” {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('172.42.56.164', 8080),...
          β”‚    β”” <starlette.middleware.errors.ServerErrorMiddleware object at 0x7fbf64fdd9d0>
          β”” <fastapi.applications.FastAPI object at 0x7fbf66e39850>

  File "/api/pkgs/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None

  File "/api/pkgs/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
          β”‚    β”‚   β”‚      β”‚        β”” <function ServerErrorMiddleware.__call__.<locals>._send at 0x7fbf64fdeee0>
          β”‚    β”‚   β”‚      β”” <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x7fbf5c5aad90>>
          β”‚    β”‚   β”” {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('172.42.56.164', 8080),...
          β”‚    β”” <starlette.middleware.base.BaseHTTPMiddleware object at 0x7fbf64fdd9a0>
          β”” <starlette.middleware.errors.ServerErrorMiddleware object at 0x7fbf64fdd9d0>

  File "/api/pkgs/starlette/middleware/base.py", line 25, in __call__
    response = await self.dispatch_func(request, self.call_next)
                     β”‚    β”‚             β”‚        β”‚    β”” <function BaseHTTPMiddleware.call_next at 0x7fbf66a819d0>
                     β”‚    β”‚             β”‚        β”” <starlette.middleware.base.BaseHTTPMiddleware object at 0x7fbf64fdd9a0>
                     β”‚    β”‚             β”” <starlette.requests.Request object at 0x7fbf640dd340>
                     β”‚    β”” <function PrometheusFastApiInstrumentator.instrument.<locals>.dispatch_middleware at 0x7fbf65196ee0>
                     β”” <starlette.middleware.base.BaseHTTPMiddleware object at 0x7fbf64fdd9a0>

  File "/api/pkgs/prometheus_fastapi_instrumentator/instrumentation.py", line 150, in dispatch_middleware
    raise e from None

  File "/api/pkgs/prometheus_fastapi_instrumentator/instrumentation.py", line 145, in dispatch_middleware
    response = await call_next(request)
                     β”‚         β”” <starlette.requests.Request object at 0x7fbf640dd340>
                     β”” <bound method BaseHTTPMiddleware.call_next of <starlette.middleware.base.BaseHTTPMiddleware object at 0x7fbf64fdd9a0>>

  File "/api/pkgs/starlette/middleware/base.py", line 45, in call_next
    task.result()
    β”‚    β”” <method 'result' of '_asyncio.Task' objects>
    β”” <Task finished name='Task-5469' coro=<BaseHTTPMiddleware.call_next.<locals>.coro() done, defined at /api/pkgs/starlette/middl...

  File "/api/pkgs/starlette/middleware/base.py", line 38, in coro
    await self.app(scope, receive, send)
          β”‚    β”‚   β”‚      β”‚        β”” <bound method Queue.put of <Queue at 0x7fbf64e0ea30 maxsize=0 tasks=1>>
          β”‚    β”‚   β”‚      β”” <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x7fbf5c5aad90>>
          β”‚    β”‚   β”” {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('172.42.56.164', 8080),...
          β”‚    β”” <starlette.middleware.cors.CORSMiddleware object at 0x7fbf64fdd970>
          β”” <starlette.middleware.base.BaseHTTPMiddleware object at 0x7fbf64fdd9a0>

  File "/api/pkgs/starlette/middleware/cors.py", line 78, in __call__
    await self.app(scope, receive, send)
          β”‚    β”‚   β”‚      β”‚        β”” <bound method Queue.put of <Queue at 0x7fbf64e0ea30 maxsize=0 tasks=1>>
          β”‚    β”‚   β”‚      β”” <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x7fbf5c5aad90>>
          β”‚    β”‚   β”” {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('172.42.56.164', 8080),...
          β”‚    β”” <starlette.exceptions.ExceptionMiddleware object at 0x7fbf64fdda60>
          β”” <starlette.middleware.cors.CORSMiddleware object at 0x7fbf64fdd970>

  File "/api/pkgs/starlette/exceptions.py", line 82, in __call__
    raise exc from None

  File "/api/pkgs/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
          β”‚    β”‚   β”‚      β”‚        β”” <function ExceptionMiddleware.__call__.<locals>.sender at 0x7fbf64a939d0>
          β”‚    β”‚   β”‚      β”” <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x7fbf5c5aad90>>
          β”‚    β”‚   β”” {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('172.42.56.164', 8080),...
          β”‚    β”” <fastapi.routing.APIRouter object at 0x7fbf657a9250>
          β”” <starlette.exceptions.ExceptionMiddleware object at 0x7fbf64fdda60>

  File "/api/pkgs/starlette/routing.py", line 580, in __call__
    await route.handle(scope, receive, send)
          β”‚     β”‚      β”‚      β”‚        β”” <function ExceptionMiddleware.__call__.<locals>.sender at 0x7fbf64a939d0>
          β”‚     β”‚      β”‚      β”” <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x7fbf5c5aad90>>
          β”‚     β”‚      β”” {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('172.42.56.164', 8080),...
          β”‚     β”” <function Route.handle at 0x7fbf66ad3b80>
          β”” <starlette.routing.Route object at 0x7fbf653fb5b0>

  File "/api/pkgs/starlette/routing.py", line 241, in handle
    await self.app(scope, receive, send)
          β”‚    β”‚   β”‚      β”‚        β”” <function ExceptionMiddleware.__call__.<locals>.sender at 0x7fbf64a939d0>
          β”‚    β”‚   β”‚      β”” <bound method RequestResponseCycle.receive of <uvicorn.protocols.http.h11_impl.RequestResponseCycle object at 0x7fbf5c5aad90>>
          β”‚    β”‚   β”” {'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.1'}, 'http_version': '1.1', 'server': ('172.42.56.164', 8080),...
          β”‚    β”” <function request_response.<locals>.app at 0x7fbf653fdee0>
          β”” <starlette.routing.Route object at 0x7fbf653fb5b0>

  File "/api/pkgs/starlette/routing.py", line 52, in app
    response = await func(request)
                     β”‚    β”” <starlette.requests.Request object at 0x7fbf5c5aa130>
                     β”” <function FastAPI.setup.<locals>.openapi at 0x7fbf653fdd30>

  File "/api/pkgs/fastapi/applications.py", line 152, in openapi
    return JSONResponse(self.openapi())
           β”‚            β”‚    β”” <function FastAPI.openapi at 0x7fbf66a91af0>
           β”‚            β”” <fastapi.applications.FastAPI object at 0x7fbf66e39850>
           β”” <class 'starlette.responses.JSONResponse'>

  File "/api/pkgs/fastapi/applications.py", line 130, in openapi
    self.openapi_schema = get_openapi(
    β”‚    β”‚                β”” <function get_openapi at 0x7fbf66a81430>
    β”‚    β”” None
    β”” <fastapi.applications.FastAPI object at 0x7fbf66e39850>

  File "/api/pkgs/fastapi/openapi/utils.py", line 363, in get_openapi
    definitions = get_model_definitions(
                  β”” <function get_model_definitions at 0x7fbf66b27af0>

  File "/api/pkgs/fastapi/utils.py", line 28, in get_model_definitions
    model_name = model_name_map[model]
                 β”‚              β”” <class 'abc.Team_PJX'>
                 β”” {<class 'abc.Team_TLK'>: 'Team_TLK', <enum 'TicketStatus'>: 'TicketStatus', <class 'abc.Project_RLS'>: 'Project_RLS', <class ...

KeyError: <class 'abc.Team_PJX'>

To Reproduce

I’m not sure how to reproduce this. As stated above, only one of two identical deployments fails with this error. I’ll see what happens when I redeploy the two services (if the failing one is fixed by the update) and report back. In the meanwhile maybe you’ll have a hunch πŸ™‚

Expected behavior No errors, openapi.json can be generated.

Versions (please complete the following information):

  • Database backend used: sqlite
  • Python version: 3.8
  • ormar version: 0.10.15
  • pydantic version: 1.8.2
  • if applicable fastapi version: 0.65.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
luisjimenez6245commented, Apr 26, 2022

Hey @pawamoy I was able to reproduce the issue.

The solution is trying to add cache to save and reuse the pydantic generated type. When fastapi tries to generate the openapi.json the method get_pydantic() is been called so many times, so there are some times where.join(choices(string.ascii_uppercase, k=3)) makes de exact same output so when this happen the Exception is raised.

1reaction
collerekcommented, Aug 24, 2021

Make sense. But without reproducibility, it will be hard to do anything about this πŸ˜…

Read more comments on GitHub >

github_iconTop Results From Across the Web

KeyError when generating openapi when using subclasses as ...
First check I added a very descriptive title to this issue. I used the GitHub search to find a similar issue and didn't...
Read more >
Gets duplicated mapping key error using openapi 3.0
You don't have any duplicate keys in your file, but it is invalid (i.e. not valid YAML) because of the indentation of the...
Read more >
The error message 'KeyError: key not found: "endpoint"' is ...
I have a Swagger/OpenAPI .json file that I wish to import. I am running the following command: Raw. $ 3scale import openapi -d...
Read more >
Build a RESTful API with Flask, MongoDB, and Python
There's no way to get PyMongo to return the document that was just inserted in a single operation (although an upsert using find_one_and_updateΒ ......
Read more >
Python KeyError Exceptions and How to Handle Them
In this tutorial, you'll learn how to handle Python KeyError exceptions. They are often caused by a bad key lookup in a dictionary,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found