Skip to content

fix(telegram): route image documents (.webp, .png, .jpg, .gif) through image handling (#20128)#20147

Closed
Sanjays2402 wants to merge 4 commits into
NousResearch:mainfrom
Sanjays2402:fix/issue-20128
Closed

fix(telegram): route image documents (.webp, .png, .jpg, .gif) through image handling (#20128)#20147
Sanjays2402 wants to merge 4 commits into
NousResearch:mainfrom
Sanjays2402:fix/issue-20128

Conversation

@Sanjays2402

Copy link
Copy Markdown
Contributor

Closes #20128.

When a user sent an image to Telegram as a document (instead of as a normal photo), Hermes rejected formats like .webp with Unsupported document type '.webp' even though the same file uploaded as a photo would have worked. Existing handling only considered SUPPORTED_DOCUMENT_TYPES and SUPPORTED_VIDEO_TYPES.

Fix

  • Added SUPPORTED_IMAGE_DOCUMENT_TYPES mapping (.jpg, .jpeg, .png, .webp, .gif) on gateway/platforms/base.py.
  • New image-document branch in gateway/platforms/telegram.py's msg.document path: resolves extension from filename or MIME type (so docs without filenames still work), downloads bytes, caches via cache_image_from_bytes(...), populates event.media_urls / event.media_types, sets message_type = MessageType.PHOTO, and routes through the existing photo enqueue path.
  • Reuses _enqueue_photo_event / _queue_media_group_event so albums / bursts of mixed image-documents merge into a single event, identical to native msg.photo uploads.
  • Updated the unsupported-type message to list the union of document and image extensions (sorted).

Test

10 new cases in TestImageDocuments (within tests/gateway/test_telegram_documents.py) covering: each of the 5 image extensions, MIME-only resolution, album buffering, mixed image+photo albums, and the updated unsupported-type message. pytest tests/gateway/test_telegram_documents.py → 49 passed.

Notes

  • MessageType only defines PHOTO (not IMAGE), so used PHOTO to match the existing photo path — image documents now produce identical events to native photos.
  • No CHANGELOG.md existed in the repo before this PR; created one with ## Unreleased / ### Fixes to host the entry.
  • Existing video / generic-document handling is untouched.

Image extensions (.jpg/.jpeg/.png/.webp/.gif) sometimes arrive on
platforms as 'documents' rather than native photo attachments
(e.g. Telegram users picking from the file picker, sticker exporters
wrapping images as files). Centralize the ext -> MIME mapping in base
so adapters can route those bytes through the image pipeline instead
of rejecting them as unsupported documents.

Refs NousResearch#20128
Telegram users who upload an image via the file picker (instead of
the native photo button) deliver the image as msg.document. Common
formats like .webp were rejected with 'Unsupported document type'
because the document branch only checked SUPPORTED_DOCUMENT_TYPES /
SUPPORTED_VIDEO_TYPES.

Add a SUPPORTED_IMAGE_DOCUMENT_TYPES branch that downloads the bytes,
caches them via cache_image_from_bytes, sets media_urls/media_types,
flips message_type to PHOTO, and routes through the existing
photo-batch / media-group buffering so the vision/image pipeline picks
them up exactly like native photos. The MIME-to-extension fallback now
also recognizes image MIME types when the filename is missing, and the
unsupported-type message lists the new image extensions.

Fixes NousResearch#20128
Adds TestImageDocuments covering the regression in NousResearch#20128:

* .webp document is downloaded, cached, and emitted as a PHOTO event
  via the photo-batching path.
* .jpg / .jpeg / .png / .gif documents take the same path
  (parametrized).
* Captions are preserved on the merged event.
* Multi-image albums (media_group_id) collapse into one PHOTO event
  with both media_urls.
* MIME-only resolution works when filename is missing.
* The 'Unsupported document type' message now lists image extensions.
* Download exceptions don't crash the handler.

Also adds IMAGE_CACHE_DIR redirection to the autouse cache fixture so
the new tests don't write into ~/.hermes.
@alt-glitch alt-glitch added type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter P2 Medium — degraded but workaround exists labels May 5, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #16710 — same fix routing image documents (.webp/.png/.jpg/.gif) through the photo pipeline on Telegram. See also #18819, #19446, #18142.

@teknium1

Copy link
Copy Markdown
Contributor

Automated hermes-sweeper review: this Telegram image-document fix is already implemented on current main.

Evidence:

  • gateway/platforms/base.py:1151 defines SUPPORTED_IMAGE_DOCUMENT_TYPES for .jpg, .jpeg, .png, .webp, and .gif.
  • gateway/platforms/telegram.py:5648 resolves Telegram document extensions from filename, then MIME type fallback.
  • gateway/platforms/telegram.py:5678 routes image documents through cache_image_from_bytes, sets MessageType.PHOTO, fills media_urls / media_types, and uses the existing photo/media-group batching paths.
  • Implementing commit: 77c4675a50db7abbfd191d4fba4746b4f3e1559e (fix(telegram): route image documents (.png/.jpg/.webp/.gif) through vision pipeline).
  • git tag --contains 77c4675a50db7abbfd191d4fba4746b4f3e1559e shows the fix is included in v2026.5.28.

Thanks for the contribution and for tying it back to #20128. The maintainer note that this duplicated #16710 matches what is now present on main.

@teknium1 teknium1 closed this Jun 11, 2026
@teknium1 teknium1 added the sweeper:implemented-on-main Sweeper: behavior already present on current main label Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists platform/telegram Telegram bot adapter sweeper:implemented-on-main Sweeper: behavior already present on current main type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Telegram: image documents like .webp are rejected as unsupported documents instead of being routed through image handling

3 participants