mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Report prompt image resizing to the model (#37134)
## What changed - Add the disabled-by-default `image_resize_notice` feature. - When enabled, append a developer message after resized images from user messages or tool outputs. The notice identifies each image and reports its original and prepared dimensions. - Persist notices with new history while avoiding backfilled notices for images processed when a session is resumed. ## Testing - Cover notice placement, image numbering, failed images, user and tool image sources, and resumed-session replay. GitOrigin-RevId: 11dd808b3ad47eac35e9e2f4a4370f85d0122770
This commit is contained in:
1 parent
2994f545a7
commit
fa5d5ae047
10 files changed
+677
-60
No files matched your search
@@ -51,7 +51,7 @@ fn preparation_preserves_small_image_bytes_and_replaces_remote_urls() {
|
||||
internal_chat_message_metadata_passthrough: None,
|
||||
}];
|
||||
|
||||
prepare_response_items(&mut items);
|
||||
prepare_response_items(&mut items, ImageResizeNoticeMode::Disabled);
|
||||
|
||||
let ResponseItem::Message { content, .. } = &items[0] else {
|
||||
panic!("expected message");
|
||||
@@ -105,7 +105,7 @@ fn detail_policies_apply_the_expected_budgets() {
|
||||
internal_chat_message_metadata_passthrough: None,
|
||||
}];
|
||||
|
||||
let metadata = prepare_response_items(&mut items);
|
||||
let metadata = prepare_response_items(&mut items, ImageResizeNoticeMode::Disabled);
|
||||
|
||||
let ResponseItem::Message { content, .. } = &items[0] else {
|
||||
panic!("expected message");
|
||||
@@ -144,7 +144,7 @@ fn preparation_reports_tool_output_item_id() {
|
||||
]),
|
||||
internal_chat_message_metadata_passthrough: None,
|
||||
}];
|
||||
let metadata = prepare_response_items(&mut items);
|
||||
let metadata = prepare_response_items(&mut items, ImageResizeNoticeMode::Disabled);
|
||||
|
||||
assert_eq!(
|
||||
metadata,
|
||||
@@ -160,6 +160,136 @@ fn preparation_reports_tool_output_item_id() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resize_notices_preserve_original_image_positions_and_skip_failed_images() {
|
||||
let (large_image_url, _) = png_data_url(/*width*/ 2048, /*height*/ 2048);
|
||||
let (small_image_url, _) = png_data_url(/*width*/ 64, /*height*/ 32);
|
||||
let mut items = vec![
|
||||
ResponseItem::Message {
|
||||
id: None,
|
||||
role: "user".to_string(),
|
||||
content: vec![
|
||||
ContentItem::InputImage {
|
||||
image_url: small_image_url,
|
||||
detail: Some(ImageDetail::High),
|
||||
},
|
||||
ContentItem::InputImage {
|
||||
image_url: "data:image/png;base64,%%%".to_string(),
|
||||
detail: Some(ImageDetail::High),
|
||||
},
|
||||
ContentItem::InputImage {
|
||||
image_url: large_image_url.clone(),
|
||||
detail: Some(ImageDetail::High),
|
||||
},
|
||||
],
|
||||
phase: None,
|
||||
internal_chat_message_metadata_passthrough: None,
|
||||
},
|
||||
ResponseItem::FunctionCallOutput {
|
||||
id: None,
|
||||
call_id: "call-image".to_string(),
|
||||
output: FunctionCallOutputPayload::from_content_items(vec![
|
||||
FunctionCallOutputContentItem::InputImage {
|
||||
image_url: "data:image/png;base64,%%%".to_string(),
|
||||
detail: Some(ImageDetail::High),
|
||||
},
|
||||
FunctionCallOutputContentItem::InputImage {
|
||||
image_url: large_image_url,
|
||||
detail: Some(ImageDetail::High),
|
||||
},
|
||||
]),
|
||||
internal_chat_message_metadata_passthrough: None,
|
||||
},
|
||||
];
|
||||
|
||||
prepare_response_items(&mut items, ImageResizeNoticeMode::Enabled);
|
||||
let expected_user_notice = concat!(
|
||||
"<image_resize_notice>\n",
|
||||
"Image 3 of 3 in the preceding user message was resized from 2048x2048 to 1600x1600 pixels.\n",
|
||||
"</image_resize_notice>"
|
||||
);
|
||||
|
||||
let ResponseItem::Message { content, .. } = &items[0] else {
|
||||
panic!("expected message");
|
||||
};
|
||||
let [
|
||||
ContentItem::InputImage {
|
||||
image_url: small_message_image_url,
|
||||
..
|
||||
},
|
||||
ContentItem::InputText {
|
||||
text: failed_message_image,
|
||||
},
|
||||
ContentItem::InputImage {
|
||||
image_url: resized_message_image_url,
|
||||
..
|
||||
},
|
||||
] = content.as_slice()
|
||||
else {
|
||||
panic!("expected unchanged image, failed image placeholder, and resized image");
|
||||
};
|
||||
assert_eq!(
|
||||
decoded_image(small_message_image_url).1.dimensions(),
|
||||
(64, 32)
|
||||
);
|
||||
assert_eq!(failed_message_image, IMAGE_PROCESSING_ERROR_PLACEHOLDER);
|
||||
assert_eq!(
|
||||
decoded_image(resized_message_image_url).1.dimensions(),
|
||||
(1600, 1600)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
&items[1],
|
||||
&ResponseItem::Message {
|
||||
id: None,
|
||||
role: "developer".to_string(),
|
||||
content: vec![ContentItem::InputText {
|
||||
text: expected_user_notice.to_string(),
|
||||
}],
|
||||
phase: None,
|
||||
internal_chat_message_metadata_passthrough: None,
|
||||
}
|
||||
);
|
||||
|
||||
let ResponseItem::FunctionCallOutput { output, .. } = &items[2] else {
|
||||
panic!("expected function call output");
|
||||
};
|
||||
let [
|
||||
FunctionCallOutputContentItem::InputText {
|
||||
text: failed_tool_image,
|
||||
},
|
||||
FunctionCallOutputContentItem::InputImage {
|
||||
image_url: resized_tool_image_url,
|
||||
..
|
||||
},
|
||||
] = output.content_items().expect("tool output content items")
|
||||
else {
|
||||
panic!("expected failed image placeholder and resized image in the tool output");
|
||||
};
|
||||
assert_eq!(failed_tool_image, IMAGE_PROCESSING_ERROR_PLACEHOLDER);
|
||||
assert_eq!(
|
||||
decoded_image(resized_tool_image_url).1.dimensions(),
|
||||
(1600, 1600)
|
||||
);
|
||||
assert_eq!(
|
||||
&items[3],
|
||||
&ResponseItem::Message {
|
||||
id: None,
|
||||
role: "developer".to_string(),
|
||||
content: vec![ContentItem::InputText {
|
||||
text: concat!(
|
||||
"<image_resize_notice>\n",
|
||||
"Image 2 of 2 in the preceding tool output was resized from 2048x2048 to 1600x1600 pixels.\n",
|
||||
"</image_resize_notice>"
|
||||
)
|
||||
.to_string(),
|
||||
}],
|
||||
phase: None,
|
||||
internal_chat_message_metadata_passthrough: None,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preparation_replaces_only_failed_tool_images_and_preserves_metadata() {
|
||||
let (valid_image_url, _) = png_data_url(/*width*/ 64, /*height*/ 32);
|
||||
@@ -195,7 +325,7 @@ fn preparation_replaces_only_failed_tool_images_and_preserves_metadata() {
|
||||
internal_chat_message_metadata_passthrough: None,
|
||||
}];
|
||||
|
||||
prepare_response_items(&mut items);
|
||||
prepare_response_items(&mut items, ImageResizeNoticeMode::Disabled);
|
||||
|
||||
assert_eq!(
|
||||
items,
|
||||
|
||||
Reference in new issue
Block a user