fix(gateway): route Telegram image-MIME documents through the photo cache path - #20700
Closed
jackControls wants to merge 1 commit into
Closed
fix(gateway): route Telegram image-MIME documents through the photo cache path#20700jackControls wants to merge 1 commit into
jackControls wants to merge 1 commit into
Conversation
Collaborator
…ache path When a Telegram user picks "Send as file" instead of "Compress images", python-telegram-bot delivers the PNG/JPG as msg.document with an image/* MIME type, not as msg.photo. The current document handler has no entry for image MIMEs in SUPPORTED_DOCUMENT_TYPES, so the file falls through to the "Unsupported document type 'unknown'" branch and the vision pipeline (native attach / vision_analyze) never sees the bytes. Models receive only the caption text and tend to hallucinate explanations like "couldn't read PNG as text" since they have no image to describe. Add an early branch in the existing elif msg.document: handler that detects image/* MIME types and routes them through the same cache_image_from_bytes + photo-batch enqueue path as msg.photo. After the patch, photos and image-as-files behave identically end-to-end. Recognized: image/jpeg, image/jpg, image/png, image/webp, image/gif, image/heic, image/heif, image/bmp.
jackControls
force-pushed
the
fix/telegram-image-as-document
branch
from
May 6, 2026 12:15
f59e206 to
42b93ba
Compare
Author
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.
What & Why
When a Telegram user sends an image with the "Send as file" option (or, on mobile, sends without ticking "Compress images"), python-telegram-bot delivers it as
msg.documentwith animage/*MIME type — not asmsg.photo.The current document handler in
gateway/platforms/telegram.pyhas no entry for image MIMEs inSUPPORTED_DOCUMENT_TYPES. So:extis set to""(noimage/png→.pngmapping)if ext not in SUPPORTED_DOCUMENT_TYPES:fires"Unsupported document type 'unknown'"vision_analyze) never sees the bytesThis is surprising for users who do tick "Compress images" — they do see the image — but breaks the moment they uncompress.
Fix: add a small early branch in the existing
elif msg.document:handler that detectsimage/*MIME types and routes them through the samecache_image_from_bytes+ photo-batch enqueue path asmsg.photo. ~30 lines, lives in the existing document branch (no new top-level handlers), and reuses the existing_photo_batch_key/_enqueue_photo_eventplumbing. Photos and image-as-files behave identically end-to-end after this lands.Recognized image MIMEs / extensions:
image/jpeg,image/jpg,image/png,image/webp,image/gif,image/heic,image/heif,image/bmp.How to test
With
agent.image_input_mode: "native"+ a multimodal main model (Qwen-VL, etc.):.heic/.gif/.webpdocument — same behaviourapplication/pdfdocument — still hits the existing PDF handling path (no regression)video/mp4document — still hits the existing video handling path (no regression)Tested on