mirror of
https://github.com/deepset-ai/haystack.git
synced 2025-11-25 14:36:05 +00:00
fix: make ChatMessage.from_dict handle cases where optional fields are missing (#9232)
* fix: make ChatMessage.from_dict handle cases where optional fields are missing * one more test
This commit is contained in:
parent
859e90cc61
commit
c67d1bf0e9
@ -361,7 +361,11 @@ class ChatMessage:
|
||||
The created object.
|
||||
"""
|
||||
if "content" in data:
|
||||
init_params = {"_role": ChatRole(data["role"]), "_name": data["name"], "_meta": data["meta"]}
|
||||
init_params: Dict[str, Any] = {
|
||||
"_role": ChatRole(data["role"]),
|
||||
"_name": data.get("name"),
|
||||
"_meta": data.get("meta") or {},
|
||||
}
|
||||
|
||||
if isinstance(data["content"], list):
|
||||
# current format - the serialized `content` field is a list of dictionaries
|
||||
@ -378,8 +382,8 @@ class ChatMessage:
|
||||
return cls(
|
||||
_role=ChatRole(data["_role"]),
|
||||
_content=_deserialize_content(data["_content"]),
|
||||
_name=data["_name"],
|
||||
_meta=data["_meta"],
|
||||
_name=data.get("_name"),
|
||||
_meta=data.get("_meta") or {},
|
||||
)
|
||||
|
||||
raise ValueError(f"Missing 'content' or '_content' in serialized ChatMessage: `{data}`")
|
||||
|
||||
@ -0,0 +1,4 @@
|
||||
---
|
||||
fixes:
|
||||
- |
|
||||
Fix `ChatMessage.from_dict` to handle cases where optional fields like `name` and `meta` are missing.
|
||||
@ -226,6 +226,16 @@ def test_from_dict_with_pre29_format():
|
||||
assert msg.meta == {"some": "info"}
|
||||
|
||||
|
||||
def test_from_dict_with_pre29_format_some_fields_missing():
|
||||
serialized_msg_pre_29 = {"role": "user", "content": "This is a message"}
|
||||
msg = ChatMessage.from_dict(serialized_msg_pre_29)
|
||||
|
||||
assert msg.role == ChatRole.USER
|
||||
assert msg._content == [TextContent(text="This is a message")]
|
||||
assert msg.name is None
|
||||
assert msg.meta == {}
|
||||
|
||||
|
||||
def test_from_dict_with_pre212_format():
|
||||
"""
|
||||
Test that we can deserialize messages serialized with versions >=2.9.0 and <2.12.0,
|
||||
@ -245,6 +255,26 @@ def test_from_dict_with_pre212_format():
|
||||
assert msg.meta == {"some": "info"}
|
||||
|
||||
|
||||
def test_from_dict_with_pre212_format_some_fields_missing():
|
||||
serialized_msg_pre_212 = {"_role": "user", "_content": [{"text": "This is a message"}]}
|
||||
msg = ChatMessage.from_dict(serialized_msg_pre_212)
|
||||
|
||||
assert msg.role == ChatRole.USER
|
||||
assert msg._content == [TextContent(text="This is a message")]
|
||||
assert msg.name is None
|
||||
assert msg.meta == {}
|
||||
|
||||
|
||||
def test_from_dict_some_fields_missing():
|
||||
serialized_msg = {"role": "user", "content": [{"text": "This is a message"}]}
|
||||
msg = ChatMessage.from_dict(serialized_msg)
|
||||
|
||||
assert msg.role == ChatRole.USER
|
||||
assert msg._content == [TextContent(text="This is a message")]
|
||||
assert msg.name is None
|
||||
assert msg.meta == {}
|
||||
|
||||
|
||||
def test_from_dict_missing_content_field():
|
||||
serialized_msg = {"role": "user", "name": "some_name", "meta": {"some": "info"}}
|
||||
with pytest.raises(ValueError):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user