Skip to content

language_model: Fix ImageSize storing pre-downscale dimensions#54357

Merged
MrSubidubi merged 1 commit intozed-industries:mainfrom
dastrobu:fix/image-size-after-downscale
Apr 21, 2026
Merged

language_model: Fix ImageSize storing pre-downscale dimensions#54357
MrSubidubi merged 1 commit intozed-industries:mainfrom
dastrobu:fix/image-size-after-downscale

Conversation

@dastrobu
Copy link
Copy Markdown
Contributor

@dastrobu dastrobu commented Apr 20, 2026

When images are resized to meet provider size constraints (Anthropic's 1568px limit or the 5MB encoded-PNG cap), the stored ImageSize was still recording the original width/height rather than the final post-downscale dimensions. This caused incorrect token estimation via estimate_tokens() since it uses width * height / 750.

Use processed_image.dimensions() after all downscale passes so that ImageSize reflects the actual image sent to the provider.

Release Notes:

  • Fixed an issue where token estimation would be incorrect in case where the thread contained downscaled images.

@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Apr 20, 2026
@SomeoneToIgnore SomeoneToIgnore added the area:ai Related to Agent Panel, Edit Prediction, Copilot, or other AI features label Apr 20, 2026
Copy link
Copy Markdown
Member

@MrSubidubi MrSubidubi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

This seems like a good fix, can we add a test for this to make sure this does not regress?

When images are resized to meet provider size constraints (Anthropic's
1568px limit or the 5MB encoded-PNG cap), the stored ImageSize was still
recording the original width/height rather than the final post-downscale
dimensions. This caused incorrect token estimation via estimate_tokens()
since it uses width * height / 750.

Use processed_image.dimensions() after all downscale passes so that
ImageSize reflects the actual image sent to the provider.

Release Notes:

- N/A
@dastrobu dastrobu force-pushed the fix/image-size-after-downscale branch from 37d10b6 to 282feaf Compare April 21, 2026 04:29
@dastrobu
Copy link
Copy Markdown
Contributor Author

This seems like a good fix, can we add a test for this to make sure this does not regress?

@MrSubidubi absolutely, thanks for the quick feedback. Test cases is updated, please review again.

@dastrobu dastrobu requested a review from MrSubidubi April 21, 2026 04:34
@MrSubidubi MrSubidubi changed the title Fix ImageSize storing pre-downscale dimensions language_model: Fix ImageSize storing pre-downscale dimensions Apr 21, 2026
Copy link
Copy Markdown
Member

@MrSubidubi MrSubidubi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find, fix and test update. Thank you for this contribution!

@MrSubidubi MrSubidubi merged commit f2f9e27 into zed-industries:main Apr 21, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ai Related to Agent Panel, Edit Prediction, Copilot, or other AI features cla-signed The user has signed the Contributor License Agreement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants