Replace invalid MCP image blocks with text fallback for non-multimodal models

This commit is contained in:
Tom Wiltzius
2026-02-08 11:47:56 -08:00
parent 8fe5066bcc
commit 8473c2ac56
3 changed files with 15 additions and 5 deletions
+3 -1
View File
@@ -3921,7 +3921,9 @@ pub(crate) async fn run_turn(
error_or_panic(
"Invalid image detected; sanitizing tool output to prevent poisoning",
);
if state.history.replace_last_turn_images("Invalid image") {
if state.history.replace_last_turn_images(
"[there was an image here but it was removed for non-multimodal models]",
) {
continue;
}
let event = EventMsg::Error(ErrorEvent {
@@ -354,7 +354,9 @@ fn replace_last_turn_images_replaces_tool_output_images() {
];
let mut history = create_history_with_items(items);
assert!(history.replace_last_turn_images("Invalid image"));
assert!(history.replace_last_turn_images(
"[there was an image here but it was removed for non-multimodal models]"
));
assert_eq!(
history.raw_items(),
@@ -365,7 +367,9 @@ fn replace_last_turn_images_replaces_tool_output_images() {
output: FunctionCallOutputPayload {
body: FunctionCallOutputBody::ContentItems(vec![
FunctionCallOutputContentItem::InputText {
text: "Invalid image".to_string(),
text:
"[there was an image here but it was removed for non-multimodal models]"
.to_string(),
},
]),
success: Some(true),
@@ -388,7 +392,9 @@ fn replace_last_turn_images_does_not_touch_user_images() {
}];
let mut history = create_history_with_items(items.clone());
assert!(!history.replace_last_turn_images("Invalid image"));
assert!(!history.replace_last_turn_images(
"[there was an image here but it was removed for non-multimodal models]"
));
assert_eq!(history.raw_items(), items);
}
+3 -1
View File
@@ -597,7 +597,9 @@ async fn replaces_invalid_local_image_after_bad_request() -> anyhow::Result<()>
"second request should replace the invalid image"
);
let user_texts = second_request.message_input_texts("user");
assert!(user_texts.iter().any(|text| text == "Invalid image"));
assert!(user_texts.iter().any(|text| {
text == "[there was an image here but it was removed for non-multimodal models]"
}));
Ok(())
}