hermes - ✅(Solved) Fix [Feature]: Telegram large video files trigger infinite fallback loop [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
NousResearch/hermes-agent#17302Fetched 2026-04-30 06:48:34
View on GitHub
Comments
0
Participants
1
Timeline
5
Reactions
0
Author
Participants
Timeline (top)
labeled ×4cross-referenced ×1

When a user sends a video file larger than 20MB to a Telegram group, the Bot cannot download it via Bot API's getFile (returns BadRequest: File is too big). However, the code only prints a warning and then continues to call handle_message() to send to the agent.

The agent receives an empty message and tries various models, continuously triggering the fallback mechanism, causing a chaotic loop:

⚠️ Non-retryable error (HTTP 400) — trying fallback...
🔄 Primary model failed — switching to fallback: minimax-m2.5-free via opencode-zen
⚠️ Non-retryable error (HTTP 400) — trying fallback...
🔄 Primary model failed — switching to fallback: minimax-m2.5-free via opencode-zen
... (loops 15+ times)

Error Message

⚠️ Non-retryable error (HTTP 400) — trying fallback... ⚠️ Non-retryable error (HTTP 400) — trying fallback...

  • The exception is caught by line 2807's except Exception, only printing a warning
  • No return statement, so after the exception, code continues to execute and calls handle_message(event) with the partially-built event
  1. Return a friendly error message to the user
  2. Add handling for "File is too big" in the except Exception block and return early except Exception as e:

Root Cause

In gateway/platforms/telegram.py lines 2746-2807:

  • Line 2747: await doc.get_file() throws BadRequest: File is too big
  • The exception is caught by line 2807's except Exception, only printing a warning
  • No return statement, so after the exception, code continues to execute and calls handle_message(event) with the partially-built event

Fix Action

Fixed

PR fix notes

PR #17325: fix(telegram): stop large videos from triggering infinite model fallback (#17302)

Description (problem / solution / changelog)

Closes #17302.

Summary

When a Telegram video > 20 MB hits the bot, getFile() raises BadRequest("File is too big"). The current handler catches the exception, logs a warning, and falls through to handle_message(event) with an effectively empty event — the agent then burns through every fallback model (15+ retries in the reporter's logs) trying to respond to nothing.

This PR fixes it with three layered defenses, mirroring the size-check pattern that the non-video document branch already uses:

1. Pre-check file_size before downloading

Both the native msg.video branch and the video-as-document branch now verify file_size <= 20 MB before calling get_file(). Oversize / unverifiable videos short-circuit with a user-visible message ("Telegram's Bot API limits file downloads to 20 MB…") and message_type=VIDEO. The agent gets a meaningful event instead of a blank one.

2. Trap "File is too big" inside the except block

For forwarded-video / edited-message edge cases where file_size lies, the BadRequest is now caught at runtime and event.text is set to an explanatory message instead of being left blank. This is the surgical fix that prevents the fallback storm even when the pre-check is bypassed.

3. Optional opt-out: telegram.extra.ignore_videos: true

When set, video messages (native and video/* MIME documents) are dropped at the top of _handle_media_message, before any work is done. Other media (PDFs, photos, voice, audio) is unaffected.

Why not the issue's exact diff

The issue's proposed diff calls self._send_safe_message(...) which doesn't exist in this codebase (only self._bot.send_message(...) does). I kept the spirit of the suggestion — the friendly text message — but routed it through the existing event.text + handle_message() path that the document handler already uses for "Unsupported document type" and "too large or unverifiable", so the fix is consistent with the surrounding code rather than introducing a new send pattern.

Tests

Added 8 new tests in tests/gateway/test_telegram_documents.py::TestVideoDownloadBlock:

  • test_oversize_native_video_short_circuits_with_friendly_text
  • test_unverifiable_native_video_size_short_circuits (parity with the existing document file_size=None security fix)
  • test_oversize_video_document_short_circuits
  • test_native_video_get_file_too_big_does_not_send_blank_event
  • test_video_document_get_file_too_big_does_not_send_blank_event
  • test_ignore_videos_config_skips_native_video
  • test_ignore_videos_config_skips_video_documents
  • test_ignore_videos_does_not_block_pdfs

The existing _make_video() helper now defaults file_size=1024 so the prior happy-path test still passes through the new gate.

$ python -m pytest tests/gateway/test_telegram_documents.py -q
............................................                          [100%]
44 passed in 3.50s

python3 -c "import ast; ast.parse(open('gateway/platforms/telegram.py').read())" clean.

Out of scope

The reporter's _send_safe_message reply-via-Telegram pattern is more invasive than needed; the agent-event path is sufficient and consistent with how the document handler already communicates blocked uploads. Happy to add a direct reply if maintainers prefer it.

Changed files

  • gateway/platforms/telegram.py (modified, +82/-4)
  • tests/gateway/test_telegram_documents.py (modified, +120/-1)

Code Example

⚠️ Non-retryable error (HTTP 400) — trying fallback...
🔄 Primary model failed — switching to fallback: minimax-m2.5-free via opencode-zen
⚠️ Non-retryable error (HTTP 400) — trying fallback...
🔄 Primary model failed — switching to fallback: minimax-m2.5-free via opencode-zen
... (loops 15+ times)

---
RAW_BUFFERClick to expand / collapse

Problem or Use Case

Description

When a user sends a video file larger than 20MB to a Telegram group, the Bot cannot download it via Bot API's getFile (returns BadRequest: File is too big). However, the code only prints a warning and then continues to call handle_message() to send to the agent.

The agent receives an empty message and tries various models, continuously triggering the fallback mechanism, causing a chaotic loop:

⚠️ Non-retryable error (HTTP 400) — trying fallback...
🔄 Primary model failed — switching to fallback: minimax-m2.5-free via opencode-zen
⚠️ Non-retryable error (HTTP 400) — trying fallback...
🔄 Primary model failed — switching to fallback: minimax-m2.5-free via opencode-zen
... (loops 15+ times)

Root Cause

In gateway/platforms/telegram.py lines 2746-2807:

  • Line 2747: await doc.get_file() throws BadRequest: File is too big
  • The exception is caught by line 2807's except Exception, only printing a warning
  • No return statement, so after the exception, code continues to execute and calls handle_message(event) with the partially-built event

Expected Behavior

When an unprocessable media file is encountered (e.g., file too large), the code should:

  1. Skip processing and NOT send to the agent, OR
  2. Return a friendly error message to the user

Suggested Fix

  1. Add handling for "File is too big" in the except Exception block and return early
  2. Add a config option ignore_videos: true for users who don't want to process videos at all

Environment

  • Hermes Agent version: latest
  • Telegram Bot API
  • Platform: macOS / Linux

Proposed Solution

diff --git a/gateway/platforms/telegram.py b/gateway/platforms/telegram.py index ce536f51..c627d5aa 100644 --- a/gateway/platforms/telegram.py +++ b/gateway/platforms/telegram.py @@ -2621,7 +2621,14 @@ class TelegramAdapter(BasePlatformAdapter): return if not self._should_process_message(update.message): return

  •    # Check if videos should be ignored (config: telegram.extra.ignore_videos = true)
  •    if self.config.extra.get("ignore_videos", False):
  •        msg = update.message
  •        if msg.video or (msg.document and "video" in (msg.document.mime_type or "")):
  •            logger.info("[Telegram] Ignoring video (ignore_videos=true)")
  •            return
  •    msg = update.message
    
       # Determine media type

@@ -2805,6 +2812,13 @@ class TelegramAdapter(BasePlatformAdapter): )

         except Exception as e:
  •            # Check for common Telegram API errors
  •            error_msg = str(e)
  •            if "File is too big" in error_msg:
  •                reply = "⚠️ Telegram Bot API limit file size up to 20MB"
  •                await self._send_safe_message(update.message.chat.id, reply, reply_to_message_id=update.message.message_id)
  •                logger.info("[Telegram] File too large, sent guidance to user")
  •                return
               logger.warning("[Telegram] Failed to cache document: %s", e, exc_info=True)
    
       media_group_id = getattr(msg, "media_group_id", None)

Alternatives Considered

No response

Feature Type

Gateway / messaging improvement

Scope

Small (single file, < 50 lines)

Contribution

  • I'd like to implement this myself and submit a PR

Debug Report (optional)

extent analysis

TL;DR

The issue can be fixed by adding a check for "File is too big" in the except Exception block and returning early with a friendly error message to the user.

Guidance

  • Check the except Exception block in gateway/platforms/telegram.py and add a condition to catch "File is too big" errors.
  • Return early with a friendly error message to the user when a file is too large, instead of continuing to execute and calling handle_message(event).
  • Consider adding a config option ignore_videos to allow users to ignore videos altogether.
  • Verify that the fix works by testing with a video file larger than 20MB and checking that the user receives an error message instead of an empty message.

Example

except Exception as e:
    error_msg = str(e)
    if "File is too big" in error_msg:
        reply = "⚠️ Telegram Bot API limit file size up to 20MB"
        await self._send_safe_message(update.message.chat.id, reply, reply_to_message_id=update.message.message_id)
        logger.info("[Telegram] File too large, sent guidance to user")
        return

Notes

The proposed solution provides a diff for the telegram.py file, which can be used as a starting point for the fix. However, it's essential to test the solution thoroughly to ensure it works as expected.

Recommendation

Apply the workaround by adding a check for "File is too big" in the except Exception block and returning early with a friendly error message to the user. This will prevent the chaotic loop and provide a better user experience.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

hermes - ✅(Solved) Fix [Feature]: Telegram large video files trigger infinite fallback loop [1 pull requests, 1 participants]