Python: fix: ChatHistoryTruncationReducer orphans TOOL role messages#13608
Open
giulio-leone wants to merge 1 commit intomicrosoft:mainfrom
Open
Python: fix: ChatHistoryTruncationReducer orphans TOOL role messages#13608giulio-leone wants to merge 1 commit intomicrosoft:mainfrom
giulio-leone wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…without FunctionResultContent The locate_safe_reduction_index backward scan only checked msg.items for FunctionCallContent/FunctionResultContent to avoid splitting tool call/result pairs. However, TOOL role messages that contain only text content (no FunctionResultContent in items) were not recognized as part of a tool pair, causing the truncation to split tool_calls from their tool responses. Fix: Include AuthorRole.TOOL check in contains_function_call_or_result so that any TOOL role message is treated as part of a tool call/result pair, regardless of its items content. Fixes microsoft#12708
Author
|
Friendly ping — CI is green and this is ready for review. Happy to address any feedback. Thanks! |
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.
Summary
Fixes #12708
ChatHistoryTruncationReducer.reduce()can orphanTOOLrole messages by truncating the precedingassistantmessage that contains thetool_calls, causing OpenAI to reject the history with:Root Cause
locate_safe_reduction_index()usescontains_function_call_or_result()to detect tool-related messages during its backward scan. This function only checksmsg.itemsforFunctionCallContent/FunctionResultContentinstances.However,
TOOLrole messages can contain only text content (noFunctionResultContentinitems), which causes the backward scan to treat them as regular messages. The truncation point then lands between thetool_callsassistant message and itsTOOLresponses, orphaning the tool results.Fix
Added
AuthorRole.TOOLcheck tocontains_function_call_or_result():This ensures any
TOOLrole message is recognized as part of a tool call/result pair, regardless of whether it hasFunctionResultContentin itsitems.Test
Added
test_locate_safe_reduction_index_tool_role_without_function_result_contentthat verifies TOOL role messages with only text content are not separated from their tool call.