From c20aac5ff8a4886afde68d8665148516aee2c736 Mon Sep 17 00:00:00 2001 From: Simin Yuan <281164480+simin-yuan@users.noreply.github.com> Date: Sat, 7 Nov 2026 01:27:46 +0800 Subject: [PATCH] fix: keep subclasses in the remaining content part from_dict methods 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. #13180 and #13536 fixed the same bug for ByteStream and for TextContent/FileContent/ImageContent; this completes the set in haystack/dataclasses. Signed-off-by: Simin Yuan <281164480+simin-yuan@users.noreply.github.com> --- haystack/dataclasses/chat_message.py | 12 +++---- haystack/dataclasses/streaming_chunk.py | 10 +++--- ...from-dict-subclasses-3f8b61c2d7a94e10.yaml | 8 +++++ test/dataclasses/test_chat_message.py | 32 +++++++++++++++++++ test/dataclasses/test_streaming_chunk.py | 23 +++++++++++++ 5 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 releasenotes/notes/fix-content-part-from-dict-subclasses-3f8b61c2d7a94e10.yaml diff --git a/haystack/dataclasses/chat_message.py b/haystack/dataclasses/chat_message.py index d86caad6a89..6dc6a45ea0b 100644 --- a/haystack/dataclasses/chat_message.py +++ b/haystack/dataclasses/chat_message.py @@ -104,7 +104,7 @@ def to_dict(self) -> dict[str, Any]: return asdict(self) @classmethod - def from_dict(cls, data: dict[str, Any]) -> "ToolCall": + def from_dict(cls, data: dict[str, Any]) -> Self: """ Creates a new ToolCall object from a dictionary. @@ -113,7 +113,7 @@ def from_dict(cls, data: dict[str, Any]) -> "ToolCall": :returns: The created object. """ - return ToolCall(**data) + return cls(**data) ToolCallResultContentT = str | Sequence[TextContent | ImageContent | FileContent] @@ -165,7 +165,7 @@ def _to_dict(self, *, for_trace: bool = False) -> dict[str, Any]: return {"result": result, "origin": self.origin.to_dict(), "error": self.error} @classmethod - def from_dict(cls, data: dict[str, Any]) -> "ToolCallResult": + def from_dict(cls, data: dict[str, Any]) -> Self: """ Creates a ToolCallResult from a dictionary. @@ -184,7 +184,7 @@ def from_dict(cls, data: dict[str, Any]) -> "ToolCallResult": if isinstance(result, list): result = [_deserialize_content_part(serialized_part) for serialized_part in result] - return ToolCallResult(result=result, origin=ToolCall.from_dict(data["origin"]), error=data["error"]) + return cls(result=result, origin=ToolCall.from_dict(data["origin"]), error=data["error"]) @_warn_on_inplace_mutation @@ -210,7 +210,7 @@ def to_dict(self) -> dict[str, Any]: return asdict(self) @classmethod - def from_dict(cls, data: dict[str, Any]) -> "ReasoningContent": + def from_dict(cls, data: dict[str, Any]) -> Self: """ Creates a new ReasoningContent object from a dictionary. @@ -219,7 +219,7 @@ def from_dict(cls, data: dict[str, Any]) -> "ReasoningContent": :returns: The created object. """ - return ReasoningContent(**data) + return cls(**data) ChatMessageContentT = TextContent | ToolCall | ToolCallResult | ImageContent | ReasoningContent | FileContent diff --git a/haystack/dataclasses/streaming_chunk.py b/haystack/dataclasses/streaming_chunk.py index d002f5c6081..5f137efb1b1 100644 --- a/haystack/dataclasses/streaming_chunk.py +++ b/haystack/dataclasses/streaming_chunk.py @@ -5,7 +5,7 @@ import inspect from collections.abc import Awaitable, Callable from dataclasses import asdict, dataclass, field -from typing import Any, Literal, overload +from typing import Any, Literal, Self, overload from haystack import logging from haystack.core.component import Component @@ -48,14 +48,14 @@ def to_dict(self) -> dict[str, Any]: return asdict(self) @classmethod - def from_dict(cls, data: dict[str, Any]) -> "ToolCallDelta": + def from_dict(cls, data: dict[str, Any]) -> Self: """ Creates a ToolCallDelta from a serialized representation. :param data: Dictionary containing ToolCallDelta's attributes. :returns: A ToolCallDelta instance. """ - return ToolCallDelta(**data) + return cls(**data) @_warn_on_inplace_mutation @@ -95,14 +95,14 @@ def to_dict(self) -> dict[str, Any]: return asdict(self) @classmethod - def from_dict(cls, data: dict[str, Any]) -> "ComponentInfo": + def from_dict(cls, data: dict[str, Any]) -> Self: """ Creates a ComponentInfo from a serialized representation. :param data: Dictionary containing ComponentInfo's attributes. :returns: A ComponentInfo instance. """ - return ComponentInfo(**data) + return cls(**data) @_warn_on_inplace_mutation diff --git a/releasenotes/notes/fix-content-part-from-dict-subclasses-3f8b61c2d7a94e10.yaml b/releasenotes/notes/fix-content-part-from-dict-subclasses-3f8b61c2d7a94e10.yaml new file mode 100644 index 00000000000..7ae078d5a30 --- /dev/null +++ b/releasenotes/notes/fix-content-part-from-dict-subclasses-3f8b61c2d7a94e10.yaml @@ -0,0 +1,8 @@ +--- +fixes: + - | + ``ToolCall.from_dict``, ``ToolCallResult.from_dict`` and ``ReasoningContent.from_dict`` in ``haystack.dataclasses.chat_message``, + as well as ``ToolCallDelta.from_dict`` and ``ComponentInfo.from_dict`` in ``haystack.dataclasses.streaming_chunk``, now + instantiate ``cls`` instead of hardcoding their own class. Calling them on a subclass returns the subclass again, so + extra fields are no longer dropped on a ``to_dict()`` / ``from_dict()`` round trip, matching ``TextContent``, ``FileContent``, + ``ImageContent`` and ``ByteStream``. diff --git a/test/dataclasses/test_chat_message.py b/test/dataclasses/test_chat_message.py index 34599549068..898e38ceec2 100644 --- a/test/dataclasses/test_chat_message.py +++ b/test/dataclasses/test_chat_message.py @@ -58,6 +58,38 @@ class MyText(TextContent): assert type(result) is MyText assert result == MyText(text="hello") + def test_tool_call_subclass_from_dict(self): + @dataclass + class MyToolCall(ToolCall): + pass + + original = MyToolCall(tool_name="get_weather", arguments={"city": "Paris"}, id="1") + result = MyToolCall.from_dict(original.to_dict()) + assert type(result) is MyToolCall + assert result == original + + def test_tool_call_result_subclass_from_dict(self): + @dataclass + class MyToolCallResult(ToolCallResult): + pass + + original = MyToolCallResult( + result="sunny", origin=ToolCall(tool_name="get_weather", arguments={"city": "Paris"}, id="1"), error=False + ) + result = MyToolCallResult.from_dict(original.to_dict()) + assert type(result) is MyToolCallResult + assert result == original + + def test_reasoning_content_subclass_from_dict(self): + @dataclass + class MyReasoning(ReasoningContent): + pass + + original = MyReasoning(reasoning_text="thinking") + result = MyReasoning.from_dict(original.to_dict()) + assert type(result) is MyReasoning + assert result == original + def test_text_content_no_warning_on_init(self): with warnings.catch_warnings(): warnings.simplefilter("error", Warning) diff --git a/test/dataclasses/test_streaming_chunk.py b/test/dataclasses/test_streaming_chunk.py index 4269b261b87..fd628152ad1 100644 --- a/test/dataclasses/test_streaming_chunk.py +++ b/test/dataclasses/test_streaming_chunk.py @@ -4,6 +4,7 @@ import logging import warnings +from dataclasses import dataclass import pytest @@ -440,3 +441,25 @@ async def callback(chunk): chunk = StreamingChunk(content="world") await _invoke_streaming_callback(callback, chunk) assert captured == [chunk] + + +def test_tool_call_delta_subclass_from_dict(): + @dataclass + class MyToolCallDelta(ToolCallDelta): + pass + + original = MyToolCallDelta(index=0, tool_name="get_weather", arguments='{"city": "Paris"}', id="1") + result = MyToolCallDelta.from_dict(original.to_dict()) + assert type(result) is MyToolCallDelta + assert result == original + + +def test_component_info_subclass_from_dict(): + @dataclass + class MyComponentInfo(ComponentInfo): + pass + + original = MyComponentInfo(type="haystack.components.generators.chat.openai.OpenAIChatGenerator", name="gen") + result = MyComponentInfo.from_dict(original.to_dict()) + assert type(result) is MyComponentInfo + assert result == original