fix(dataset): pair Anthropic tool_result blocks with their tool_use by id - #10293
Open
Zhuoxi2000 wants to merge 1 commit into
Open
Zhuoxi2000 wants to merge 1 commit into
Zhuoxi2000 wants to merge 1 commit into
Conversation
Anthropic pairs tool_result blocks with tool_use blocks by tool_use_id, so a parallel result batch may arrive in a different order from the calls. anthropic_to_messages discarded the ids and emitted positional tool_response messages in arrival order, pairing each result with the wrong call. Align a complete result batch (one result per tool_use, distinct ids) to the tool_use order before the ids are discarded, as normalize_openai_tool_calls does for OpenAI tool_calls (modelscope#10174). The blocks are reordered rather than the emitted messages, so images inside the results stay consistent with their <image> placeholders. Incomplete or id-less batches are left as-is.
This branch has not been 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.
PR type
PR information
Anthropic
tool_resultblocks are matched totool_useblocks bytool_use_id, so the results of parallel calls can arrive in a different order from the calls.MessagesPreprocessor.anthropic_to_messagesdropped the ids and emitted positionaltool_responsemessages in arrival order. With results [B, A] for calls [A, B], each call was paired with the other call's result. #10174 fixed the same problem for OpenAItool_callsinnormalize_openai_tool_calls; this PR does the same for the Anthropic path.Changes in
swift/dataset/preprocessor/core.py:MessagesPreprocessor._align_anthropic_tool_results. It takes thetool_useblocks of the previous message and the leading run oftool_resultblocks in the current message. If the results form a complete batch (one result pertool_use, distinct non-null ids, the same id set), it sorts them intotool_useorder. Other cases are left as they are, including legacy data without ids and partial batches. The guard is the same as in fix: preserve OpenAI parallel tool-result associations #10174.anthropic_to_messagesremembers thetool_useblocks of the previous list-content message and aligns the next message's blocks before converting them.The content blocks are reordered, not the emitted messages. Images inside a
tool_resultare appended toimageswhile the blocks are converted, so they stay in the same order as their<image>placeholders. Within a reordered batch, that order is the aligned call order. Text blocks after the results (a user follow-up in the same message) stay after the tool responses. The input row is not modified in place.No separate issue; the bug is described above.
Experiment results
New tests in
tests/general/test_data_preprocess.py::TestProviderMessagesPreprocess:test_anthropic_parallel_tool_results_out_of_orderpasses two results to two calls, in order and reversed, and expectstool_responsein call order.test_anthropic_out_of_order_tool_results_keep_images_aligneduses two image-bearing results in reverse order, followed by a user text block. It checks that the<image>placeholders, theimageslist and the trailing user message all line up.Before (main @ 02baac9, fix reverted):
After:
flake8 7.3.0, isort 8.0.1 and yapf 0.43.0 (the versions in
.pre-commit-config.yaml) report nothing on the changed files.