{"record":{"id":"e3e31755e1132f45","repo":"microsoft/semantic-kernel","slug":"dataclass-has-nested-dataclasses-or-base-models-w","errorCode":null,"errorMessage":"Dataclass has nested dataclasses or base models, which are not supported. To use nested types, use a Pydantic model","messagePattern":"Dataclass has nested dataclasses or base models, which are not supported\\. To use nested types, use a Pydantic model","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/semantic_kernel/agents/runtime/core/serialization.py","lineNumber":136,"sourceCode":"JSON_DATA_CONTENT_TYPE = \"application/json\"\n\"\"\"JSON data content type\"\"\"\n\n# TODO(evmattso): what's the correct content type? There seems to be some disagreement over what it should be\nPROTOBUF_DATA_CONTENT_TYPE = \"application/x-protobuf\"\n\"\"\"Protobuf data content type\"\"\"\n\n\n@experimental\nclass DataclassJsonMessageSerializer(MessageSerializer[DataclassT]):\n    \"\"\"Serializer for dataclass messages.\"\"\"\n\n    def __init__(self, cls: type[DataclassT]) -> None:\n        \"\"\"Initialize the serializer with a dataclass type.\"\"\"\n        if contains_a_union(cls):\n            raise ValueError(\"Dataclass has a union type, which is not supported. To use a union, use a Pydantic model\")\n\n        if has_nested_dataclass(cls) or has_nested_base_model(cls):\n            raise ValueError(\n                \"Dataclass has nested dataclasses or base models, which are not supported. To use nested types, \"\n                \"use a Pydantic model\"\n            )\n\n        self.cls = cls\n\n    @property\n    def data_content_type(self) -> str:\n        \"\"\"Return the data content type.\"\"\"\n        return JSON_DATA_CONTENT_TYPE\n\n    @property\n    def type_name(self) -> str:\n        \"\"\"Return the type name.\"\"\"\n        return _type_name(self.cls)\n\n    def deserialize(self, payload: bytes) -> DataclassT:\n        \"\"\"Deserialize the payload into a dataclass message.\"\"\"","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/agents/runtime/core/serialization.py#L118-L154","documentation":"Raised by DataclassJsonMessageSerializer.__init__ when the dataclass message has a nested dataclass or Pydantic BaseModel field (has_nested_dataclass or has_nested_base_model returns True). The dataclass JSON serializer does not support nested types, so construction is rejected; use a Pydantic model instead.","triggerScenarios":"Constructing/registering a dataclass message whose field is itself a dataclass or a BaseModel (and depending on the helper, a collection of them).","commonSituations":"Composing messages from smaller dataclass records; embedding a Pydantic sub-model inside a dataclass message.","solutions":["Convert the message to a Pydantic BaseModel, which supports nesting.","Or flatten nested fields into the top-level dataclass.","Or register a custom serializer that handles the nested structure."],"exampleFix":"// before\nfrom dataclasses import dataclass\n\n@dataclass\nclass Inner:\n    x: int\n\n@dataclass\nclass Msg:\n    inner: Inner  # nested dataclass -> ValueError\n\n// after\nfrom pydantic import BaseModel\n\nclass Inner(BaseModel):\n    x: int\n\nclass Msg(BaseModel):\n    inner: Inner","handlingStrategy":"validation","validationCode":"from semantic_kernel.agents.runtime.core.serialization import has_nested_dataclass, has_nested_base_model\nimport dataclasses\n\ndef dataclass_flat(cls) -> bool:\n    return dataclasses.is_dataclass(cls) and not (has_nested_dataclass(cls) or has_nested_base_model(cls))","typeGuard":"def is_pydantic_or_flat_dataclass(cls) -> bool:\n    from pydantic import BaseModel\n    import dataclasses\n    if isinstance(cls, type) and issubclass(cls, BaseModel):\n        return True\n    return dataclasses.is_dataclass(cls) and not (has_nested_dataclass(cls) or has_nested_base_model(cls))","tryCatchPattern":null,"preventionTips":["Use Pydantic models for messages with nested types.","Flatten dataclass messages, or register a custom serializer.","Check the nesting helpers before registering a dataclass message."],"tags":["serialization","dataclass","nested-types","pydantic"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}