Skip to content

Fix apply_chat_template crash on tool_call messages without content#45348

Merged
qgallouedec merged 7 commits intomainfrom
content-optional
Apr 13, 2026
Merged

Fix apply_chat_template crash on tool_call messages without content#45348
qgallouedec merged 7 commits intomainfrom
content-optional

Conversation

@qgallouedec
Copy link
Copy Markdown
Member

What does this PR do?

Fixes #45290

Code Agent Policy

The Transformers repo is currently being overwhelmed by a large number of PRs and issue comments written by
code agents. We are currently bottlenecked by our ability to review and respond to them. As a result,
we ask that new users do not submit pure code agent PRs at this time.
You may use code agents in drafting or to help you diagnose issues. We'd also ask autonomous "OpenClaw"-like agents
not to open any PRs or issues for the moment.

PRs that appear to be fully agent-written will probably be closed without review, and we may block users who do this
repeatedly or maliciously.

This is a rapidly-evolving situation that's causing significant shockwaves in the open-source community. As a result,
this policy is likely to be updated regularly in the near future. For more information, please read CONTRIBUTING.md.

  • I confirm that this is not a pure code agent PR.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@zucchini-nlp @Rocketknight1

elif content["type"] == "image_url":
if isinstance(content, str):
parsed["content"].append({"type": "text", "text": content})
elif isinstance(content, list):
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

elif instead of else because content can also be None now

if isinstance(content, str):
parsed["content"].append({"type": "text", "text": content})
elif isinstance(content, list):
for content_block in content:
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

content_block name is also use elsewhere in the codebase

@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.

@qgallouedec qgallouedec changed the title Make content truly optional Fix apply_chat_template crash on tool_call messages without content Apr 9, 2026
Copy link
Copy Markdown
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

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

Nice as per processing changes, I can't say much about the chat template diff, so let's wait for the expert @Rocketknight1

Comment on lines -544 to +547
final_message = chat[-1]["content"]
if isinstance(final_message, (list, tuple)):
final_message = chat[-1].get("content")
if final_message is None:
raise ValueError("continue_final_message is set but the final message has no content to continue!")
elif isinstance(final_message, (list, tuple)):
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.

@Rocketknight1 to confirm on these

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.

Yes, this makes sense to me. continue_final_message was something we initially added for text-only models, so it's not really designed to handle situations where the final message isn't just text to be continued. Erroring out when there's no content is fine.

Comment on lines -930 to +934
if isinstance(message["content"], str):
parsed["content"] = message["content"]
elif isinstance(message["content"], list):
texts = [c["text"] for c in message["content"] if c["type"] == "text"]
if isinstance(content, str):
parsed["content"] = content
elif isinstance(content, list):
texts = [c["text"] for c in content if c["type"] == "text"]
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.

so what happens to the tool-usage into when a tool-LLM is used in cli? Do we not need to re-direct it inside text?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The tool_calls / tool_call_id fields are not forwarded by this func but that's a pre-existing gap, not introduced by this PR.

This function only transforms the content field. It already dropped all other message fields before this change.

I think forwarding tool_calls/tool_call_id through to apply_chat_template so multi-turn tool-use conversations template correctly should be its own PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

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

Approving then, so you can merge

@github-actions
Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: smolvlm

@qgallouedec qgallouedec changed the title Fix apply_chat_template crash on tool_call messages without content Fix apply_chat_template crash on tool_call messages without content Apr 13, 2026
@qgallouedec qgallouedec added this pull request to the merge queue Apr 13, 2026
Merged via the queue into main with commit e40b0c0 Apr 13, 2026
29 checks passed
@qgallouedec qgallouedec deleted the content-optional branch April 13, 2026 19:02
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(tokenize=True) crashes on assistant messages with tool calls and no content

4 participants