fix(gateway): wire HERMES_TELEGRAM_HTTP_MEDIA_WRITE_TIMEOUT to PTB so large media uploads stop hitting the 20s default#21777
Open
tangivis wants to merge 3 commits into
Conversation
Both Telegram send paths construct python-telegram-bot's HTTPXRequest without setting media_write_timeout, so multipart uploads (send_video, send_document, large send_photo) fall back to PTB's 20s default. That default is too short for any meaningful media on real-world uplinks, and the existing HERMES_TELEGRAM_HTTP_* env vars couldn't override it because the binding was missing in code. Fixes the wiring in both paths and defaults to 300s — matching the adapter's other generous request timeouts and giving users a single env var (HERMES_TELEGRAM_HTTP_MEDIA_WRITE_TIMEOUT) to bump higher when needed: - gateway/platforms/telegram.py:936-942 — add the missing key to request_kwargs so both the polling client and the dedicated get_updates client share the same media-write budget. - tools/send_message_tool.py:_send_telegram — replace the bare Bot(token=token) with an HTTPXRequest-configured client that mirrors the adapter's env vars. Without this, cron-driven sends and direct send_message tool calls hit the same 20s ceiling. Tests cover the default value, env override, and graceful fallback on malformed env values, for both code paths. Fixes NousResearch#21757
Match the surrounding request_kwargs style (other entries are bare). The block-level rationale already covers PTB defaults at line 920-922. Per review feedback.
This was referenced Jun 12, 2026
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 does this PR do?
Telegram media uploads (
send_video,send_document, largesend_photo) hitWriteTimeoutafter exactly 20 seconds because both Telegram send paths construct PTB'sHTTPXRequestwithout passingmedia_write_timeout. The existing env var infrastructure suggests this is meant to be tunable — the gateway adapter already plumbsconnect_timeout,read_timeout,write_timeout,pool_timeoutthroughHERMES_TELEGRAM_HTTP_*— butmedia_write_timeoutwas simply never wired up.Result: setting
HERMES_TELEGRAM_HTTP_MEDIA_WRITE_TIMEOUT=600in.envdoes nothing. BumpingHERMES_TELEGRAM_HTTP_WRITE_TIMEOUTdoesn't help either, because PTB applies a separatemedia_write_timeoutto multipart uploads —write_timeoutonly governs JSON message sends.This PR plugs the gap in both Telegram send paths and defaults to 300s (matching the request-level generosity already present elsewhere). Users who need more for very large files on slow uplinks can override via env.
Related Issue
Fixes #21757
Type of Change
Changes Made
gateway/platforms/telegram.py:936-948— addmedia_write_timeouttorequest_kwargsso both the polling client and the dedicatedget_updatesclient receive it. Default 300s, override viaHERMES_TELEGRAM_HTTP_MEDIA_WRITE_TIMEOUT.tools/send_message_tool.py:_send_telegram— replace the bareBot(token=token)with anHTTPXRequest-configured client that mirrors the gateway adapter's env vars. Cron-delivered media and directsend_messagetool calls also need the longer timeout, otherwise they hit the same 20s ceiling.tests/gateway/test_telegram_timeouts.py(new) — three tests covering default, env override, and graceful fallback for malformed env values.tests/tools/test_send_message_tool.py— updated_install_telegram_mockhelper (now also mockstelegram.requestand accepts the newrequest=kwarg onBot); addedTestSendTelegramTimeoutWiringmirroring the gateway tests.website/docs/reference/environment-variables.md:419— document the newHERMES_TELEGRAM_HTTP_MEDIA_WRITE_TIMEOUTknob alongside the existing Telegram HTTP timeouts.How to Test
Repro on
main(faa13e49f):journalctl --user -u hermes-gateway -fand observehttpx.WriteTimeoutafter ~20s.HERMES_TELEGRAM_HTTP_MEDIA_WRITE_TIMEOUT=600to~/.hermes/.env, restart the gateway → same failure, because the env var was never read.After this PR:
HERMES_TELEGRAM_HTTP_MEDIA_WRITE_TIMEOUT=600set,HTTPXRequestis constructed withmedia_write_timeout=600.0(verified by the new tests).Test runs
Pinned via
scripts/run_tests.shfor parity, but I ran in single-process mode here becausexdistwas thrashing on this machine (unrelated load).Why a Telegram-specific env var (rather than a generic gateway one)
The repo already has a generic gateway HTTP knob —
HERMES_GATEWAY_HTTPX_*ingateway/platforms/_http_client_limits.py— but it controlshttpx.Limits(keepalive, max-connections), not request-level timeouts.media_write_timeoutis unique to PTB'sHTTPXRequestwrapper; raw httpx and the other platforms' SDKs (discord.py, slack_bolt, baileys) don't have an equivalent concept. Keeping this Telegram-specific avoids a leaky abstraction.Checklist
Code
fix(gateway):)pytestand the targeted tests pass; full Telegram suite (148 tests) is greenDocumentation & Housekeeping
website/docs/reference/environment-variables.mdcli-config.yaml.exampleif I added/changed config keys — N/A (env var only, no YAML key)CONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — N/A