fix: remove orphaned FunctionExecutionResultMessages after truncation#7958
Open
abhi-0203 wants to merge 1 commit into
Open
fix: remove orphaned FunctionExecutionResultMessages after truncation#7958abhi-0203 wants to merge 1 commit into
abhi-0203 wants to merge 1 commit into
Conversation
TokenLimitedChatCompletionContext.get_messages() truncates by popping the middle message until the token budget is satisfied. The old cleanup only checked if messages[0] was a FunctionExecutionResultMessage. If truncation removed an AssistantMessage containing FunctionCalls while its paired FunctionExecutionResultMessage sat elsewhere in the list, the orphan was never repaired — leaving a tool result with no matching tool call. Replace the index-0-only check with a full pass: collect all call_ids from remaining AssistantMessages with FunctionCalls, then filter out any FunctionExecutionResultMessages whose call_ids have no matching call. Closes microsoft#7955
Contributor
|
@abhi-0203 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
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
TokenLimitedChatCompletionContext.get_messages()truncates by repeatedly popping the message atlen(messages) // 2until the token budget is satisfied. The old cleanup only checked ifmessages[0]was aFunctionExecutionResultMessage. If truncation removed anAssistantMessagecontainingFunctionCalls while its pairedFunctionExecutionResultMessagesat elsewhere in the list, the orphan was never repaired — leaving a tool result with no matching tool call.Fix
Replace the index-0-only check with a full pass: collect all
call_ids from remainingAssistantMessages withFunctionCalls, then filter out anyFunctionExecutionResultMessages whosecall_ids have no matching call.Reproduction
With 7 messages and
token_limit=6, exactly one pop is needed:middle_index = 7 // 2 = 3, which removes theAssistantMessageholdingcall_1. The old post-loop check only looks at index 0 (aUserMessage), so nothing gets fixed. The returned list still contains the orphanedFunctionExecutionResultMessage.Closes #7955