Add a unified image budget (#37206)

## What changed

- Add the gated `unified_image_budget` feature for models that support original image detail or Responses Lite.
- Apply one 6,000-pixel, 10,000-patch preprocessing limit regardless of legacy image detail hints.
- Hide detail controls from `view_image` and code mode while continuing to accept existing hints for compatibility.
- Preserve detail-based resizing and tool contracts for unsupported models.

## Testing

- Cover unified resizing limits, Responses Lite, legacy detail hints, unsupported models, and `view_image` integration in code mode.

GitOrigin-RevId: cb07bff1669a96599fdfd076b3d9ec80f2b6fff7
This commit is contained in:
Curtis 'Fjord' Hawthorne authored and copyberry committed 2026-08-06 03:15:08 +00:00
1 parent bc8b25ea02
commit 0a0ebb8535
13 files changed
+466 -52

No files matched your search

+16 -8
View File
@@ -34,6 +34,7 @@ impl Default for ViewImageHandler {
Self {
options: ViewImageToolOptions {
can_request_original_image_detail: false,
unified_image_budget: false,
include_environment_id: false,
},
}
@@ -120,8 +121,7 @@ impl ViewImageHandler {
environment_id,
detail,
} = parse_arguments(&arguments)?;
// `high` is the explicit spelling of the default resized path.
// Other string values remain invalid rather than being silently reinterpreted.
// Keep accepting previously supported detail hints after they disappear from the schema.
let detail = match detail.as_deref() {
None => None,
Some("high") => Some(ViewImageDetail::High),
@@ -175,8 +175,8 @@ impl ViewImageHandler {
})?;
let can_request_original_detail = can_request_original_image_detail(&turn.model_info);
let use_original_detail =
can_request_original_detail && matches!(detail, Some(ViewImageDetail::Original));
let use_original_detail = self.options.unified_image_budget
|| can_request_original_detail && matches!(detail, Some(ViewImageDetail::Original));
let image_detail = if use_original_detail {
ImageDetail::Original
} else {
@@ -196,6 +196,7 @@ impl ViewImageHandler {
Ok(boxed_tool_output(ViewImageOutput {
image_url,
image_detail,
unified_image_budget: self.options.unified_image_budget,
}))
}
}
@@ -205,6 +206,7 @@ impl CoreToolRuntime for ViewImageHandler {}
pub struct ViewImageOutput {
image_url: String,
image_detail: ImageDetail,
unified_image_budget: bool,
}
impl ToolOutput for ViewImageOutput {
@@ -234,10 +236,14 @@ impl ToolOutput for ViewImageOutput {
}
fn code_mode_result(&self, _payload: &ToolPayload) -> serde_json::Value {
serde_json::json!({
"image_url": self.image_url,
"detail": self.image_detail
})
if self.unified_image_budget {
serde_json::json!({ "image_url": self.image_url })
} else {
serde_json::json!({
"image_url": self.image_url,
"detail": self.image_detail
})
}
}
}
@@ -283,6 +289,7 @@ mod tests {
let output = ViewImageOutput {
image_url: "data:image/png;base64,AAA".to_string(),
image_detail: DEFAULT_IMAGE_DETAIL,
unified_image_budget: false,
};
assert_eq!(output.log_preview(), "<image data URL omitted: 25 bytes>");
@@ -293,6 +300,7 @@ mod tests {
let output = ViewImageOutput {
image_url: "data:image/png;base64,AAA".to_string(),
image_detail: DEFAULT_IMAGE_DETAIL,
unified_image_budget: false,
};
let result = output.code_mode_result(&ToolPayload::Function {
@@ -9,6 +9,7 @@ use std::collections::BTreeMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ViewImageToolOptions {
pub can_request_original_image_detail: bool,
pub unified_image_budget: bool,
pub include_environment_id: bool,
}
@@ -17,7 +18,7 @@ pub fn create_view_image_tool(options: ViewImageToolOptions) -> ToolSpec {
"path".to_string(),
JsonSchema::string(Some("Local filesystem path to an image file.".to_string())),
)]);
if options.can_request_original_image_detail {
if options.can_request_original_image_detail && !options.unified_image_budget {
properties.insert(
"detail".to_string(),
JsonSchema::string_enum(
@@ -45,25 +46,29 @@ pub fn create_view_image_tool(options: ViewImageToolOptions) -> ToolSpec {
strict: false,
defer_loading: None,
parameters: JsonSchema::object(properties, Some(vec!["path".to_string()]), Some(false.into())),
output_schema: Some(view_image_output_schema()),
output_schema: Some(view_image_output_schema(options)),
})
}
fn view_image_output_schema() -> Value {
json!({
fn view_image_output_schema(options: ViewImageToolOptions) -> Value {
let mut schema = json!({
"type": "object",
"properties": {
"image_url": {
"type": "string",
"description": "Data URL for the loaded image."
},
"detail": {
"type": "string",
"enum": ["high", "original"],
"description": "Image detail hint returned by view_image. Returns `high` for default resized behavior or `original` when original resolution is preserved."
}
},
"required": ["image_url", "detail"],
"required": ["image_url"],
"additionalProperties": false
})
});
if !options.unified_image_budget {
schema["properties"]["detail"] = json!({
"type": "string",
"enum": ["high", "original"],
"description": "Image detail hint returned by view_image. Returns `high` for default resized behavior or `original` when original resolution is preserved."
});
schema["required"] = json!(["image_url", "detail"]);
}
schema
}