Fix apply_chat_template crash on tool_call messages without content#45348
Fix apply_chat_template crash on tool_call messages without content#45348qgallouedec merged 7 commits intomainfrom
apply_chat_template crash on tool_call messages without content#45348Conversation
| elif content["type"] == "image_url": | ||
| if isinstance(content, str): | ||
| parsed["content"].append({"type": "text", "text": content}) | ||
| elif isinstance(content, list): |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
content_block name is also use elsewhere in the codebase
|
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. |
…nsformers into content-optional
zucchini-nlp
left a comment
There was a problem hiding this comment.
Nice as per processing changes, I can't say much about the chat template diff, so let's wait for the expert @Rocketknight1
| 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)): |
There was a problem hiding this comment.
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.
| 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"] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
zucchini-nlp
left a comment
There was a problem hiding this comment.
Approving then, so you can merge
|
[For maintainers] Suggested jobs to run (before merge) run-slow: smolvlm |
apply_chat_template crash on tool_call messages without content
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.Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@zucchini-nlp @Rocketknight1