fix(frontend): Add multimodal placeholders to Gemma4 tool message template (#41459)

Signed-off-by: Harshal Janjani <harshaljanjani@gmail.com>
Co-authored-by: Ben Browning <bbrownin@redhat.com>
This commit is contained in:
Harshal Janjani
2026-05-28 14:48:12 -07:00
committed by GitHub
co-authored by Ben Browning
parent 9769e2df2a
commit 69c9f19957
2 changed files with 78 additions and 0 deletions
+9
View File
@@ -295,6 +295,15 @@
{%- endif -%}
{%- endfor -%}
{{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
{%- for part in tool_body -%}
{%- if part.get('type') == 'image' -%}
{{- '<|image|>' -}}
{%- elif part.get('type') == 'audio' -%}
{{- '<|audio|>' -}}
{%- elif part.get('type') == 'video' -%}
{{- '<|video|>' -}}
{%- endif -%}
{%- endfor -%}
{%- else -%}
{{- format_tool_response_block(ns_tname.name, tool_body) -}}
{%- endif -%}
@@ -343,3 +343,72 @@ class TestGemma4ChatTemplate:
assert '<|"|>Alice<|"|>' in result
assert "active:true" in result
assert "count:42" in result
def test_tool_response_with_multimodal_content(self, gemma4_template):
"""Multimodal placeholders in tool messages are emitted after the
tool_response block."""
messages = [
{"role": "user", "content": "Download the image and describe it."},
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {
"name": "download_image",
"arguments": '{"url": "https://example.com/x.png"}',
},
},
],
},
{
"role": "tool",
"tool_call_id": "call_1",
"content": [
{"type": "text", "text": "Image downloaded successfully."},
{"type": "image"},
],
},
]
result = _render(gemma4_template, messages, add_generation_prompt=True)
assert "<|tool_response>" in result
assert "response:download_image{" in result
assert "<tool_response|>" in result
assert "<|image|>" in result
def test_tool_response_with_all_modalities(self, gemma4_template):
"""All multimodal types (image, audio, video) in a single tool
response are rendered."""
messages = [
{"role": "user", "content": "Process media"},
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "c1",
"type": "function",
"function": {
"name": "process",
"arguments": "{}",
},
},
],
},
{
"role": "tool",
"tool_call_id": "c1",
"content": [
{"type": "text", "text": "Results."},
{"type": "image"},
{"type": "audio"},
{"type": "video"},
],
},
]
result = _render(gemma4_template, messages, add_generation_prompt=True)
assert "<|image|>" in result
assert "<|audio|>" in result
assert "<|video|>" in result