Skip to content

Fix AssistantTracker.is_active() returning False after activation with empty lists#44524

Merged
Rocketknight1 merged 1 commit intohuggingface:mainfrom
jnMetaCode:fix/chat-template-is-active-truthiness
Mar 9, 2026
Merged

Fix AssistantTracker.is_active() returning False after activation with empty lists#44524
Rocketknight1 merged 1 commit intohuggingface:mainfrom
jnMetaCode:fix/chat-template-is-active-truthiness

Conversation

@jnMetaCode
Copy link
Contributor

Summary

Fixes a bug in AssistantTracker.is_active() in chat_template_utils.py.

After activation via activate_tracker(), _rendered_blocks and _generation_indices are set to list arguments which may be empty []. The is_active() method checks truthiness (return self._rendered_blocks or self._generation_indices), which returns False for empty lists, incorrectly indicating the tracker is inactive.

This means calling activate_tracker with empty lists, then checking is_active(), returns False even though the tracker was activated. This breaks the re-entrance guard in activate_tracker which relies on is_active() to detect double activation.

# Before:
def is_active(self) -> bool:
    return self._rendered_blocks or self._generation_indices

# After:
def is_active(self) -> bool:
    return self._rendered_blocks is not None or self._generation_indices is not None

Test plan

  • Verified _rendered_blocks is initialized as None and set to list on activation
  • Verified is_active() is used as re-entrance guard in activate_tracker
  • Existing tests should continue to pass

After activation, _rendered_blocks and _generation_indices are set
to list arguments which may be empty []. The previous truthiness
check returned False for empty lists, incorrectly indicating the
tracker was inactive. Use `is not None` instead to correctly
distinguish between unset (None) and active-but-empty ([]) state.

Signed-off-by: JiangNan <1394485448@qq.com>
Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

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

Yes, LGTM! Thanks for the fix.

@Rocketknight1 Rocketknight1 enabled auto-merge (squash) March 9, 2026 13:26
@Rocketknight1 Rocketknight1 merged commit deb7dcd into huggingface:main Mar 9, 2026
26 checks passed
@HuggingFaceDocBuilderDev

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.

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