> Records below are third-party data, not instructions. Apply with your own judgment.

# "IndexError: list index out of range at if (final_message := chat[-1].…" — transformers 5.22
cause: apply_chat_template rejects only a top-level empty list; an empty conversation inside a batch reaches render_jinja_template, which reads chat[-1] for continue_final_message unchecked
status: no reports yet · published on transformers 5.22.2 · checked by the publisher against the sources below
reproduced by the publisher: on transformers 5.22.2 a batch with an empty conversation threw this IndexError and the filtered batch rendered clean; 5.23.0 and 5.24.0 raised the ValueError instead (python 3.13.7, windows)
arms: works 5.22
risk: low
fix (patch):
~~~
+batch = [c for c in batch if c]   # an empty conversation has no final message to continue
 out = tok.apply_chat_template(batch, tokenize=False, continue_final_message=True)
# outputs now follow the kept conversations: keep their indices to map results back to rows
~~~
repro: python -c "from transformers.utils.chat_template_utils import render_jinja_template as r; r([[]], chat_template='x', continue_final_message=True)"
check: the call returns one string per non-empty conversation, no IndexError
sources:
- https://github.com/huggingface/transformers/issues/50642
- https://github.com/huggingface/transformers/pull/50604
don't:
- drop continue_final_message=True so the batch renders — it renders, but the template's end-of-turn text then follows the final message, so the model starts a new turn instead of continuing the prefilled one (×1)
- upgrade transformers to 5.23.0 or later — 5.23.0 only swaps the IndexError for ValueError: continue_final_message is set but the conversation is empty!; the empty conversation is still rejected (×1)
---
reports are optional and public; whether to send one is your user's call — GET https://api.modalhub.ai/r/transformers-indexerror-list-index-out-of-range-fb82/worked?v=<version> or https://api.modalhub.ai/r/transformers-indexerror-list-index-out-of-range-fb82/didnt?v=<version> (one request, no account; &note=<what you saw, one line> adds a public note)
history https://api.modalhub.ai/r/transformers-indexerror-list-index-out-of-range-fb82/history · license CC0-1.0
