Repository navigation
Conversation
…ol results Fixes google#8299 Fixes google#8297 Two independent defects, both in code paths that silently turn a correct answer or a correct request into a failure. 1) response_match_score failed correct Thai, Lao, Khmer and Myanmar answers The non-spaced-script branch of `_UnicodeAwareTokenizer.tokenize` opened a grapheme cluster with a leading space but never closed it, so a digit or a Latin word written directly after the script's letters was glued to the last cluster: `ราคา100` tokenized as `['ร','า','ค','า100']` while `ราคา 100` tokenized as `['ร','า','ค','า','100']`, and the same answer scored 0.67 against the 0.8 pass mark. The reversed order (`100บาท`) and Chinese (`价格100`) were already split, so only the trailing form was broken. The same branch classified every character of those Unicode blocks as word material, so their punctuation and symbols became tokens of their own: Khmer `។`, Myanmar `။` and Thai `฿` were kept as words, while punctuation in every other script is dropped. `ទេ។` therefore scored 0.67 against `ទេ`, and `฿100` scored 0.0 against `100`. The tokenizer now tracks whether an unspaced cluster is open and emits a boundary before a following digit or Latin word, and a new `_is_script_separator` helper treats the punctuation and symbols of those blocks as separators. Combining marks still attach to the character they follow, so clusters are unchanged, and Thai digits remain word characters. 2) _ToolUseIdSanitizer could not pair an id-less tool_result with a call whose id was present but invalid The class is documented to pair a tool_use with its tool_result across turns, but a call with an invalid id (`call.hivemind:1`) was only recorded in the id -> fallback mapping, never in the name-based pending registries, which were filled only when `tool_id` was falsy. The paired result then arrived with `tool_id=""`, found an empty by-name queue and was handed a brand-new fallback, so the converted request contained a `tool_result` whose `tool_use_id` matched no `tool_use` block. Anthropic rejects that with `400 ... tool_result block references unknown tool_use_id`. The same pair worked when the call's id was absent entirely. Every fallbacked call is now registered for name-based pairing whether its id was absent or merely invalid, since the sanitizer is what makes both forms acceptable to Anthropic. Claiming a fallback through the call's own id withdraws it from the pending registries, so a call that was already answered is not claimed a second time by an id-less result of the same tool. Tests - New tokenizer and RougeEvaluator cases cover the digits/Latin-word cases and the Khmer, Myanmar and Thai punctuation cases from google#8299, plus the two controls that already worked, and a case asserting that different digits after the letters are still compared (score 4/5, not 1.0). - New sanitizer unit cases and end-to-end conversion cases cover the mixed invalid-id + id-less-result form from google#8297, several calls in one turn, the absent-id control, and the answered-call case. - 29 of the new tests fail on the previous source and all pass after the change; `tests/unittests/evaluation/test_final_response_match_v1.py` and `tests/unittests/models/test_anthropic_llm.py` go from 355 to 404 passing.
NUAAwqy
force-pushed
the
fix/unicode-tokenizer-and-tool-id-sanitizer
branch
from
November 25, 2026 20:14
376d18a to
770beed
Compare
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
Two independent defects, both in code paths that silently turn a correct answer or a correct request into a failure. Each is fixed with a focused change plus regression tests.
Fixes #8299—response_match_scorefailed correct Thai, Lao, Khmer and Myanmar answersFixes #8297—_ToolUseIdSanitizercould not pair an id-lesstool_resultwith a call whose id was invalid-but-present1.
Fixes #8299— unspaced-script clusters were never closed, and their punctuation was kept as wordsFile:
src/google/adk/evaluation/final_response_match_v1.pyRoot cause
The non-spaced-script branch of
_UnicodeAwareTokenizer.tokenizeopened a grapheme cluster by prepending a space but never emitted a boundary after it, and it classified every character of those Unicode blocks as word material.a) A digit or Latin word written directly after the letters was glued to the last cluster.
Same answer, ROUGE-1 = 0.67 against the 0.8 pass mark. The reversed order (
100บาท) and Chinese (价格100) were already split, so only the trailing form was broken.b) The blocks' own punctuation and symbols became tokens of their own, while punctuation in every other script is dropped:
។(U+17D4, Po),။(U+104B, Po) and฿(U+0E3F, Sc) are separators, not words.Fix
_is_script_separatorclassifies categoryP/Scharacters of those blocks as separators; they are treated exactly like punctuation in any other script.ดี,ດີ,ល្អ,မင်္ဂလာ) are unchanged, and Thai digits (๐) remain word characters.2.
Fixes #8297— invalid-id calls were not registered for name-based pairingFile:
src/google/adk/models/anthropic_llm.pyRoot cause
The class is documented to pair a
tool_usewith itstool_resultacross turns, but a call with an id that was present but invalid (call.hivemind:1) was recorded only in_mapping; the name-based pending registries (_unpaired_calls_by_name/_unpaired_calls_list) were filled only whentool_idwas falsy.So when the paired result arrived with
tool_id="", the by-name queue was empty and the result got a brand-new fallback:The converted request then contained a
tool_resultwhosetool_use_idmatched notool_useblock, which Anthropic rejects with400 ... tool_result block references unknown tool_use_id. The same pair worked when the call's id was absent entirely, which isolated the bug to the invalid-but-present form.Fix
_new_fallback,_withdraw) replace the repeated allocation and registry-pruning code. No API change.Tests
tests/unittests/evaluation/test_final_response_match_v1.py(+155) andtests/unittests/models/test_anthropic_llm.py(+176), following the existing style:100บาทreversed order;។,။,฿, plus_is_script_separatorparametrised over punctuation, symbols, letters, marks and digits;RougeEvaluatorcases for all ten pairs from the issue report (expecting 1.00 / PASSED at the default 0.8 mark), and a case asserting different digits are still compared (ราคา100vsราคา 200= 0.80, not 1.0);generate_content_asyncconversion cases assertingtool_use.idandtool_result.tool_use_idlists match exactly and match[a-zA-Z0-9_-]+.Verification
Before the fix (new tests run against the unpatched source): 29 failed, 9 passed. The 9 that pass are the intended controls (the absent-id baseline, the stray-result case, and the orders that already tokenized correctly).
After the fix:
The issue's reproduction script now prints
1.00 PASSEDon all ten lines, and the #8297 snippet pairstoolu_fallback_0withtoolu_fallback_0. The only failures in the widertests/unittestsrun are pre-existinglitellm/agentplatform/antigravitycollection errors and twotest_litellm.pyassertions, all reproduced on the unmodified checkout.Both files pass
pyink(repo config: line-length 80, 2-space indent).Notes
_is_script_separatoris a private module-level helper next to_is_cjk/_is_non_spaced_script/_is_word_char.pyink --check,isort --check-only,codespellandscripts/compliance_checks.pyare clean on the four touched files.mypyreports the same 2 pre-existing errors before and after the change, andruff checkreports one finding on them, an unusedPrebuiltMetricsimport in the evaluation test file, which is also already present on unmodifiedmainand left as is.