Skip to content

Conversation

@julian-risch
Copy link
Member

@julian-risch julian-risch commented Jan 21, 2026

Related Issues

Proposed Changes:

  • Simplify default_to_dict so that we call to_dict() for any init parameters that have a callable to_dict
  • Extend default_from_dict so that we call from_dict for any dict values where type is a class that can be imported and that has a callable from_dict. Or we call default_from_dict if the type is a class that can be imported but that has no callable from_dict.
  • Added tests to verify that the automatic DocumentStore handling works through component_to_dict/component_from_dict, which internally use default_to_dict/default_from_dict
  • Added a test that checks the error message contains the correct key when deserialization fails
  • Call Secret.from_dict instead of deserialize_secrets_inplace

How did you test it?

unit tests

Notes for the reviewer

Different DocumentStores don't have any init parameters in common. For example, ChromaDocumentStore does not have index as init parameter. I don't have a good idea how to better check that a dict is a serialized DocumentStore other than trying to import the class and checking if it looks like a DocumentStore. I am unsure if the complexity added by _is_serialized_document_store is worth it.
@sjrl suggested to simply check if to_dict/from_dict is defined and can be called.

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.

@vercel
Copy link

vercel bot commented Jan 21, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Review Updated (UTC)
haystack-docs Ignored Ignored Preview Jan 22, 2026 5:15pm

Request Review

@github-actions github-actions bot added topic:core type:documentation Improvements on the docs labels Jan 21, 2026
@coveralls
Copy link
Collaborator

coveralls commented Jan 21, 2026

Pull Request Test Coverage Report for Build 21257841866

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 44 unchanged lines in 10 files lost coverage.
  • Overall coverage increased (+0.1%) to 92.448%

Files with Coverage Reduction New Missed Lines %
components/generators/chat/openai_responses.py 2 87.38%
tools/pipeline_tool.py 2 93.55%
dataclasses/chat_message.py 3 99.08%
core/super_component/utils.py 4 95.79%
utils/auth.py 4 89.42%
core/component/sockets.py 5 85.37%
core/serialization.py 5 95.69%
tools/component_tool.py 5 94.32%
tools/tool.py 5 96.77%
components/tools/tool_invoker.py 9 96.35%
Totals Coverage Status
Change from base Build 21248482406: 0.1%
Covered Lines: 14775
Relevant Lines: 15982

💛 - Coveralls

@julian-risch
Copy link
Member Author

@sjrl While this PR is draft, I'd already like to get your opinion on it. In particular, do you think _is_serialized_document_store is worth it? Do you have a better idea? Or should we better decide to not handle DocumentStore in default_from_dict / default_to_dict?

serialized_params = {}
for key, value in init_parameters.items():
if isinstance(value, (Secret, ComponentDevice)):
if isinstance(value, (Secret, ComponentDevice)) or _is_document_store(value):
Copy link
Contributor

Choose a reason for hiding this comment

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

apologies if this is a silly question, but why not just check if the value has the method to_dict? We already do this type of checking in our other serialization methods like used in for State and in PipelineSnapshot.

Then we trust the user to have serialized the object properly. This would also be a nifty way to support even custom objects or components made by users.

Comment on lines 273 to 283
def _is_serialized_document_store(value: Any) -> bool:
"""
Check if a value is a serialized DocumentStore dictionary.
A dictionary is considered a serialized DocumentStore if:
- It has a "type" key with a string value that is a fully qualified class name
- It has an "init_parameters" key with a dict value
- The class can be imported and has a "from_dict" method
This matches the structure produced by DocumentStore.to_dict().
"""
Copy link
Contributor

@sjrl sjrl Jan 22, 2026

Choose a reason for hiding this comment

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

As a parallel comment to this one https://github.com/deepset-ai/haystack/pull/10421/files#r2717180576 I wonder if we should be a little more lax here and just looks to see if value is a dict with at least key type and check if the loaded class has a from_dict method. Then if it does we go ahead and try to run cls.from_dict. So again putting trust into the user that they are following the expected serialization sturcture of a dict with type and init_parameters as keys and that the imported cls has the from_dict method.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the quick feedback! Makes sense. I'll update the draft PR accordingly.

@julian-risch julian-risch changed the title feat: Add default (de-)serialization for DocumentStore feat: Add default (de-)serialization for init parameter objects that define to_dict/from_dict Jan 22, 2026
@julian-risch julian-risch marked this pull request as ready for review January 22, 2026 17:04
@julian-risch julian-risch requested a review from a team as a code owner January 22, 2026 17:04
@julian-risch julian-risch requested review from anakin87 and sjrl and removed request for a team and anakin87 January 22, 2026 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle (de-)serialization of DocumentStore in default_from_dict, default_to_dict automatically

4 participants