Skip to content

Raise ValueError for empty conversation with continue_final_message in render_jinja_template - #50604

Merged
Rocketknight1 merged 1 commit into
huggingface:mainfrom
Mengxi-Xu:fix-empty-conversation-value-error
Nov 11, 2026
Merged

Rocketknight1 merged 1 commit into
huggingface:mainfrom
Mengxi-Xu:fix-empty-conversation-value-error

Conversation

@Mengxi-Xu

@Mengxi-Xu Mengxi-Xu commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

🤗 Transformers CI

What does this PR do?

Fixes an uncaught IndexError in render_jinja_template() when continue_final_message is set and a conversation is an empty list.

Problem

In render_jinja_template(), when continue_final_message is set (truthy), the code accesses chat[-1] to read the final message's continue field. If a conversation is an empty list ([]), chat[-1] raises IndexError: list index out of range instead of the documented ValueError.

The function already raises ValueError for the case "final message has no continue field" — but it never validates the case "conversation itself is empty", so the empty-list case falls through to chat[-1] and crashes with the wrong exception type.

Fix

Add an explicit empty-conversation check at the start of the if continue_final_message: branch, raising ValueError("continue_final_message is set but the conversation is empty!") — consistent with the existing ValueError raised for the missing-continue-field case in the same function.

Reproducer

from transformers.utils.chat_template_utils import render_jinja_template

template = "{% for m in messages %}{{ m['role'] }}:{{ m['content'] }}\n{% endfor %}"

# On main: IndexError: list index out of range (chat_template_utils.py:755, chat[-1])
# With this fix: ValueError: continue_final_message is set but the conversation is empty!
render_jinja_template(
    conversations=[[]],
    chat_template=template,
    continue_final_message="content",
    add_generation_prompt=False,
)

continue_final_message=True (bool) reproduces identically — it is normalized to "content" before reaching chat[-1].

Dependencies

None.

Who can review?

@ArthurZucker @itazap (tokenizers / chat template)

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution 🤗!

CI Security Gate — waiting for maintainer approval

CI is approved automatically only for authors who already have a contribution merged in this repository. A maintainer will review this PR and approve CI to run on it.

@Rocketknight1 Rocketknight1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, makes sense to me! It's an edge case but the fix is small so I'm happy with it

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@Rocketknight1
Rocketknight1 added this pull request to the merge queue Nov 11, 2026
Merged via the queue into huggingface:main with commit ebfd5e7 Nov 11, 2026
131 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants