Skip to content

fix(telegram): fall back to send_document when send_photo rejects a local image#15837

Closed
QifengKuang wants to merge 1 commit into
NousResearch:mainfrom
QifengKuang:fix/telegram-document-fallback-for-rejected-photo
Closed

fix(telegram): fall back to send_document when send_photo rejects a local image#15837
QifengKuang wants to merge 1 commit into
NousResearch:mainfrom
QifengKuang:fix/telegram-document-fallback-for-rejected-photo

Conversation

@QifengKuang

Copy link
Copy Markdown
Contributor

Summary

When TelegramAdapter.send_image_file calls send_photo and Telegram rejects the image (very tall screenshots, extreme aspect ratios, files outside photo dimension limits, etc.), the current handler degrades straight to the base adapter — which sends a text-only `Image: /path/to/file.png` message. The user sees a path string instead of an openable attachment.

This change inserts a send_document fallback in between: same file, uploaded as a Telegram document. The user still receives a downloadable, openable attachment with the original caption. Only if document upload also fails do we fall back to the base adapter's text path.

Why

Hit in production with a long browser screenshot bot reply. The current behavior leaks an internal local filesystem path into the chat (`Image: /Users//.hermes/profiles/.../cache/screenshots/browser_screenshot_…png`), which is both useless to the recipient and a small privacy leak.

Behavior matrix

Telegram response to send_photo Before After
Accepted photo sent ✅ photo sent ✅
Rejected (dimensions / ratio / size) text `Image: /local/path.png` ❌ document attachment ✅
Rejected by photo and document text `Image: /local/path.png` text `Image: /local/path.png` (unchanged)

Implementation notes

  • Reuses the existing _metadata_thread_id / _message_thread_id_for_send helpers so forum-thread routing is preserved.
  • Caption is still trimmed to the document caption limit (1024 chars) just like the photo path.
  • The first failure is now logged at warning (recoverable) instead of error; the document failure stays error.

Test plan

  • python -m py_compile gateway/platforms/telegram.py
  • Manual: in a deployment that previously surfaced `Image: /…/browser_screenshot_….png`, the same payload now arrives as a document.
  • (Reviewer) Confirm `send_document`'s caption length limit (1024) is the same the rest of the codebase assumes.

…al image

Telegram rejects some valid local images as photos (very tall screenshots,
extreme aspect ratios, files over photo dimension limits, etc.). The
previous handler logged the rejection and degraded to the base adapter,
which sends a text-only "Image: /path/to/file.png" message — the user
sees a path string instead of an openable attachment.

Insert a document-upload fallback between the failed send_photo and the
text-only path: same file, uploaded as a Telegram document so the user
still receives a downloadable attachment with the original caption.
Only fall back to the base adapter if document upload also fails.
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists platform/telegram Telegram bot adapter comp/gateway Gateway runner, session dispatch, delivery labels Apr 26, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Related to #15728 (same send_photo → send_document fallback approach) and #6249 (root issue: local file path leaked as text). Review together.

@teknium1

teknium1 commented May 4, 2026

Copy link
Copy Markdown
Contributor

Salvaged via #19714 onto current main. Your broader try/except-style fallback was the right idea — earlier work (#19630) only handled the specific Photo_invalid_dimensions case, but your observation was correct that other failures benefit from the same fallback. Adapted to keep INFO-level logging for the expected dimension case and WARNING for other failures, so log noise stays quiet when the fallback is the known-correct path. Your commit authorship was preserved. Thanks @QifengKuang!

teknium1 pushed a commit that referenced this pull request May 4, 2026
…ust dim errors

Broadens the existing fallback (previously only fired for
Photo_invalid_dimensions) to cover every send_photo exception class:
rate limits, corrupt file markers, format edge cases. The expected
dimension case still logs at INFO (document is the right path); all
other cases log at WARNING with exc_info so they're visible in logs.

If send_document itself fails, we still fall back to the base adapter's
text-only 'Image: /path' rendering as a last resort.

Salvage of #15837 — original PR author QifengKuang proposed the broader
try/except-style fallback. Adapted to keep the existing INFO-vs-WARNING
log split for dimension errors (the expected case).

Co-authored-by: QifengKuang <k2767567815@gmail.com>
@teknium1 teknium1 closed this May 4, 2026
cluricaun28 referenced this pull request in cluricaun28/Logos May 5, 2026
…ust dim errors

Broadens the existing fallback (previously only fired for
Photo_invalid_dimensions) to cover every send_photo exception class:
rate limits, corrupt file markers, format edge cases. The expected
dimension case still logs at INFO (document is the right path); all
other cases log at WARNING with exc_info so they're visible in logs.

If send_document itself fails, we still fall back to the base adapter's
text-only 'Image: /path' rendering as a last resort.

Salvage of #15837 — original PR author QifengKuang proposed the broader
try/except-style fallback. Adapted to keep the existing INFO-vs-WARNING
log split for dimension errors (the expected case).

Co-authored-by: QifengKuang <k2767567815@gmail.com>
nickdlkk pushed a commit to nickdlkk/hermes-agent that referenced this pull request May 11, 2026
…ust dim errors

Broadens the existing fallback (previously only fired for
Photo_invalid_dimensions) to cover every send_photo exception class:
rate limits, corrupt file markers, format edge cases. The expected
dimension case still logs at INFO (document is the right path); all
other cases log at WARNING with exc_info so they're visible in logs.

If send_document itself fails, we still fall back to the base adapter's
text-only 'Image: /path' rendering as a last resort.

Salvage of NousResearch#15837 — original PR author QifengKuang proposed the broader
try/except-style fallback. Adapted to keep the existing INFO-vs-WARNING
log split for dimension errors (the expected case).

Co-authored-by: QifengKuang <k2767567815@gmail.com>
rmulligan pushed a commit to rmulligan/hermes-agent that referenced this pull request May 11, 2026
…ust dim errors

Broadens the existing fallback (previously only fired for
Photo_invalid_dimensions) to cover every send_photo exception class:
rate limits, corrupt file markers, format edge cases. The expected
dimension case still logs at INFO (document is the right path); all
other cases log at WARNING with exc_info so they're visible in logs.

If send_document itself fails, we still fall back to the base adapter's
text-only 'Image: /path' rendering as a last resort.

Salvage of NousResearch#15837 — original PR author QifengKuang proposed the broader
try/except-style fallback. Adapted to keep the existing INFO-vs-WARNING
log split for dimension errors (the expected case).

Co-authored-by: QifengKuang <k2767567815@gmail.com>
rousegordon-ops pushed a commit to rousegordon-ops/hermes-agent that referenced this pull request May 12, 2026
…ust dim errors

Broadens the existing fallback (previously only fired for
Photo_invalid_dimensions) to cover every send_photo exception class:
rate limits, corrupt file markers, format edge cases. The expected
dimension case still logs at INFO (document is the right path); all
other cases log at WARNING with exc_info so they're visible in logs.

If send_document itself fails, we still fall back to the base adapter's
text-only 'Image: /path' rendering as a last resort.

Salvage of NousResearch#15837 — original PR author QifengKuang proposed the broader
try/except-style fallback. Adapted to keep the existing INFO-vs-WARNING
log split for dimension errors (the expected case).

Co-authored-by: QifengKuang <k2767567815@gmail.com>
(cherry picked from commit 69fc6d9)
jsboige pushed a commit to jsboige/hermes-agent that referenced this pull request May 14, 2026
…ust dim errors

Broadens the existing fallback (previously only fired for
Photo_invalid_dimensions) to cover every send_photo exception class:
rate limits, corrupt file markers, format edge cases. The expected
dimension case still logs at INFO (document is the right path); all
other cases log at WARNING with exc_info so they're visible in logs.

If send_document itself fails, we still fall back to the base adapter's
text-only 'Image: /path' rendering as a last resort.

Salvage of NousResearch#15837 — original PR author QifengKuang proposed the broader
try/except-style fallback. Adapted to keep the existing INFO-vs-WARNING
log split for dimension errors (the expected case).

Co-authored-by: QifengKuang <k2767567815@gmail.com>
dannyJ848 pushed a commit to dannyJ848/hermes-agent that referenced this pull request May 17, 2026
…ust dim errors

Broadens the existing fallback (previously only fired for
Photo_invalid_dimensions) to cover every send_photo exception class:
rate limits, corrupt file markers, format edge cases. The expected
dimension case still logs at INFO (document is the right path); all
other cases log at WARNING with exc_info so they're visible in logs.

If send_document itself fails, we still fall back to the base adapter's
text-only 'Image: /path' rendering as a last resort.

Salvage of NousResearch#15837 — original PR author QifengKuang proposed the broader
try/except-style fallback. Adapted to keep the existing INFO-vs-WARNING
log split for dimension errors (the expected case).

Co-authored-by: QifengKuang <k2767567815@gmail.com>
gweeteve pushed a commit to gweeteve/hermes-agent that referenced this pull request Jun 2, 2026
…ust dim errors

Broadens the existing fallback (previously only fired for
Photo_invalid_dimensions) to cover every send_photo exception class:
rate limits, corrupt file markers, format edge cases. The expected
dimension case still logs at INFO (document is the right path); all
other cases log at WARNING with exc_info so they're visible in logs.

If send_document itself fails, we still fall back to the base adapter's
text-only 'Image: /path' rendering as a last resort.

Salvage of NousResearch#15837 — original PR author QifengKuang proposed the broader
try/except-style fallback. Adapted to keep the existing INFO-vs-WARNING
log split for dimension errors (the expected case).

Co-authored-by: QifengKuang <k2767567815@gmail.com>
Egavasyug pushed a commit to Egavasyug/hermes-agent that referenced this pull request Jun 10, 2026
…ust dim errors

Broadens the existing fallback (previously only fired for
Photo_invalid_dimensions) to cover every send_photo exception class:
rate limits, corrupt file markers, format edge cases. The expected
dimension case still logs at INFO (document is the right path); all
other cases log at WARNING with exc_info so they're visible in logs.

If send_document itself fails, we still fall back to the base adapter's
text-only 'Image: /path' rendering as a last resort.

Salvage of NousResearch#15837 — original PR author QifengKuang proposed the broader
try/except-style fallback. Adapted to keep the existing INFO-vs-WARNING
log split for dimension errors (the expected case).

Co-authored-by: QifengKuang <k2767567815@gmail.com>
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 type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants