Skip to content

The agent that grows with you

fix(vision): review follow-ups — LA/PA JPEG guard, third embed site

kshitijk4poor30d4555085ec
diff --git a/tests/tools/test_vision_tools.py b/tests/tools/test_vision_tools.py
index 58a6d8d0044..45d9ed75773 100644
--- a/tests/tools/test_vision_tools.py
+++ b/tests/tools/test_vision_tools.py
@@ -804,6 +804,42 @@ class TestResizeImageForVision:
)
assert result.startswith("data:image/png;base64,")
+ def test_force_jpeg_handles_alpha_and_exotic_modes(self, tmp_path):
+ """RGBA/LA/P PNGs must convert cleanly — JPEG can't encode alpha.
+
+ force_jpeg newly routes PNG inputs to the JPEG encoder, so modes
+ JPEG can't save (LA grayscale+alpha especially) must be normalized
+ to RGB instead of crashing img.save().
+ """
+ try:
+ from PIL import Image
+ except ImportError:
+ pytest.skip("Pillow not installed")
+ import random
+
+ rng = random.Random(7)
+ for mode, size in (("RGBA", (600, 400)), ("LA", (600, 400)), ("P", (600, 400))):
+ img = Image.new(mode, size)
+ # Noise so the PNG exceeds a tiny budget and the resize fires.
+ if mode == "P":
+ img.putpalette([i % 256 for i in range(768)])
+ img.putdata([rng.randrange(256) for _ in range(size[0] * size[1])])
+ else:
+ bands = len(img.getbands())
+ img.putdata([
+ tuple(rng.randrange(256) for _ in range(bands))
+ for _ in range(size[0] * size[1])
+ ])
+ path = tmp_path / f"img_{mode}.png"
+ img.save(path, "PNG")
+
+ result = _resize_image_for_vision(
+ path, mime_type="image/png",
+ max_base64_bytes=16 * 1024, max_dimension=1568,
+ force_jpeg=True,
+ )
+ assert result.startswith("data:image/jpeg;base64,"), mode
+
# ---------------------------------------------------------------------------
# _image_exceeds_dimension — proactive embed-time pixel-cap detector
diff --git a/tools/browser_use_cli.py b/tools/browser_use_cli.py
index 0c99f42c467..294f11bc68a 100644
--- a/tools/browser_use_cli.py
+++ b/tools/browser_use_cli.py
@@ -418,13 +418,25 @@ def _native_screenshot_result(result: Dict[str, Any], path: str) -> Optional[Dic
from pathlib import Path
from tools.vision_tools import (
+ _EMBED_MAX_DIMENSION,
+ _EMBED_TARGET_BYTES,
_resize_image_for_vision,
_should_use_native_vision_fast_path,
)
if not _should_use_native_vision_fast_path():
return None
- data_url = _resize_image_for_vision(Path(path))
+ # History-reuse cap (#92699): this data URL bakes into the tool
+ # result and is re-sent on every later turn — same policy as the
+ # vision_analyze / browser_vision native embeds (256 KB / 1568 px,
+ # JPEG quality ladder instead of PNG dimension-halving).
+ data_url = _resize_image_for_vision(
+ Path(path),
+ mime_type="image/png",
+ max_base64_bytes=_EMBED_TARGET_BYTES,
+ max_dimension=_EMBED_MAX_DIMENSION,
+ force_jpeg=True,
+ )
text = json.dumps(result, ensure_ascii=False)
return {
"_multimodal": True,
diff --git a/tools/vision_tools.py b/tools/vision_tools.py
index e950bbb506a..8d05c153e6f 100644
--- a/tools/vision_tools.py
+++ b/tools/vision_tools.py
@@ -887,8 +887,10 @@ def _resize_image_for_vision(image_path: Path, mime_type: Optional[str] = None,
if data_url is None:
data_url = _image_to_base64_data_url(image_path, mime_type=mime_type)
return data_url # fall through to size-check in caller
- # Convert RGBA to RGB for JPEG output
- if pil_format == "JPEG" and img.mode in {"RGBA", "P"}:
+ # JPEG cannot encode alpha or palette/grayscale-alpha modes; normalize
+ # anything that isn't already plain RGB/grayscale. force_jpeg newly
+ # routes PNG inputs here, so exotic modes (LA/PA) must not crash save().
+ if pil_format == "JPEG" and img.mode not in {"RGB", "L"}:
img = img.convert("RGB")
# Strategy: halve dimensions until both base64 fits AND pixel dimensions