Skip to content

Commit 62ac27c

Browse files
authored
chore: remove deprecated function ChatRole and from_function class method in ChatMessage (#8725)
* rm deprecated function role and from_function class method in chatmessage * release note
1 parent 26b8077 commit 62ac27c

File tree

3 files changed

+12
-44
lines changed

3 files changed

+12
-44
lines changed

haystack/dataclasses/chat_message.py

-29
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
import json
6-
import warnings
76
from dataclasses import asdict, dataclass, field
87
from enum import Enum
98
from typing import Any, Dict, List, Optional, Sequence, Union
@@ -28,9 +27,6 @@ class ChatRole(str, Enum):
2827
#: The tool role. A message from a tool contains the result of a Tool invocation.
2928
TOOL = "tool"
3029

31-
#: The function role. Deprecated in favor of `TOOL`.
32-
FUNCTION = "function"
33-
3430
@staticmethod
3531
def from_str(string: str) -> "ChatRole":
3632
"""
@@ -128,11 +124,6 @@ def __new__(cls, *args, **kwargs):
128124

129125
return super(ChatMessage, cls).__new__(cls)
130126

131-
def __post_init__(self):
132-
if self._role == ChatRole.FUNCTION:
133-
msg = "The `FUNCTION` role has been deprecated in favor of `TOOL` and will be removed in 2.10.0. "
134-
warnings.warn(msg, DeprecationWarning)
135-
136127
def __getattribute__(self, name):
137128
"""
138129
This method is reimplemented to make the `content` attribute removal more visible.
@@ -299,26 +290,6 @@ def from_tool(
299290
_meta=meta or {},
300291
)
301292

302-
@classmethod
303-
def from_function(cls, content: str, name: str) -> "ChatMessage":
304-
"""
305-
Create a message from a function call. Deprecated in favor of `from_tool`.
306-
307-
:param content: The text content of the message.
308-
:param name: The name of the function being called.
309-
:returns: A new ChatMessage instance.
310-
"""
311-
msg = (
312-
"The `from_function` method is deprecated and will be removed in version 2.10.0. "
313-
"Its behavior has changed: it now attempts to convert legacy function messages to tool messages. "
314-
"This conversion is not guaranteed to succeed in all scenarios. "
315-
"Please migrate to `ChatMessage.from_tool` and carefully verify the results if you "
316-
"continue to use this method."
317-
)
318-
warnings.warn(msg)
319-
320-
return cls.from_tool(content, ToolCall(id=None, tool_name=name, arguments={}), error=False)
321-
322293
def to_dict(self) -> Dict[str, Any]:
323294
"""
324295
Converts ChatMessage into a dictionary.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
upgrade:
3+
- |
4+
The deprecated `FUNCTION` role has been removed from the `ChatRole` enum. Use `TOOL` instead.
5+
The deprecated class method `ChatMessage.from_function` has been removed. Use `ChatMessage.from_tool` instead.

test/dataclasses/test_chat_message.py

+7-15
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,14 @@ def test_mixed_content():
146146
assert message.tool_call == content[1]
147147

148148

149-
def test_from_function():
150-
# check warning is raised
151-
with pytest.warns():
152-
message = ChatMessage.from_function("Result of function invocation", "my_function")
149+
def test_function_role_removed():
150+
with pytest.raises(ValueError):
151+
ChatRole.from_str("function")
153152

154-
assert message.role == ChatRole.TOOL
155-
assert message.tool_call_result == ToolCallResult(
156-
result="Result of function invocation",
157-
origin=ToolCall(id=None, tool_name="my_function", arguments={}),
158-
error=False,
159-
)
153+
154+
def test_from_function_class_method_removed():
155+
with pytest.raises(AttributeError):
156+
ChatMessage.from_function("Result of function invocation", "my_function")
160157

161158

162159
def test_serde():
@@ -234,11 +231,6 @@ def test_chat_message_init_content_parameter_type():
234231
ChatMessage(ChatRole.USER, "This is a message")
235232

236233

237-
def test_chat_message_function_role_deprecated():
238-
with pytest.warns(DeprecationWarning):
239-
ChatMessage(ChatRole.FUNCTION, TextContent("This is a message"))
240-
241-
242234
def test_to_openai_dict_format():
243235
message = ChatMessage.from_system("You are good assistant")
244236
assert message.to_openai_dict_format() == {"role": "system", "content": "You are good assistant"}

0 commit comments

Comments
 (0)