Skip to content

fix: prevent IndexError in apply_chat_template with empty conversation list#46794

Open
BB0813 wants to merge 1 commit into
huggingface:mainfrom
BB0813:fix/apply-chat-template-empty-list
Open

fix: prevent IndexError in apply_chat_template with empty conversation list#46794
BB0813 wants to merge 1 commit into
huggingface:mainfrom
BB0813:fix/apply-chat-template-empty-list

Conversation

@BB0813

@BB0813 BB0813 commented Jun 21, 2026

Copy link
Copy Markdown

Fix

Fixes #46752

Problem

apply_chat_template crashes with IndexError: list index out of range when passed an empty list:

tokenizer.apply_chat_template([], tokenize=False)
# IndexError: list index out of range

Solution

Add a len(conversation) > 0 guard before accessing conversation[0] in tokenization_utils_base.py line 3089.

Changes

  • src/transformers/tokenization_utils_base.py: Added length check before accessing conversation[0]

Testing

  • Empty list no longer raises IndexError
  • Normal conversation lists continue to work correctly

…n list

Add a length check before accessing conversation[0] to prevent
IndexError when an empty list is passed to apply_chat_template.

Fixes huggingface#46752
@github-actions

Copy link
Copy Markdown
Contributor

CI Dashboard: View test results in Grafana

⚠️ Code quality check failed — all test jobs were skipped. Fix the code quality issues and push again to run tests.

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.

apply_chat_template crashes with IndexError when passed an empty list

1 participant