Skip to content

fix: retokenize a Document after its text changes - #583

Merged
aksg87 merged 4 commits into
google:mainfrom
mittalpk:fix/document-tokenized-text-stale-cache
Nov 4, 2026
Merged

aksg87 merged 4 commits into
google:mainfrom
mittalpk:fix/document-tokenized-text-stale-cache

Conversation

@mittalpk

Copy link
Copy Markdown
Contributor

Description

Document.tokenized_text cached its first tokenization, but text is a plain public field, so assigning new text kept the old tokens. Extraction then prompted with the old text and aligned against it, producing MATCH_EXACT intervals that point at the wrong span of the returned text.

The getter now compares the cached tokenization's text with the current text and tokenizes again when they differ. The same check is applied to AnnotatedDocument. A tokenization set explicitly for the current text is still kept.

Fixes #580

Bug fix

How Has This Been Tested?

Added three tests to tests/data_lib_test.py: a Document and an AnnotatedDocument re-tokenize after their text changes, and a custom tokenization for unchanged text is kept. The first two fail without the fix. The issue's offline reproduction now extracts Bob with a correct interval.

$ pytest tests
984 passed, 5 failed (create_provider_plugin_test.py, also failing on main in my environment: they need pip network access)

Checklist:

  • I have read and acknowledged Google's Open Source
    Code of conduct.
  • I have read the
    Contributing
    page, and I either signed the Google
    Individual CLA
    or am covered by my company's
    Corporate CLA.
  • I have discussed my proposed solution with code owners in the linked
    issue(s) and we have agreed upon the general approach.
  • I have made any needed documentation changes, or noted in the linked
    issue(s) that documentation elsewhere needs updating.
  • I have added tests, or I have ensured existing tests cover the changes
  • I have followed
    Google's Python Style Guide
    and ran pylint over the affected code.

@github-actions github-actions Bot added the size/XS Pull request with less than 50 lines changed label Oct 23, 2026
@github-actions

Copy link
Copy Markdown

⚠️ Branch Update Required

Your branch is 1 commits behind main. Please update your branch to ensure CI checks run with the latest code:

git fetch origin main
git merge origin/main
git push

Note: Enable "Allow edits by maintainers" to allow automatic updates.

@aksg87

aksg87 commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

Thanks @mittalpk. I merged main, cleared the AnnotatedDocument cache when text is set to None, and parameterized the tests.

@aksg87
aksg87 merged commit 06836b1 into google:main Nov 4, 2026
21 of 22 checks passed
@aksg87 aksg87 mentioned this pull request Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XS Pull request with less than 50 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: changing Document.text after extraction reuses stale tokens and produces incorrect exact matches

2 participants