microsoft/autogen · error · ValueError

Dataclass has nested dataclasses or base models, which are n

Error message

Dataclass has nested dataclasses or base models, which are not supported. To use nested types, use a Pydantic model

What it means

DataclassJsonMessageSerializer rejects dataclasses that contain nested dataclass fields or nested pydantic BaseModel fields. Nested types require recursive serialization with embedded type information, which the flat dataclass JSON serializer does not implement, so its constructor raises ValueError and points to the Pydantic serializer, which handles nesting natively.

Source

Thrown at python/packages/autogen-core/src/autogen_core/_serialization.py:108


DataclassT = TypeVar("DataclassT", bound=IsDataclass)

JSON_DATA_CONTENT_TYPE = "application/json"
"""JSON data content type"""

# TODO: what's the correct content type? There seems to be some disagreement over what it should be
PROTOBUF_DATA_CONTENT_TYPE = "application/x-protobuf"
"""Protobuf data content type"""


class DataclassJsonMessageSerializer(MessageSerializer[DataclassT]):
    def __init__(self, cls: type[DataclassT]) -> None:
        if contains_a_union(cls):
            raise ValueError("Dataclass has a union type, which is not supported. To use a union, use a Pydantic model")

        if has_nested_dataclass(cls) or has_nested_base_model(cls):
            raise ValueError(
                "Dataclass has nested dataclasses or base models, which are not supported. To use nested types, use a Pydantic model"
            )

        self.cls = cls

    @property
    def data_content_type(self) -> str:
        return JSON_DATA_CONTENT_TYPE

    @property
    def type_name(self) -> str:
        return _type_name(self.cls)

    def deserialize(self, payload: bytes) -> DataclassT:
        message_str = payload.decode("utf-8")
        return self.cls(**json.loads(message_str))

    def serialize(self, message: DataclassT) -> bytes:

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Flatten the nested types into a single dataclass with primitive fields
  2. Convert the whole message tree to pydantic BaseModel and register PYDANTICJsonMessageSerializer (nested models supported)
  3. Write a custom MessageSerializer for this dataclass that serializes nested fields explicitly

Example fix

# before
@dataclass
class Inner:
    x: int
@dataclass
class Outer:
    inner: Inner

# after
class Inner(BaseModel):
    x: int
class Outer(BaseModel):
    inner: Inner
runtime.add_message_serializer(PYDANTICJsonMessageSerializer(Outer))
Defensive patterns

Strategy: validation

Validate before calling

import dataclasses, typing

def has_nested_types(cls) -> bool:
    for f in dataclasses.fields(cls):
        t = f.type
        for arg in typing.get_args(t) or (t,):
            if dataclasses.is_dataclass(arg) or (isinstance(arg, type) and arg.__name__ == 'BaseModel'):
                return True
    return False

assert not has_nested_types(Outer), "flatten or use pydantic"

Type guard

def is_flat_dataclass(cls) -> bool:
    import dataclasses, typing, pydantic
    ok = True
    for f in dataclasses.fields(cls):
        for arg in typing.get_args(f.type) or (f.type,):
            if dataclasses.is_dataclass(arg) or (isinstance(arg, type) and issubclass(arg, pydantic.BaseModel)):
                ok = False
    return ok

Try / catch

try:
    runtime.add_message_serializer(DataclassJsonMessageSerializer(Msg))
except ValueError as e:
    if "nested" in str(e):
        runtime.add_message_serializer(PYDANTICJsonMessageSerializer(to_pydantic(Msg)))
    else:
        raise

Prevention

When it happens

Trigger: Building DataclassJsonMessageSerializer(Outer) where Outer has a field annotated with another @dataclass class or a BaseModel subclass (including Optional[Inner] wrappers around a nested model). Registering such a message via MessageRegistry/dataclass helpers produces the same error at serializer construction.

Common situations: Composing message payloads out of smaller dataclasses; gradually pydantifying a codebase so a dataclass message picks up a BaseModel field; copying example messages that were written as nested structures.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/6073b378d7f7ddcb. Report an issue: GitHub.