Skip to content

DecodeSentences._transform() crashes with IndexError — constrained decoding does not enforce sentence index bounds #885

Closed

Description

@lastras

File: mellea/formatters/granite/intrinsics/output.py, DecodeSentences._transform(), lines 714–722
Also relevant: mellea/formatters/granite/intrinsics/input.py, _mark_sentence_boundaries() / _transform()
Commit: c0b163c


Summary

The citations intrinsic crashes with IndexError: list index out of range when the model outputs a sentence index r or c that exceeds the number of sentence boundary markers actually present in the answer/documents. This is expected to be prevented by constrained decoding, but it is not — because the sentence count bounds are computed during input formatting and never propagated into the JSON schema used for constrained decoding.


Root Cause

The citations formatter has two stages.

Input stage (input.py, _mark_sentence_boundaries()): NLTK tokenizes the last assistant message and all documents, inserting <r0>, <r1>, ... and <c0>, <c1>, ... markers. After this runs, the exact sentence counts are known:

  • r valid range: [0, n_response_sentences - 1]
  • c valid range: [0, n_total_doc_sentences - 1] (global index across all documents — <c0>.<ck-1> where k = total sentences across all docs)

These counts are computed locally inside _mark_sentence_boundaries() but are never returned or used further. The index variable that accumulates the total document sentence count is simply discarded.

Schema construction (input.py, __init__()): The structured_outputs schema is set once at construction time from the static io.yaml:

"r": { "minimum": 0, "type": "integer" },
"c": { "items": { "minimum": 0, "type": "integer" }, "type": "array" }

No "maximum" is ever set. Constrained decoding therefore enforces only that r and c values are non-negative integers — the upper bound is never provided.

Output stage (output.py, DecodeSentences._transform(), lines 714–722): Indexes into begins/ends/texts using the raw model-output integer with no bounds check:

sentence_num = value
result[self.begin_name]       = begins[sentence_num]       # IndexError when OOB
result[self.end_name]         = ends[sentence_num]
result[self.text_name]        = texts[sentence_num]
result[self.document_id_name] = document_ids[sentence_num]

Minimal Reproduction (no live model required)

from mellea.formatters.granite.intrinsics.output import _desplit_sentences, DecodeSentences
from unittest.mock import MagicMock

rule = DecodeSentences.__new__(DecodeSentences)
rule.config     = {"sentence_boundaries": {"last_message": "r", "documents": "c"},
                   "instruction": False, "docs_as_message": False}
rule.source     = ["last_message"]
rule.begin_name = "response_begin"
rule.end_name   = "response_end"
rule.text_name  = "response_text"

mock_msg = MagicMock(); mock_msg.content = "<r0> Only one sentence here."
mock_cc  = MagicMock(); mock_cc.messages = [mock_msg]; mock_cc.extra_body = None

prepare_output = rule._prepare(MagicMock(), MagicMock(), None, mock_cc)
assert len(prepare_output["begins"]) == 1  # only r=0 exists

rule._transform(1, ("root", 0, "r"), prepare_output)
# → IndexError: list index out of range

Observed in Production

Intermittently during multi-turn RAG pipelines with top_k=20 retrieved documents. The pipeline completes generation successfully but crashes during citations post-processing.

Activity

  1. added
    area/adapter-functions
    area/stdlib
    enhancement
    area/component
    area/formatter
    on May 20, 2026
  2. added
    p2
    on Jun 18, 2026
  3. planetf1 commented on Oct 20, 2026

    @planetf1
    Contributor

    Closing as absorbed by #1719. Its fix item 2 bounds-checks DecodeSentences, which is exactly this crash site: an out-of-range index becomes a ValueError naming the index and the sentence count, and a negative index stops silently picking a sentence from the end. The deeper idea from this issue, propagating sentence-count bounds into the constrained-decoding schema so the model can't emit out-of-range indices at all, is deliberately out of scope there. If the new ValueError starts firing in practice, please reopen and we'll take the schema route.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/adapter-functionsarea/componentarea/formatterarea/stdlibenhancementp2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions