Description
Describe the bug
ChatHistory.add_tool_message() drops an empty-string tool result instead of preserving it as FunctionResultContent. The appended message still has role tool, but its items list is empty and the supplied tool_call_id is lost. Serializing that otherwise ordinary history message then raises IndexError because ChatMessageContent.to_dict() expects a tool message to contain a function result.
An empty string is a valid tool result for cases such as a successful command with no stdout or a no-op tool. The helper documents content as a string and requires tool_call_id; it should retain both even when the string is empty.
To reproduce
from semantic_kernel.contents.chat_history import ChatHistory
history = ChatHistory()
history.add_tool_message("", tool_call_id="call_123")
message = history.messages[-1]
print(message.role.value, len(message.items))
print(message.to_dict())On current main (3438d882):
tool 0
IndexError: list index out of range
No model, provider, credentials, or network request is involved.
Root cause and proposed scope
ChatHistory._prepare_for_add() currently constructs FunctionResultContent only when content is truthy:
if role == AuthorRole.TOOL and content and not items:Changing that predicate to content is not None preserves the existing behavior while allowing a valid empty result to retain result="", id, and call_id. A focused regression should assert both the FunctionResultContent fields and the serialized tool-message dictionary.
This is related to, but distinct from, #13678: that PR improves an Azure adapter's error for manually constructed tool messages with no items. This issue is about preventing the public ChatHistory.add_tool_message() helper from creating that invalid shape when its documented string input is empty.
Expected behavior
The helper should append one FunctionResultContent with the empty result and supplied call ID, and message.to_dict() should produce a valid tool message instead of raising.
Platform
- Language: Python
- Source: current repository
mainat3438d882 - AI model: not applicable
- OS: macOS; provider-free reproduction
I can submit the focused predicate change and regression once maintainers confirm this contract.
Activity
hhhhzzzj commentedon Sep 3, 2026
I looked into the empty tool message issue in semantic_kernel/contents/chat_history.py.
In add_tool_message(), the logic skips creating a FunctionResultContent when result evaluates to falsy (such as an empty string ""). This leaves items empty and drops tool_call_id, causing to_dict() to fail with IndexError: list index out of range.
Allowing empty strings to be wrapped as valid FunctionResultContent(id=tool_call_id, name=function_name, result=result) and adding a serialization test for empty tool results will resolve this. Happy to put together a PR for this if the maintainers agree.
anythingoah commentedon Sep 10, 2026
Did this surface as a hard serialization crash, or did results silently lose their correlation first and the crash came later? Curious what tipped you off that empty results were the root cause.
YusefSyed commentedon Sep 10, 2026
anythingoah Codex-assisted clarification: the report comes from the small, provider-free reproducer above. At the reported revision,
add_tool_message("", tool_call_id="call_123")completes, but the appended tool message already has an emptyitemslist instead of aFunctionResultContentcarrying the result and call ID. The explicitto_dict()call then raisesIndexError.The empty-string-specific behavior follows from the
and contentcondition in_prepare_for_add(): it treats""as absent when deciding whether to create the result item. So the item/correlation loss precedes the serialization error in this reproducer. The report does not establish a longer production-system failure timeline.Python: Handle empty chat message function results (microsoft#14375)