Skip to content

fix(telegram): fail fast when user images cannot be accessed - #22413

Open
HiddenPuppy wants to merge 2 commits into
NousResearch:mainfrom
HiddenPuppy:fix/issue-22385-telegram-image-failfast
Open

HiddenPuppy wants to merge 2 commits into
NousResearch:mainfrom
HiddenPuppy:fix/issue-22385-telegram-image-failfast

Conversation

@HiddenPuppy

Copy link
Copy Markdown
Contributor

Summary

When a user sends an image via Telegram and the vision analysis pipeline fails (e.g. no Gemini key configured, transient API error), the agent previously received either an empty user message or a hint telling it to retry with vision_analyze. This caused the agent to spend 30+ minutes in futile retry loops (issue #22385).

Root Cause

Two gaps conspired to create the loop:

  1. If the Telegram gateway cached a photo successfully but the _enrich_message_with_vision pipeline subsequently failed for all images, the agent received the user's caption text (or an empty string if there was no caption), plus a hint telling it to try vision_analyze — which would fail again with the same reason, causing another retry.
  2. If a pending/queued event had no media_urls at all, _build_media_placeholder returned an empty string, giving the agent nothing to work with.

Changes

File Change
gateway/platforms/telegram.py Normalize MIME type image/jpgimage/jpeg to use standard MIME types
gateway/run.py_enrich_message_with_vision Track all_failed across all images. When ALL analyses fail AND the user provided no caption, return a clear fail-fast message ([The user sent an image but I'm unable to see it...]) instead of prompting the agent to retry
gateway/run.py_build_media_placeholder Return a neutral fallback string instead of empty string when there are no media URLs at all

Testing

  • pytest tests/gateway/ — 751 passed, 1 skipped, 1 pre-existing failure (unrelated DingTalk test)

Fixes #22385

When a user sends an image via Telegram and the vision analysis
pipeline fails (e.g. no Gemini key configured), the agent previously
received either an empty user message or a hint telling it to retry
with vision_analyze — causing up to 30+ minutes of futile retries.

Changes:
- Normalize MIME type 'image/jpg' → 'image/jpeg' in the Telegram
  photo handler to avoid non-standard MIME types
- In _enrich_message_with_vision: track all_failed across all images.
  When ALL analyses fail AND the user provided no caption, return a
  clear fail-fast message instead of prompting the agent to retry
- In _build_media_placeholder: return a neutral fallback string
  instead of empty string when there are no media_urls at all,
  preventing the agent from receiving empty user input

Fixes NousResearch#22385
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery platform/telegram Telegram bot adapter labels May 9, 2026

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for isolating the post-cache failure path. The core issue remains on current main: gateway/run.py:15054-15065 still tells the model to retry vision_analyze after a failed automatic analysis.

Problems

  • The Telegram hunk is stale: the live adapter moved from gateway/platforms/telegram.py to plugins/platforms/telegram/adapter.py; the active MIME assignment is now at plugins/platforms/telegram/adapter.py:7801. GitHub currently reports this PR as CONFLICTING.
  • The diff adds no regression tests. Existing _enrich_message_with_vision coverage in tests/gateway/test_vision_memory_leak.py covers successful analyses, not the all-failed and exception paths this change relies on.

Suggested changes

  • Transplant the MIME fix to the bundled Telegram adapter, using its existing extension-to-MIME map at plugins/platforms/telegram/adapter.py:236-242.
  • Add focused tests for all-failed/no-caption, exception/no-caption, and mixed success/failure image inputs, plus the empty-media placeholder if retained.

Automated hermes-sweeper review.

cached_path = cache_image_from_bytes(bytes(image_bytes), ext=ext)
event.media_urls = [cached_path]
event.media_types = [f"image/{ext.lstrip('.')}" ]
# Normalize MIME: "image/jpg" → "image/jpeg" (standard MIME)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This handler has moved on current main to plugins/platforms/telegram/adapter.py:7737-7813, where the live assignment still emits image/jpg at line 7801. Please transplant this normalization there (the plugin already defines _TELEGRAM_IMAGE_EXT_TO_MIME at lines 236-242) and cover it with a regression test.

Comment thread gateway/run.py
@@ -12171,6 +12180,7 @@ async def _enrich_message_with_vision(
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please add regression coverage for this new all-failed path: success: false and thrown exceptions with no caption should return the fail-fast text, while a mixed success/failure batch must preserve the successful description and the original caption behavior.

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:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:risk-message-delivery Sweeper risk: may drop, duplicate, misroute, or suppress messages type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Telegram: images sent by user are not accessible — agent spends 30+ min failing to read them

3 participants