fix(sanitize): preserve assistant messages with tool_calls when stripping images
_strip_images_from_messages() deleted any non-tool message whose content became empty after image removal. An assistant message whose content was entirely images but which carried tool_calls was therefore dropped, orphaning its paired tool responses — providers reject the next request with unmatched tool_call_id errors (HTTP 400). Replace such messages with the plaintext placeholder instead, exactly like tool-role messages. Adds a regression test covering the assistant + tool_calls + image-only-content case. Closes #40463
This commit is contained in:
@@ -412,9 +412,11 @@ def _strip_images_from_messages(messages: list) -> bool:
|
||||
with a plaintext placeholder, NOT deleted — deleting them would leave
|
||||
the paired ``tool_call_id`` on the prior assistant message unmatched,
|
||||
which providers reject with HTTP 400.
|
||||
* Non-tool messages whose content becomes empty are dropped. In
|
||||
practice this only hits synthetic image-only user messages appended
|
||||
for attachment delivery; real user turns always include text.
|
||||
* Assistant messages carrying ``tool_calls`` are likewise replaced, not
|
||||
deleted — dropping them would orphan their tool responses.
|
||||
* Other messages whose content becomes empty are dropped. In practice
|
||||
this only hits synthetic image-only user messages appended for
|
||||
attachment delivery; real user turns always include text.
|
||||
|
||||
This runs on the persistent history as well as the per-call copy, so any
|
||||
message it rewrites must also lose its ``api_content`` sidecar: the sidecar
|
||||
@@ -443,13 +445,15 @@ def _strip_images_from_messages(messages: list) -> bool:
|
||||
if len(new_parts) < len(content):
|
||||
if new_parts:
|
||||
msg["content"] = new_parts
|
||||
elif msg.get("role") == "tool":
|
||||
# Preserve tool_call_id linkage — providers require every
|
||||
# assistant tool_call to have a matching tool response.
|
||||
elif msg.get("role") == "tool" or msg.get("tool_calls"):
|
||||
# Preserve message linkage — providers require every assistant
|
||||
# tool_call to have a matching tool response, and an assistant
|
||||
# message carrying tool_calls must survive even if its content
|
||||
# was entirely images.
|
||||
msg["content"] = "[image content removed — server does not support images]"
|
||||
else:
|
||||
# Synthetic image-only user/assistant message with no text;
|
||||
# safe to drop.
|
||||
# Synthetic image-only user/assistant message with no text and
|
||||
# no tool_calls; safe to drop.
|
||||
to_delete.append(i)
|
||||
# Content was rewritten — the pre-strip sidecar is now stale.
|
||||
drop_stale_api_content(msg)
|
||||
|
||||
@@ -83,6 +83,53 @@ class TestStripImagesPreservesAlternation:
|
||||
assert msgs[2]["content"] == [{"type": "text", "text": "Captured 1024x768"}]
|
||||
assert msgs[2]["tool_call_id"] == "call_1"
|
||||
|
||||
def test_assistant_with_tool_calls_and_image_only_content_preserved(self):
|
||||
"""Assistant messages carrying tool_calls must NEVER be deleted —
|
||||
dropping them would orphan the paired tool responses, which providers
|
||||
reject with unmatched tool_call_id errors.
|
||||
"""
|
||||
msgs = [
|
||||
{"role": "user", "content": "annotate this screenshot"},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": [
|
||||
{"type": "image_url", "image_url": {"url": "data:image/png;base64,..."}},
|
||||
],
|
||||
"tool_calls": [{
|
||||
"id": "call_xyz",
|
||||
"type": "function",
|
||||
"function": {"name": "annotate", "arguments": "{}"},
|
||||
}],
|
||||
},
|
||||
{"role": "tool", "tool_call_id": "call_xyz", "content": "done"},
|
||||
]
|
||||
changed = _strip_images_from_messages(msgs)
|
||||
assert changed is True
|
||||
# Length preserved — assistant message with tool_calls NOT deleted
|
||||
assert len(msgs) == 3
|
||||
assert msgs[1]["tool_calls"][0]["id"] == "call_xyz"
|
||||
# Content replaced with text placeholder (now a string, not a list)
|
||||
assert isinstance(msgs[1]["content"], str)
|
||||
assert "image content removed" in msgs[1]["content"].lower()
|
||||
# Paired tool response still matches
|
||||
assert msgs[2]["tool_call_id"] == "call_xyz"
|
||||
|
||||
def test_image_only_user_message_dropped(self):
|
||||
"""Synthetic image-only user messages (gateway injection pattern) are
|
||||
safe to drop — no tool_call_id linkage to preserve."""
|
||||
msgs = [
|
||||
{"role": "user", "content": "what's in this?"},
|
||||
{"role": "assistant", "content": "I'll check."},
|
||||
{
|
||||
"role": "user",
|
||||
"content": [{"type": "image_url", "image_url": {"url": "data:..."}}],
|
||||
},
|
||||
]
|
||||
changed = _strip_images_from_messages(msgs)
|
||||
assert changed is True
|
||||
# Synthetic image-only user message dropped
|
||||
assert len(msgs) == 2
|
||||
assert msgs[-1]["role"] == "assistant"
|
||||
|
||||
def test_multiple_tool_messages_all_preserved(self):
|
||||
"""Parallel tool calls: each tool_call_id must retain a paired message."""
|
||||
|
||||
Reference in New Issue
Block a user