title: fastapi custom response class as default response class
body: <p>I am trying to use a custom response class as <a href="https://fastapi.tiangolo.com/advanced/custom-response/#default-response-class" rel="nofollow noreferrer">default response</a>.</p>
  <pre class="lang-py prettyprint-override"><code>from fastapi.responses import Response
  from bson.json_util import dumps
  
  class MongoResponse(Response):
      def __init__(self, content, *args, **kwargs):
          super().__init__(
              content=dumps(content),
              media_type=&quot;application/json&quot;,
              *args,
              **kwargs,
          )
  </code></pre>
  <p>This works fine when I explicitly use the response class.</p>
  <pre class="lang-py prettyprint-override"><code>@app.get(&quot;/&quot;)
  async def getDoc():
      foo = client.get_database('foo')
      result = await foo.bar.find_one({'author': 'fool'})
      return MongoResponse(result)
  </code></pre>
  <p>However, when I try to pass this as argument to the FastAPI constructor, it doesn't seem to be used when only returning the data from the request handler.</p>
  <pre class="lang-py prettyprint-override"><code>app = FastAPI(default_response_class=MongoResponse)
  
  @app.get(&quot;/&quot;)
  async def getDoc():
      foo = client.get_database('foo')
      result = await foo.bar.find_one({'author': 'fool'})
      return result
  </code></pre>
  <p>When I look at below stack trace, it seems like its still using the normal default response which is the <a href="https://fastapi.tiangolo.com/advanced/custom-response/#jsonresponse" rel="nofollow noreferrer">json response</a>.</p>
  <pre class="lang-sh prettyprint-override"><code>ERROR:    Exception in ASGI application
  Traceback (most recent call last):
    File &quot;/home/blue/podman/test/.venv/lib/python3.6/site-packages/uvicorn/protocols/http/httptools_impl.py&quot;, line 390, in run_asgi
      result = await app(self.scope, self.receive, self.send)
    File &quot;/home/blue/podman/test/.venv/lib/python3.6/site-packages/uvicorn/middleware/proxy_headers.py&quot;, line 45, in __call__
      return await self.app(scope, receive, send)
    File &quot;/home/blue/podman/test/.venv/lib/python3.6/site-packages/fastapi/applications.py&quot;, line 181, in __call__
      await super().__call__(scope, receive, send)  # pragma: no cover
    File &quot;/home/blue/podman/test/.venv/lib/python3.6/site-packages/starlette/applications.py&quot;, line 111, in __call__
      await self.middleware_stack(scope, receive, send)
    File &quot;/home/blue/podman/test/.venv/lib/python3.6/site-packages/starlette/middleware/errors.py&quot;, line 181, in __call__
      raise exc from None
    File &quot;/home/blue/podman/test/.venv/lib/python3.6/site-packages/starlette/middleware/errors.py&quot;, line 159, in __call__
      await self.app(scope, receive, _send)
    File &quot;/home/blue/podman/test/.venv/lib/python3.6/site-packages/starlette/exceptions.py&quot;, line 82, in __call__
      raise exc from None
    File &quot;/home/blue/podman/test/.venv/lib/python3.6/site-packages/starlette/exceptions.py&quot;, line 71, in __call__
      await self.app(scope, receive, sender)
    File &quot;/home/blue/podman/test/.venv/lib/python3.6/site-packages/starlette/routing.py&quot;, line 566, in __call__
      await route.handle(scope, receive, send)
    File &quot;/home/blue/podman/test/.venv/lib/python3.6/site-packages/starlette/routing.py&quot;, line 227, in handle
      await self.app(scope, receive, send)
    File &quot;/home/blue/podman/test/.venv/lib/python3.6/site-packages/starlette/routing.py&quot;, line 41, in app
      response = await func(request)
    File &quot;/home/blue/podman/test/.venv/lib/python3.6/site-packages/fastapi/routing.py&quot;, line 199, in app
      is_coroutine=is_coroutine,
    File &quot;/home/blue/podman/test/.venv/lib/python3.6/site-packages/fastapi/routing.py&quot;, line 122, in serialize_response
      return jsonable_encoder(response_content)
    File &quot;/home/blue/podman/test/.venv/lib/python3.6/site-packages/fastapi/encoders.py&quot;, line 94, in jsonable_encoder
      sqlalchemy_safe=sqlalchemy_safe,
    File &quot;/home/blue/podman/test/.venv/lib/python3.6/site-packages/fastapi/encoders.py&quot;, line 139, in jsonable_encoder
      raise ValueError(errors)
  ValueError: [TypeError(&quot;'ObjectId' object is not iterable&quot;,), TypeError('vars() argument must have __dict__ attribute',)]
  
  </code></pre>