Skip to content

fix: ChatHistory: preserve empty tool results with tool_call_id - #14480

Open
Wu Shuwen (dajiaohuang) wants to merge 1 commit into
microsoft:mainfrom
dajiaohuang:fix/empty-tool-results-serialization
Open

Wu Shuwen (dajiaohuang) wants to merge 1 commit into
microsoft:mainfrom
dajiaohuang:fix/empty-tool-results-serialization

Conversation

@dajiaohuang

Copy link
Copy Markdown

Description

_prepare_for_add() checked 'content' (truthy check) when deciding to create FunctionResultContent for a TOOL message. An empty string content is falsy, so add_tool_message('', tool_call_id='...') silently dropped the tool_call_id and created an invalid message with no items.

Reproduction

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))  # tool 0
print(message.to_dict())  # IndexError: list index out of range

Fix

Change 'content' to 'content is not None' so an empty string result is accepted and the FunctionResultContent is created with the call_id preserved.

Fixes #14359.

_prepare_for_add() checked 'content' (truthy check) when deciding to
create FunctionResultContent for a TOOL message. An empty string content
is falsy, so add_tool_message('', tool_call_id='...') silently dropped
the tool_call_id and created an invalid message with no items.

Change 'content' to 'content is not None' so an empty string result
is accepted and the FunctionResultContent is created with the call_id.

Fixes microsoft#14359.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Add the requested regression test for empty tool results and serialization.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
What changed in this PR

Fixes ChatHistory so empty-string tool results preserve their tool_call_id and serialize correctly.

Changes:

  • Accepts non-None tool content, including empty strings.
  • Preserves FunctionResultContent for empty tool results.
File Summary
python/​semantic_kernel/​contents/​chat_history.py Preserves empty tool results and IDs; add a regression test covering empty results and to_dict() serialization.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

kwargs["role"] = role

if role == AuthorRole.TOOL and content and not items:
if role == AuthorRole.TOOL and content is not None and not items:

This branch had an error being deployed

1 failed deployment
github-app-auth — fe42415c Deployed Sep 25, 2026 by dajiaohuang via review #540
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Empty tool results lose correlation and fail serialization

2 participants