Skip to content

fix: keep subclasses in content part from_dict - #13536

Merged
sjrl merged 1 commit into
deepset-ai:mainfrom
houyunsun:fix/content-from-dict-subclass
Nov 6, 2026
Merged

sjrl merged 1 commit into
deepset-ai:mainfrom
houyunsun:fix/content-from-dict-subclass

Conversation

@houyunsun

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

ImageContent.from_dict, FileContent.from_dict and TextContent.from_dict were classmethods that hardcoded the base class (return ImageContent(**data) etc.), so calling them on a subclass returned a plain base-class instance. Any extra field or method on the subclass was silently dropped after a to_dict() / from_dict() round trip.

They now instantiate cls(**data) and are annotated -> Self, same as ByteStream.from_dict after #13180. No other behaviour changes: the content-part registry in _deserialize_content_part still looks up the base classes, so ChatMessage deserialization is unaffected.

How did you test it?

Added one regression test per class (test_*_subclass_from_dict) that defines a trivial dataclass subclass and asserts type(Sub.from_dict(Sub(...).to_dict())) is Sub.

  • New tests before the fix: 3 failed (assert ImageContent is MyImage, same for the other two).
  • After the fix: 3 passed.
  • pytest test/dataclasses: 347 passed, 1 failed. The failure is test_image_content_from_url_wrong_mime_type, which fetches from the network and dies on my machine's SSL certs — it fails the same way on a clean checkout.
  • ruff check and ruff format --check clean on the touched files.

Notes for the reviewer

I left ImageFileToImageContent/PDFToImageContent alone — they go through ImageContent(...) constructors, not from_dict, so they're out of scope here. Happy to extend to any other from_dict in haystack/dataclasses if you'd like them all consistent in one PR.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

ImageContent, FileContent and TextContent all had a from_dict that
instantiated the base class directly, so Sub.from_dict(data) gave you a
plain base-class object back and anything the subclass added was gone
after a to_dict/from_dict round trip. ByteStream already does this with
cls() (#13180), the other three just never got the same treatment.

Return type is now Self and the body builds cls(). The registry lookup in
_deserialize_content_part keeps passing the base classes, so ChatMessage
deserialization output doesn't change.
@houyunsun
houyunsun requested a review from a team as a code owner November 2, 2026 21:12
@houyunsun
houyunsun requested review from julian-risch and removed request for a team November 2, 2026 21:12
@vercel

vercel Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

@sunhouy is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Nov 2, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@HaystackBot

Copy link
Copy Markdown
Contributor

Hi @sunhouy, thanks a lot for your contribution! 🙏

We noticed that the Contributor License Agreement (CLA) check (license/cla) hasn't passed yet, so we've temporarily moved this PR to draft and paused the review assignment.

To get your PR reviewed, please sign the CLA via the link in the license/cla check below (or in the CLA bot comment). As soon as the check turns green, this PR will automatically be marked ready for review again and a reviewer will be re-assigned.

@HaystackBot
HaystackBot removed the request for review from julian-risch November 2, 2026 22:21
@HaystackBot HaystackBot added the cla-pending PR is in draft until the contributor signs the CLA label Nov 2, 2026
@HaystackBot
HaystackBot marked this pull request as draft November 2, 2026 22:21
@HaystackBot
HaystackBot marked this pull request as ready for review November 3, 2026 01:09
@HaystackBot HaystackBot removed the cla-pending PR is in draft until the contributor signs the CLA label Nov 3, 2026
@HaystackBot

Copy link
Copy Markdown
Contributor

Thanks for signing the CLA, @sunhouy! 🎉 This PR is now ready for review again and the reviewer has been re-assigned.

@sjrl sjrl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sjrl
sjrl removed the request for review from julian-risch November 6, 2026 13:04
@sjrl sjrl self-assigned this Nov 6, 2026
@github-actions github-actions Bot added the type:documentation Improvements on the docs label Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/agents
  utils.py
  haystack/components/converters
  markdown.py
  msg.py
  haystack/components/converters/image
  document_to_image.py
  haystack/components/generators/chat
  openai_responses.py
  haystack/components/joiners
  document_joiner.py
  list_joiner.py
  haystack/components/preprocessors
  csv_document_cleaner.py
  document_cleaner.py
  hierarchical_document_splitter.py
  python_code_splitter.py
  haystack/components/rankers
  llm_ranker.py
  haystack/components/retrievers
  auto_merging_retriever.py
  haystack/core/pipeline
  pipeline.py
  utils.py
  haystack/dataclasses
  chat_message.py
  document.py
  file_content.py
  image_content.py
  haystack/document_stores/in_memory
  document_store.py
  haystack/hooks
  utils.py
  haystack/hooks/compaction
  sliding_window.py
  haystack/hooks/human_in_the_loop
  strategies.py
  haystack/hooks/structured_output
  hooks.py
  haystack/tools
  from_function.py
Project Total  

The report is truncated to 25 files out of 26. To see the full report, please visit the workflow summary page.

This report was generated by python-coverage-comment-action

@sjrl
sjrl merged commit 3d230e6 into deepset-ai:main Nov 6, 2026
40 of 41 checks passed
simin-yuan added a commit to simin-yuan/haystack that referenced this pull request Nov 6, 2026
ToolCall, ToolCallResult, ReasoningContent, ToolCallDelta and ComponentInfo
hardcoded their own class in from_dict, so a subclass lost its extra fields on
a to_dict()/from_dict() round trip. deepset-ai#13180 and deepset-ai#13536 fixed the same bug for
ByteStream and for TextContent/FileContent/ImageContent; this completes the set
in haystack/dataclasses.

Signed-off-by: Simin Yuan <[email protected]>
simin-yuan added a commit to simin-yuan/haystack that referenced this pull request Nov 6, 2026
ToolCall, ToolCallResult, ReasoningContent, ToolCallDelta and ComponentInfo
hardcoded their own class in from_dict, so a subclass lost its extra fields on
a to_dict()/from_dict() round trip. deepset-ai#13180 and deepset-ai#13536 fixed the same bug for
ByteStream and for TextContent/FileContent/ImageContent; this completes the set
in haystack/dataclasses.

Signed-off-by: Simin Yuan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants