Skip to content

fix: send_model_picker fails in Telegram General topic (Message thread not found)#21542

Closed
echo-mydevbot wants to merge 1 commit into
NousResearch:mainfrom
echo-mydevbot:fix/telegram-model-picker-general-topic
Closed

fix: send_model_picker fails in Telegram General topic (Message thread not found)#21542
echo-mydevbot wants to merge 1 commit into
NousResearch:mainfrom
echo-mydevbot:fix/telegram-model-picker-general-topic

Conversation

@echo-mydevbot

Copy link
Copy Markdown

Bug

/model command in Telegram supergroup General topic returns a plain text fallback instead of the interactive inline keyboard picker. Gateway logs show:

WARNING gateway.platforms.telegram: [Telegram] send_model_picker failed: Message thread not found

Root Cause

send_model_picker() in gateway/platforms/telegram.py was bypassing _message_thread_id_for_send(), the class method that correctly returns None for the General topic thread_id "1". Instead, it did:

message_thread_id=int(thread_id) if thread_id else None

Telegram's Bot API rejects message_thread_id=1 because the General topic doesn't use that parameter — it should be omitted entirely.

Every other inline keyboard method (send_exec_approval, send_slash_confirm, regular send()) correctly uses the two-step pattern: _metadata_thread_id(metadata)_message_thread_id_for_send(thread_id). send_model_picker was the only method that didn't.

Fix

Use the same helper methods as all other methods:

thread_id = self._metadata_thread_id(metadata)
message_thread_id = self._message_thread_id_for_send(thread_id)
kwargs: Dict[str, Any] = {
    "chat_id": int(chat_id),
    "text": text,
    "parse_mode": ParseMode.MARKDOWN,
    "reply_markup": keyboard,
    **self._link_preview_kwargs(),
}
if message_thread_id is not None:
    kwargs["message_thread_id"] = message_thread_id
msg = await self._bot.send_message(**kwargs)

Tests

Added tests/gateway/test_telegram_model_picker_thread.py with 3 tests:

  • General topic (thread_id="1") → message_thread_id omitted (the bug case)
  • Real topic (thread_id="42") → message_thread_id=42 passed
  • No metadatamessage_thread_id not included

All 3 new tests + all 11 existing test_telegram_thread_fallback tests pass.

Verification

Tested in production on a Telegram supergroup where /model previously returned text fallback. After the fix, the interactive model picker renders correctly in the General topic.

send_model_picker() in TelegramAdapter was bypassing the
_message_thread_id_for_send() helper that correctly returns None for
the General topic thread_id '1'. Instead, it converted thread_id
directly with int(thread_id) if thread_id else None, passing
message_thread_id=1 to the Telegram Bot API.

The Telegram API rejects message_thread_id=1 with 'Message thread not
found' because the General topic in supergroups doesn't use the
message_thread_id parameter. This caused /model in General chats to
fail, falling back to a plain text response instead of the interactive
inline keyboard picker.

Every other inline keyboard method (send_exec_approval, send_slash_confirm)
and all regular message sends correctly use _metadata_thread_id() +
_message_thread_id_for_send(). send_model_picker was the only outlier.

Fix: Use the same two-step pattern:
1. Extract thread_id via _metadata_thread_id(metadata)
2. Filter via _message_thread_id_for_send(thread_id) — returns None for '1'
3. Only include message_thread_id in kwargs when it's not None

Added regression tests for:
- General topic (thread_id '1') → message_thread_id omitted
- Real topic (thread_id '42') → message_thread_id=42 passed
- No metadata → message_thread_id omitted
@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 duplicate This issue or pull request already exists labels May 7, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of PR #12873 (and also #19961, #18612) — all fix the same bug where send_model_picker() bypasses _message_thread_id_for_send(), causing "Message thread not found" in Telegram General topic. The original fix PR is #12873 which targets issue #12839.

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Thanks for catching this — the General-topic mapping (message_thread_id="1"None) you proposed for send_model_picker is now on main. It landed via #22410 (salvage of @leehack's #22053 telegram DM topic fix), which routed send_model_picker through _thread_kwargs_for_send — a strict superset of the _message_thread_id_for_send mapping you proposed. That helper now handles both forum General topics (your case) AND Hermes-created DM topic lanes that #22053 was originally about.

The parallel code path in the send_message tool's standalone _send_telegram helper had the same bug — fixed in #22423 just now (Fixes #22267).

Closing as superseded — thanks for the contribution and for keeping us focused on this bug class!

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 duplicate This issue or pull request already exists 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