Skip to content

fix(eval, models): close unspaced-script clusters and pair id-less tool results with invalid-id calls - #8366

Open
NUAAwqy wants to merge 1 commit into
google:mainfrom
NUAAwqy:fix/unicode-tokenizer-and-tool-id-sanitizer
Open

NUAAwqy wants to merge 1 commit into
google:mainfrom
NUAAwqy:fix/unicode-tokenizer-and-tool-id-sanitizer

Conversation

@NUAAwqy

@NUAAwqy NUAAwqy commented Nov 25, 2026 •

Copy link
Copy Markdown

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_score failed correct Thai, Lao, Khmer and Myanmar answers
  • Fixes #8297 — _ToolUseIdSanitizer could not pair an id-less tool_result with a call whose id was invalid-but-present

1. Fixes #8299 — unspaced-script clusters were never closed, and their punctuation was kept as words

File: src/google/adk/evaluation/final_response_match_v1.py

Root cause

The non-spaced-script branch of _UnicodeAwareTokenizer.tokenize opened 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.

ราคา100   -> ['ร', 'า', 'ค', 'า100']
ราคา 100  -> ['ร', 'า', 'ค', 'า', '100']

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:

ទេ។   -> ['ទេ', '។']   vs  ទេ   -> ['ទេ']     # 0.67
฿100  -> ['฿100']       vs  100  -> ['100']    # 0.00

។ (U+17D4, Po), ။ (U+104B, Po) and ฿ (U+0E3F, Sc) are separators, not words.

Fix

  • The loop now tracks whether an unspaced cluster is still open and emits a space before a following digit or Latin word, so the cluster is always closed.
  • New helper _is_script_separator classifies category P/S characters of those blocks as separators; they are treated exactly like punctuation in any other script.
  • Combining marks still attach to the character they follow, so existing clusters (ดี, ດີ, ល្អ, မင်္ဂလာ) are unchanged, and Thai digits (๐) remain word characters.

2. Fixes #8297 — invalid-id calls were not registered for name-based pairing

File: src/google/adk/models/anthropic_llm.py

Root cause

The class is documented to pair a tool_use with its tool_result across 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 when tool_id was falsy.

So when the paired result arrived with tool_id="", the by-name queue was empty and the result got a brand-new fallback:

sanitize(is_call=True,  tool_id="call.hivemind:1", tool_name="get_weather")  -> toolu_fallback_0
sanitize(is_call=False, tool_id="",                tool_name="get_weather")  -> toolu_fallback_1

The converted request then contained a tool_result whose tool_use_id matched no tool_use block, which Anthropic rejects with 400 ... 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

  • Every fallbacked call is registered for name-based pairing, whether its id was absent or merely invalid — the sanitizer is the component that makes both forms acceptable to Anthropic.
  • Claiming a fallback through the call's own id now withdraws it from the pending registries, so a call that was already answered cannot be claimed a second time by an id-less result of the same tool.
  • Only calls enter the pending registries. A result whose id was never seen still gets a fallback of its own, but it is not left pending, so a later ID-less result cannot be paired with a result instead of a call.
  • Two small helpers (_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) and tests/unittests/models/test_anthropic_llm.py (+176), following the existing style:

  • tokenizer cases for digits and Latin words after Thai/Lao/Khmer/Myanmar letters, including the two forms that already worked, and the 100บาท reversed order;
  • tokenizer cases for ។, ။, ฿, plus _is_script_separator parametrised over punctuation, symbols, letters, marks and digits;
  • RougeEvaluator cases 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 (ราคา100 vs ราคา 200 = 0.80, not 1.0);
  • sanitizer unit cases for the mixed invalid-id + id-less form, multiple calls in one turn, the absent-id control, the answered-call case, and the stray-result case (a result with an unknown invalid id must not become a pairing target);
  • end-to-end generate_content_async conversion cases asserting tool_use.id and tool_result.tool_use_id lists 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).

FAILED ...::test_unicode_aware_tokenizer_ignores_spacing_after_unspaced_script[ราคา100-ราคา 100]
FAILED ...::test_unicode_aware_tokenizer_splits_digits_after_thai_word
FAILED ...::test_unicode_aware_tokenizer_drops_unspaced_script_punctuation[ទេ។-ទេ]
FAILED ...::test_rouge_evaluator_ignores_spacing_and_punctuation_of_script_words[thai-digits]
FAILED ...::test_tool_use_id_sanitizer_pairs_id_less_result_with_call[id-present-but-invalid]
FAILED ...::test_generate_content_async_pairs_id_less_results_with_invalid_calls[invalid-then-id-less]
29 failed, 9 passed

After the fix:

tests/unittests/evaluation/test_final_response_match_v1.py
tests/unittests/models/test_anthropic_llm.py
405 passed, 3 warnings in 8.82s          (was 355 passed)

pytest tests/unittests -k 'response_match or rouge'  -> 147 passed, 8 skipped
pytest tests/unittests -k 'anthropic'                -> 404 passed, 8 skipped

The issue's reproduction script now prints 1.00 PASSED on all ten lines, and the #8297 snippet pairs toolu_fallback_0 with toolu_fallback_0. The only failures in the wider tests/unittests run are pre-existing litellm/agentplatform/antigravity collection errors and two test_litellm.py assertions, all reproduced on the unmodified checkout.

Both files pass pyink (repo config: line-length 80, 2-space indent).

Notes

  • No public API change; _is_script_separator is a private module-level helper next to _is_cjk / _is_non_spaced_script / _is_word_char.
  • Zero-width joiners inside a word (Memory search splits words at ZWJ/ZWNJ and matches unrelated Bengali and Hindi text #8181) are deliberately untouched.
  • pyink --check, isort --check-only, codespell and scripts/compliance_checks.py are clean on the four touched files. mypy reports the same 2 pre-existing errors before and after the change, and ruff check reports one finding on them, an unused PrebuiltMetrics import in the evaluation test file, which is also already present on unmodified main and left as is.

…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
NUAAwqy force-pushed the fix/unicode-tokenizer-and-tool-id-sanitizer branch from 376d18a to 770beed Compare November 25, 2026 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant