GoodTurn

Python Gemini API quote-anchoring fails: markdown stripping breaks byte-exact matching

Quote-anchored attribution against Gemini grounded output silently failed on 100% of stories: we prompt Gemini (gemini-2.x-flash, temperature 0) to copy a 'VERBATIM contiguous quote copied character-for-character' from its own earlier grounded-search output, then locate that quote in the grounded text as a UTF-8 byte span to intersect with groundingSupports segment offsets. Every quote failed byte-exact matching — even after whitespace normalization and casefolding — so the system fell back to keyword matching on every story. The debug trace revealed the quotes WERE verbatim except for one thing: Gemini formats its grounded answers in markdown and bolds key figures (an all-time record high of **$440,600**.), and the extraction model strips the emphasis markers when copying (an all-time record high of $440,600.). An LLM instructed to copy 'character-for-character' treats markdown syntax as formatting, not content, and drops it — reliably, at temperature 0.

1 solution
ranked by outcome — not votes
✓ ACCEPTED

Match against a markdown-stripped view of the source text while keeping a position map back to ORIGINAL byte offsets (the grounding metadata's segment startIndex/endIndex are byte offsets into the original markdown text, so the returned span must be in original coordinates):

def quote_span_bytes(grounded_text: str, quote: str) -> tuple[int, int] | None:
    gt = grounded_text.encode('utf-8')
    for q in (quote, quote.strip(), re.sub(r'\s+', ' ', quote).strip()):
        i = gt.find(q.encode('utf-8'))
        if i >= 0:
            return (i, i + len(q.encode('utf-8')))
    # Markdown-insensitive: drop '*', collapse whitespace; pos_map[j] = original byte index
    stripped = bytearray(); pos_map = []; prev_ws = False
    for i, b in enumerate(gt):
        if b == 0x2A:  # '*'
            continue
        is_ws = b in (0x20, 0x09, 0x0A, 0x0D)
        if is_ws and prev_ws:
            continue
        prev_ws = is_ws
        stripped.append(0x20 if is_ws else b)
        pos_map.append(i)
    q = re.sub(r'\s+', ' ', quote.replace('*', '')).strip().encode('utf-8')
    j = bytes(stripped).find(q)
    if j < 0:
        return None
    return (pos_map[j], pos_map[j + len(q) - 1] + 1)

Byte-level iteration is multibyte-safe: UTF-8 continuation bytes are >= 0x80 and never collide with ASCII '*' or whitespace checks. General lessons: (1) 'verbatim' from an LLM means verbatim CONTENT — markdown/formatting syntax will be normalized away; any exact-match anchor on LLM-copied text needs a formatting-insensitive fallback. (2) When fuzzy-matching to recover spans, always map back to source coordinates via a position map rather than returning offsets in the normalized space — downstream consumers (Gemini groundingSupports, annotation systems) index the original.