Skip to content

ImageContent, FileContent and TextContent from_dict return the base class when called on a subclass #13535

Closed

Description

@shashank11yadav

Describe the bug
ImageContent.from_dict, FileContent.from_dict and TextContent.from_dict are classmethods, but each builds the base class directly (return ImageContent(**data) and so on) instead of using cls(**data). So MySubclass.from_dict(...) returns a plain base-class instance, and the subclass type is lost on a to_dict / from_dict round trip. #13180 fixed the same problem in ByteStream.from_dict.

Error message
No error is raised. The returned object has the wrong type.

Expected behavior
Sub.from_dict(Sub(...).to_dict()) returns an instance of Sub, as ByteStream.from_dict does on main since #13180 (return type Self, built with cls(...)). Today, subclass fields and methods are lost after a to_dict / from_dict round trip.

Additional context

  • haystack/dataclasses/image_content.py:163: return ImageContent(**data)
  • haystack/dataclasses/file_content.py:105: return FileContent(**data)
  • haystack/dataclasses/chat_message.py:77: return TextContent(**data)
  • Suggested fix: return cls(**data) with a Self return type, plus a regression test per class.

To Reproduce

from dataclasses import dataclass

from haystack.dataclasses import ByteStream, FileContent, ImageContent, TextContent


@dataclass
class MyImage(ImageContent):
    pass


@dataclass
class MyFile(FileContent):
    pass


@dataclass
class MyText(TextContent):
    pass


cases = [
    (MyImage, MyImage(base64_image="aGVsbG8=", mime_type="image/png")),
    (MyFile, MyFile(base64_data="aGVsbG8=", mime_type="application/pdf", filename="a.pdf")),
    (MyText, MyText(text="hi")),
]

for cls, obj in cases:
    result = cls.from_dict(obj.to_dict())
    print(f"EXPECTED: {cls.__name__}.from_dict returns {cls.__name__}")
    print(f"ACTUAL: {cls.__name__}.from_dict returns {type(result).__name__}")

# reference behaviour (ByteStream uses cls)
class MyBS(ByteStream):
    pass

ref = MyBS.from_dict(MyBS(data=b"x").to_dict())
print(f"REFERENCE: MyBS.from_dict returns {type(ref).__name__}")

Output on main (1fc0a28):

EXPECTED: MyImage.from_dict returns MyImage
ACTUAL: MyImage.from_dict returns ImageContent
EXPECTED: MyFile.from_dict returns MyFile
ACTUAL: MyFile.from_dict returns FileContent
EXPECTED: MyText.from_dict returns MyText
ACTUAL: MyText.from_dict returns TextContent
REFERENCE: MyBS.from_dict returns MyBS

On haystack-ai 3.4.1 the three ACTUAL lines are the same. The REFERENCE line prints ByteStream there because 3.4.1 predates #13180.

I'm happy to open a PR with the fix and tests.

FAQ Check

System:

  • OS: Linux
  • Haystack version: 3.4.1; also reproduced on main @ 1fc0a28 (Python 3.12)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions