fix: auto-compress screenshots to reduce context window size#4751
Open
PHclaw wants to merge 2 commits into
Open
fix: auto-compress screenshots to reduce context window size#4751PHclaw wants to merge 2 commits into
PHclaw wants to merge 2 commits into
Conversation
Problem: Without llm_screenshot_size configured, screenshots were passed through at full resolution (~500KB-2MB for 1920x1080 PNG). Multiple screenshots per conversation quickly exhaust the context window, causing API 400/413 errors. Fix: - _resize_screenshot() now always compresses images (not just when llm_screenshot_size is configured) - Default max width: 1280px (preserves ~95% size reduction for typical screenshots) - JPEG format for non-transparent images (~85% compression vs PNG) - PNG only for images with transparency (RGBA/LA/P modes) - Return type changed to tuple(base64_str, media_type) for correct MIME type - Call site updated to use dynamic media type in data URL Closes browser-use#4742
Author
|
This PR fixes the context overflow by always compressing screenshots to 1280px JPEG by default. A 1920x1080 screenshot goes from ~1MB PNG to ~80KB JPEG, a 90%+ reduction. |
Contributor
There was a problem hiding this comment.
1 issue found across 1 file
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="browser_use/agent/prompts.py">
<violation number="1" location="browser_use/agent/prompts.py:400">
P0: Unterminated string literal causes a module-level SyntaxError, preventing this file from importing.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review, or fix all with cubic.
|
👋 This PR has been automatically marked as stale because it hasn't had activity for 45 days. To keep this PR open:
This will be automatically closed in 14 days if no further activity occurs. Thanks for contributing to browser-use! 🤖 |
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
Without llm_screenshot_size configured, screenshots were passed through at full resolution (~500KB-2MB for 1920x1080 PNG). Multiple screenshots per conversation quickly exhaust the context window, causing API 400/413 errors.
Changes
_resize_screenshot() (prompts.py)
Before: Returned screenshot as-is when llm_screenshot_size was None.
After: Always compresses images:
Call site (prompts.py)
Updated to unpack the tuple and use dynamic media_type in the data URL:
`python
processed_screenshot, media_type = self._resize_screenshot(screenshot)
data:{media_type};base64,{processed_screenshot}
`
Impact
Closes #4742
Summary by cubic
Auto-compress screenshots to shrink LLM context payloads and stop 400/413 errors with multiple images. Always resize and set the correct image format and media type.
llm_screenshot_sizewhen set, otherwise scale to 1280px width (typical 1920×1080 drops to ~50–100KB)._resize_screenshotnow returns(base64, media_type); call site usesdata:{media_type};base64,....image/png.Written for commit 3dea9d6. Summary will update on new commits. Review in cubic