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.

Returning `dataclass` with pydantic-compatible fields (e.g. datetime) fails for FastAPI >= 0.67

See original GitHub issue

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • 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.

Example & Description

I have encountered an issue when returning dataclass responses with some pydantic-compatible (but incompatible with json encoder of Python standard library) fields, e.g. datetime objects or pathlib file paths.

Here’s a self-contained, minimal, reproducible, example where I have added a datetime field to the Item class in tests/test_serialize_response_dataclass.py:

from datetime import datetime
from fastapi import FastAPI
from fastapi.testclient import TestClient
from dataclasses import dataclass

app = FastAPI()


@dataclass
class Item:
    name: str
    date: datetime


@app.get("/items/no-response-model/object")
def get_no_response_model_object():
    return Item(name="object", date=datetime(2021, 7, 26))


client = TestClient(app)


def test_no_response_model_object():
    response = client.get("/items/no-response-model/object")
    response.raise_for_status()
    assert response.json() == {
        "name": "object",
        "date": datetime(2021, 7, 26).isoformat(),
    }

I would

  • Run the test code snippet with Pydantic 0.67
  • I would expect it pass because datetime objects are compatible with Pydantic models
  • It fails with the following stack trace:
Fail: TypeError: Object of type datetime is not JSON serializablepytest(./tests/test_serialize_response_dataclass_minimal_failing.py::test_no_response_model_object)
test_serialize_response_dataclass_minimal_failing.py(23, 5): test_no_response_model_object
test_serialize_response_dataclass_minimal_failing.py(24, 5): response = client.get("/items/no-response-model/object")
sessions.py(555, 9): return self.request('GET', url, **kwargs)
testclient.py(415, 9): return super().request(
sessions.py(542, 9): resp = self.send(prep, **send_kwargs)
sessions.py(655, 9): r = adapter.send(request, **kwargs)
testclient.py(243, 17): raise exc from None
testclient.py(240, 13): loop.run_until_complete(self.app(scope, receive, send))
base_events.py(642, 9): return future.result()
applications.py(199, 17): await super().__call__(scope, receive, send)
applications.py(112, 9): await self.middleware_stack(scope, receive, send)
errors.py(181, 13): raise exc from None
errors.py(159, 13): await self.app(scope, receive, _send)
exceptions.py(82, 17): raise exc from None
exceptions.py(71, 13): await self.app(scope, receive, sender)
routing.py(580, 17): await route.handle(scope, receive, send)
routing.py(241, 13): await self.app(scope, receive, send)
routing.py(52, 13): response = await func(request)
routing.py(243, 13): response = actual_response_class(response_data, **response_args)
responses.py(53, 9): self.body = self.render(content)
responses.py(161, 9): return json.dumps(
__init__.py(234, 5): return cls(
encoder.py(199, 9): chunks = self.iterencode(o, _one_shot=True)
encoder.py(257, 9): return _iterencode(o, 0)
encoder.py(179, 9): raise TypeError(f'Object of type {o.__class__.__name__} '

I have created a PR #3607 with an attempt to fix the issue and extend the existing tests in tests/test_serialize_response_dataclass.py. Without this PR, all tests in this updated file fail.

Note that the example above and the extended tests also pass with a pre 0.67 version of FastApi (before official dataclass response support).

Environment

  • OS: [e.g. Linux / Windows / macOS]: macOS
  • FastAPI Version [e.g. 0.3.0]: 0.67.0
  • Python version: 3.9.6

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
tiangolocommented, Aug 26, 2022

Thanks for the complete report! And thanks for the fix @himbeles in https://github.com/tiangolo/fastapi/pull/3607

This will be available in the next release, FastAPI 0.81.0 🚀 🎉

1reaction
dragooncommented, Aug 12, 2022

This is also important for me, and the PR already exists, let me know if you need any help on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Dataclasses - FastAPI
Using Dataclasses¶. FastAPI is built on top of Pydantic, and I have been showing you how to use Pydantic models to declare requests...
Read more >
FastAPI + SQLAlchemy - InvalidRequestError when posting ...
The documentation for mariadb's datetime states that 0 microseconds is the default datetime precision. This appears to be so in your db ...
Read more >
Attrs, Dataclasses and Pydantic - Stefan Scherfke
I've been using attrs for a long time now and I am really liking it. It is very flexible, has a nice API,...
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