Repository navigation
fix: keep subclasses in content part from_dict - #13536
Conversation
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.
|
@sunhouy is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
|
Hi @sunhouy, thanks a lot for your contribution! 🙏 We noticed that the Contributor License Agreement (CLA) check ( To get your PR reviewed, please sign the CLA via the link in the |
|
Thanks for signing the CLA, @sunhouy! 🎉 This PR is now ready for review again and the reviewer has been re-assigned. |
Coverage reportClick to see where and how coverage changed
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 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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]>
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]>
Related Issues
ImageContent,FileContentandTextContentfrom_dictreturn the base class when called on a subclass #13535Proposed Changes:
ImageContent.from_dict,FileContent.from_dictandTextContent.from_dictwere 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 ato_dict()/from_dict()round trip.They now instantiate
cls(**data)and are annotated-> Self, same asByteStream.from_dictafter #13180. No other behaviour changes: the content-part registry in_deserialize_content_partstill looks up the base classes, soChatMessagedeserialization is unaffected.How did you test it?
Added one regression test per class (
test_*_subclass_from_dict) that defines a trivial dataclass subclass and assertstype(Sub.from_dict(Sub(...).to_dict())) is Sub.3 failed(assert ImageContent is MyImage, same for the other two).3 passed.pytest test/dataclasses: 347 passed, 1 failed. The failure istest_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 checkandruff format --checkclean on the touched files.Notes for the reviewer
I left
ImageFileToImageContent/PDFToImageContentalone — they go throughImageContent(...)constructors, notfrom_dict, so they're out of scope here. Happy to extend to any otherfrom_dictinhaystack/dataclassesif you'd like them all consistent in one PR.Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.