Skip to content

Commit e33a9e4

Browse files
fix: add chat message name field (#8969)
* add-chat-message-name-field * add release notes * Update add-chat-message-name-field-a8ae96fb9ff13f7b.yaml --------- Co-authored-by: Stefano Fiorucci <[email protected]>
1 parent 830e749 commit e33a9e4

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

haystack/dataclasses/chat_message.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ def to_openai_dict_format(self) -> Dict[str, Any]:
371371

372372
openai_msg: Dict[str, Any] = {"role": self._role.value}
373373

374+
# Add name field if present
375+
if self._name is not None:
376+
openai_msg["name"] = self._name
377+
374378
if tool_call_results:
375379
result = tool_call_results[0]
376380
if result.origin.id is None:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
In the `ChatMessage.to_openai_dict_format` utility method,
5+
include the `name` field in the returned dictionary, if present.
6+
Previously, the `name` field was erroneously skipped.

test/dataclasses/test_chat_message.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ def test_to_openai_dict_format():
257257
)
258258
assert message.to_openai_dict_format() == {"role": "tool", "content": tool_result, "tool_call_id": "123"}
259259

260+
message = ChatMessage.from_user(text="I have a question", name="John")
261+
assert message.to_openai_dict_format() == {"role": "user", "content": "I have a question", "name": "John"}
262+
263+
message = ChatMessage.from_assistant(text="I have an answer", name="Assistant1")
264+
assert message.to_openai_dict_format() == {"role": "assistant", "content": "I have an answer", "name": "Assistant1"}
265+
260266

261267
def test_to_openai_dict_format_invalid():
262268
message = ChatMessage(_role=ChatRole.ASSISTANT, _content=[])

0 commit comments

Comments
 (0)