fix: don't modify the input dictionary in from_dict - #12420
Closed
Ayushhgit wants to merge 1 commit into
Closed
Conversation
`from_dict` deserialized in place, replacing values inside the caller's dictionary with live objects: a callable path became the callable, a nested component dict became the component, "overwrite" became DuplicatePolicy.OVERWRITE. The dictionary was then no longer serializable and a second `from_dict` call on it failed. `Pipeline.from_dict` already copies its input to prevent this. Do the same in the 49 `from_dict` implementations that deserialize in place, through a `_copy_serialized_data` helper wrapping the existing `_deepcopy_with_exceptions`, so components and tools that are already live objects are passed through instead of copied. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
@Ayushhgit is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
|
Closing, see #12418 (comment) |
Contributor
Coverage reportClick to see where and how coverage changed
The report is truncated to 25 files out of 52. To see the full report, please visit the workflow summary page. This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issues
haystack-experimentalonlyProposed Changes:
from_dict()deserializes in place, so it replaced values inside the dictionary the caller passed in with live objects: a callable path became the callable, a nested component dict became the component,"overwrite"becameDuplicatePolicy.OVERWRITE. Afterwards the caller's dict could no longer be serialized, and a secondfrom_dict()call on it failed with errors like'function' object has no attribute 'split'orThe value of 'chat_generator' is not a dictionary.Two places in the codebase already treat this as the expected contract:
PipelineBase._from_dict_implcopies its input first —data_copy = _deepcopy_with_exceptions(data) # to prevent modification of original datatest_component_from_dict_does_not_mutate_inputasserts it for the secretsdefault_from_dictdeserializesThis PR extends the same contract to the hand-written
from_dictimplementations:_copy_serialized_datatohaystack/utils/deserialization.py, a thin wrapper over the existing_deepcopy_with_exceptions(so components, tools and toolsets that are already live objects are passed through, not copied)from_dictimplementations that deserialize in place — components, tools, toolsets, hooks andSuperComponentNo public behavior other than the mutation changes: the objects returned by
from_dictare the same as before, and the deserialization helpers themselves are untouched.Document.from_dictandChatMessage.from_dictneeded no change, they already build a new dict.How did you test it?
test/test_from_dict_no_mutation.py: for one component per deserialization style (callable, tools, nested chat generator, chat messages, type, enum, filter policy, pipeline, tool) it asserts thatfrom_dictleaves the input dict equal to what it was, that callingfrom_dicttwice on the same data returns equal components, and that the data is still JSON-serializable afterwards. All 25 of these tests fail onmainand pass with this change.component.registryand fails if any component that can be built without arguments mutates its input, so new components are covered automatically.hatch run test:unit: 6212 passed. The 2 failures on my machine (test_run_with_bad_filter,test_from_file_path_guess_mime_type[spam.csv]) also fail on a clean checkout ofmain— they are ajqerror-message and a Windows mimetypes difference, unrelated to this change.ruff check/ruff formatare clean.hatch run test:typesreports 25 errors on my machine, all of them unusedtype: ignorecomments andopenaistub mismatches in the OpenAI generators and their tests, none in the files this PR touches — they come from the unpinnedopenaiversion locally.Notes for the reviewer
component_from_dict, becauseCls.from_dict(data)is called directly by users and by the deserialization helpers, so a central hook would not cover it. If you would rather solve this centrally, I'm happy to rework it.Pipeline.from_dictnow copies twice: once for the whole pipeline, then once per component. The data is plain configuration and_deepcopy_with_exceptionsskips objects that are expensive or impossible to copy, so this should not be noticeable, but say the word if you want the pipeline-level copy dropped instead.This PR was generated with an AI assistant. I have reviewed the changes and run the relevant tests.
Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.