fix(vision): guard image pixel dimensions (8000px cap), not just bytes#39033
Merged
Conversation
Contributor
🔎 Lint report:
|
| Rule | Count |
|---|---|
unresolved-import |
1 |
First entries
agent/conversation_compression.py:679: [unresolved-import] unresolved-import: Cannot resolve imported module `PIL`
✅ Fixed issues: none
Unchanged: 5100 pre-existing issues carried over.
Diagnostics are surfaced as warnings — this check never fails the build.
Anthropic enforces two independent ceilings per image: 1. 5 MB encoded byte size 2. 8000 px longest side Hermes only guarded #1. A tall screenshot (e.g. 1200x12000 at 0.06 MB) passes every byte check but fails the pixel check, returning a non-retryable HTTP 400 that permanently bricks the conversation thread. Fixes: - error_classifier: add 'image dimensions exceed' pattern to _IMAGE_TOO_LARGE_PATTERNS so the 400 is classified as image_too_large and triggers the shrink/retry path instead of falling through to non-retryable error. - conversation_compression: check pixel dimensions (via Pillow) even when byte size is under the 4 MB target. If max(dims) > 8000, force shrink. - vision_tools._resize_image_for_vision: add optional max_dimension param. When set, images exceeding the pixel cap are downscaled even if they're under the byte budget. The resize loop now checks both byte AND pixel limits before accepting a candidate. Closes #37677
… Pillow Follow-up to the salvaged #37727. That PR fixed the reactive recovery path (classifier + post-failure shrinker) but left the PROACTIVE embed-time guard in vision_tools byte-only — a tall small-byte screenshot (e.g. 1200x12000 at 0.06 MB) still baked into immutable history un-resized, relying on a failed round-trip to trigger reactive shrink. - vision_tools: add _image_exceeds_dimension() + _EMBED_MAX_DIMENSION (7900px); the embed-time cap now fires on bytes OR pixels and passes max_dimension to the resizer, so tall small-byte images are shrunk before they're embedded. - vision_tools: best-effort lazy-install of Pillow (tool.vision) in the resize ImportError fallback so the soft dep self-heals (respects allow_lazy_installs). - error_classifier: add two more Anthropic dimension-cap wording variants. - pyproject + lazy_deps: declare Pillow as the [vision] extra / tool.vision lazy dep (it was undeclared everywhere; without it ALL resize recovery no-ops). - tests: cover _image_exceeds_dimension (tall/small/edge/no-Pillow/corrupt). Co-authored-by: kyssta-exe <kyssta-exe@users.noreply.github.com>
a4b2208 to
820804b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
One oversized image no longer permanently bricks a conversation thread on Anthropic. A tall screenshot (e.g. 1200×12000 @ 0.06 MB) passes every byte check but trips Anthropic's independent 8000px-per-side cap → non-retryable HTTP 400 → every subsequent turn replays the immutable image and re-fails.
Salvages @kyssta-exe's #37727 (cherry-picked, authorship preserved) + adds the proactive embed-time fix #37727 missed.
Changes
agent/error_classifier.py: add"image dimensions exceed"+ two wording variants to_IMAGE_TOO_LARGE_PATTERNSso the dimension 400 is classifiedimage_too_largeand routed into the shrink/retry path. (contributor + follow-up)tools/vision_tools.py:_resize_image_for_visiongains amax_dimensionparam (pre-cap + loop check). (contributor)_image_exceeds_dimension()+_EMBED_MAX_DIMENSION=7900; the proactive embed cap now fires on bytes OR pixels and passesmax_dimension, so a tall small-byte image is shrunk before it's baked into immutable history — not just reactively after a failed round-trip. (follow-up — the gap fix(vision): guard image pixel dimensions, not just bytes #37677 #37727 left open)agent/conversation_compression.py: reactive shrinker decodes + checks pixels when bytes are fine. (contributor)pyproject.toml+tools/lazy_deps.py+uv.lock: declare Pillow as the[vision]extra /tool.visionlazy dep — it was undeclared everywhere; without it ALL resize recovery silently no-ops. (follow-up)tests/: contributor's shrink-recovery tests + 5 new_image_exceeds_dimensioncases.Validation
_image_exceeds_dimension(1200×12000)(800×600)False; edge/no-Pillow/corrupt → False_resize_image_for_vision(tall, max_dim=7900)test_vision_tools.py/test_image_shrink_recovery.py/test_error_classifier.pyuv lock --checkpillowadded)Closes #37677. Supersedes #37727 (cherry-picked with authorship preserved).
Infographic