Repository navigation
Fix apply_chat_template crash on tool_call messages without content
#45348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
edc4677
eead60a
2af897d
ba6909f
f22e75c
1033e04
ec04c37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -930,24 +930,25 @@ def get_processor_inputs_from_messages(messages: list[dict], modality: Modality) | |
| for message in messages: | ||
| parsed = {"role": message["role"], "content": []} | ||
|
|
||
| content = message.get("content") | ||
| if modality == Modality.LLM: | ||
| 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"] | ||
| parsed["content"] = " ".join(texts) | ||
|
|
||
| elif modality == Modality.VLM: | ||
| if isinstance(message["content"], str): | ||
| parsed["content"].append({"type": "text", "text": message["content"]}) | ||
| else: | ||
| for content in message["content"]: | ||
| if content["type"] == "text": | ||
| parsed["content"].append(content) | ||
| elif content["type"] == "image_url": | ||
| if isinstance(content, str): | ||
| parsed["content"].append({"type": "text", "text": content}) | ||
| elif isinstance(content, list): | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| for content_block in content: | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if content_block["type"] == "text": | ||
| parsed["content"].append(content_block) | ||
| elif content_block["type"] == "image_url": | ||
| from PIL import Image | ||
|
|
||
| url = content["image_url"]["url"] | ||
| url = content_block["image_url"]["url"] | ||
| if "base64" in url: | ||
| image_data = re.sub("^data:image/.+;base64,", "", url) | ||
| image = Image.open(BytesIO(base64.b64decode(image_data))) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -543,8 +543,10 @@ def render_jinja_template( | |
| chat = chat.messages | ||
| if continue_final_message: | ||
| chat = deepcopy(chat) | ||
| 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)): | ||
|
Comment on lines
-546
to
+549
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Rocketknight1 to confirm on these
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this makes sense to me. |
||
| for content_block in reversed(final_message): | ||
| if "text" in content_block: | ||
| # Pick the last text block in the message (the first one we hit while iterating in reverse) | ||
|
|
||
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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_templateso multi-turn tool-use conversations template correctly should be its own PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#45418