Here is an MCVE with the same problem
This use dataclass, but i have the same problem with msgspec
Litestar version 2.21.1
from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING
from litestar import Litestar, get
from litestar.plugins.sqlalchemy import SQLAlchemyAsyncConfig, SQLAlchemyPlugin
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
if TYPE_CHECKING:
pass
class Base(DeclarativeBase): ...
class TodoItem(Base):
__tablename__ = "todo_item"
title: Mapped[str] = mapped_column(primary_key=True)
done: Mapped[bool]
@dataclass
class Person:
name: str
todo: TodoItem
@get("/person/{name:str}", sync_to_thread=False)
def get_person(name: str) -> Person:
# return Person(name=name)
return Person(name=name, todo=TodoItem(title="test", done=True))
config = SQLAlchemyAsyncConfig(
connection_string="sqlite+aiosqlite:///todo_async.sqlite",
create_all=True,
metadata=Base.metadata,
)
plugin = SQLAlchemyPlugin(config=config)
app = Litestar(route_handlers=[get_person], plugins=[plugin])
Then accessing "http://127.0.0.1:8000/person/test" create an
SerializationException on GET /person/test
Unsupported type: <class 'app.TodoItem'>