Repository navigation
Python: keep empty tool results through the prompt round trip - #14686
Open
Himansh Raj (iamthehimansh) wants to merge 1 commit into
Open
Himansh Raj (iamthehimansh) wants to merge 1 commit into
Himansh Raj (iamthehimansh) wants to merge 1 commit into
Conversation
FunctionResultContent.to_element renders an empty result as a self-closing element, and from_element read its text back as None, which ChatMessageContent then sent to the model as the string "None". Map missing text to "". Fixes microsoft#14685
Himansh Raj (iamthehimansh)
deployed
to
github-app-auth
October 29, 2026 19:27 — with
GitHub Actions
Active
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Fixes #14685.
Since #14472,
ChatHistory.add_tool_message("")keeps an empty tool result. But that result doesn't survive the XML prompt round trip thatKernelFunctionFromPromptperforms on every chat completion call.to_element()renders it as a self-closing<function_result id="call_1" />. Whenfrom_element()parses that back, it readselement.text, which isNonefor an empty element.ChatMessageContentthen stringifies the value, so the model receives the literal tool result"None"instead of an empty one.Description
FunctionResultContent.from_elementnow maps a missing element text to"". Non-empty results are unchanged.test_element_round_trip_keeps_result(empty and non-empty), which serialises the element to XML and parses it back.test_chat_history_prompt_round_trip_keeps_empty_tool_result, which reproduces the issue end to end throughto_prompt()/from_rendered_prompt().Before (main
4bb64ee, macOS arm64, Python 3.12):After:
Tests, run from
python/:pytest tests/unit/contents: 421 passed onmain, 424 passed with this change.2 failed, 25 passedintest_function_result_content.py.ruff check/ruff format --checkon both files are clean.This doesn't overlap the open #14375 (the
from_function_call_content_and_resultfactory) or #14480 (theadd_tool_messagepath), and it doesn't touch the JSONserialize()/restore_chat_history()path, which already keeps"".Contribution Checklist
AI-assisted: yes (Cana)