Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions python/semantic_kernel/contents/chat_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ def add_message(

def _prepare_for_add(
self, role: AuthorRole, content: str | None = None, items: list[KernelContent] | None = None, **kwargs: Any
) -> dict[str, str]:
) -> dict[str, Any]:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in commit f53a8be by changing _prepare_for_add return annotation to dict[str, Any]. This reflects the actual role and items values returned by the helper.

"""Prepare a message to be added to the history."""
kwargs["role"] = role

if role == AuthorRole.TOOL and content and not items:
if role == AuthorRole.TOOL and content is not None and not items:
tool_call_id = kwargs.pop("tool_call_id", None)
function_name = kwargs.pop("function_name", "unknown")
function_result_content = FunctionResultContent(
Expand Down
16 changes: 16 additions & 0 deletions python/tests/unit/contents/test_chat_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,22 @@ def test_add_tool_message_to_dict_succeeds(chat_history: ChatHistory):
assert result["tool_call_id"] == "call_123"


def test_add_empty_tool_message_to_dict_succeeds(chat_history: ChatHistory):
chat_history.add_tool_message("", tool_call_id="call_123", function_name="test_function")

msg = chat_history.messages[-1]
assert isinstance(msg.items[0], FunctionResultContent)
assert msg.items[0].result == ""
assert msg.items[0].function_name == "test_function"
assert msg.items[0].id == "call_123"
assert msg.items[0].call_id == "call_123"

result = msg.to_dict()
assert result["content"] == ""
assert result["role"] == AuthorRole.TOOL
assert result["tool_call_id"] == "call_123"


def test_add_tool_message_list(chat_history: ChatHistory):
content = [FunctionResultContent(id="test", result="Tool message")]
chat_history.add_tool_message(content)
Expand Down
Loading