A newer version of the Gradio SDK is available: 6.24.0
Offset Contract
Every EncodedToken produced by Tokenizer.unified.DualTrackTokenizer and
its sub-tokenizers carries start and end integer fields. This document
specifies what those fields mean.
Coordinate system
startandendare Python character offsets into the original input string passed toencode_with_spans(text). They are not byte offsets and not UTF-16 code-unit offsets.- The slice
text[token.start:token.end]should round-trip — when the tokenizer can do so — to a string that re-encodes to the same token. [start, end)is half-open.start == endis allowed for zero-width tokens such as inserted boundary markers.
Special cases
- BOS, EOS, and other synthetic specials use
start = end = -1. - Byte-fallback tokens point at the original character span that
produced the bytes. A 3-byte emoji becomes 3 byte tokens that share
the same
(start, end)covering the source character. - MorphBPE tokens cover the original-text character range, including any
MVS/NNBSP/FVScontrol characters that fall inside the morpheme. When precise mapping is not possible the tokenizer uses conservative wider spans rather than raising. - Image / video patch tokens use:
start, end= the span of the<image>(or<video>) placeholder in the original text, or-1, -1if the placeholder was injected programmatically.metadata = {"image_index": N}(orvideo_index) so consumers can correlate patches to a specific image in theimages=argument.
Invariants
For every encoded sequence:
- All non-special token spans satisfy
0 <= start <= end <= len(text). - The non-special spans are weakly monotonic: each
startis>=the previous token'sstart. len(input_ids) == len(tokens) == len(attention_mask).special_tokens_mask[i] == 1ifftokens[i].track == "special".
Why this matters
The contract enables:
- Re-rendering token-level model outputs back onto the source string (highlighting, NER, alignment).
- Sanity-checking that BPE merges never silently dropped or duplicated characters.
- Aligning multimodal placeholder ranges with raw image/video inputs.