Skip to content

Telegram: sendRichMessageDraft works but final message falls back to MarkdownV2 (expect_edits kills rich path) #46515

Closed

Description

@valentinpezon-primo

Bug Description

When Telegram rich messages are enabled and draft-based streaming is active, the sendRichMessageDraft preview renders correctly during streaming (user sees rich tables, formatting, etc.), but the final persistent message is sent as plain MarkdownV2 instead of via sendRichMessage.

Root Cause

In gateway/stream_consumer.py line 1535, when the final message is created after draft streaming completes (first real send, _message_id is None, finalize=True):

result = await self.adapter.send(
    chat_id=self.chat_id,
    content=text,
    reply_to=self._initial_reply_to_id,
    metadata={**(self.metadata or {}), "expect_edits": True},  # ← This
)

The expect_edits: True flag passes through to _should_attempt_rich() in gateway/platforms/telegram.py line 988:

and not (metadata or {}).get("expect_edits")

This check DISABLES sendRichMessage for the final delivery, forcing a fallback to sendMessage with MarkdownV2. The user sees a beautiful rich table during the streaming preview, then it gets replaced by plain text.

Expected Behavior

When finalize=True and draft-based streaming was used (not edit-based), the expect_edits flag should be False since no further edits will be made. This would allow sendRichMessage to persist the same rich formatting shown in the draft preview.

Steps to Reproduce

  1. Enable telegram.extra.rich_messages: true in config
  2. Send a message to the bot that triggers a response with tables/lists
  3. Watch the streaming draft — it renders as rich markdown ✅
  4. When streaming completes, the final message is plain MarkdownV2 ❌

Evidence

Gateway log shows streamed=True previewed=False content_delivered=True — the normal final send is suppressed because the stream consumer already delivered content. But that delivery uses the expect_edits path which skips sendRichMessage. No errors appear in the logs because the sendRichMessage attempt is logged at debug level and expect_edits prevents the attempt entirely.

Environment

  • Hermes version: 0.16.0 (config v29)
  • python-telegram-bot: latest (Bot API 10.1 endpoints available)
  • macOS, Telegram Desktop

Activity

  1. added
    type/bugSomething isn't working
    P2Medium — degraded but workaround exists
    comp/gatewayGateway runner, session dispatch, delivery
    on Jun 15, 2026
  2. learhy commented on Jun 22, 2026

    @learhy

    Independent reproduction + additional evidence (session 2026-06-22)

    Hit this exact symptom today on main (commit 36ae95847). Confirming the root-cause analysis is correct and adding log evidence from my session.

    What the user sees

    I sent a ~2800-char reply containing a markdown pipe table and a few code fences. On Telegram the user only saw roughly the first 2-3 lines of the response — the rest vanished. The gateway log shows the response was 3463 chars and marked content_delivered=True. The user reported three separate times that the message was truncated; on resend it worked (when I kept the reply under ~2200 chars).

    Gateway log evidence

    2026-06-22 19:26:42,110 gateway.run: Suppressing normal final send for session agent:main:telegram:dm:7276844555:
        final delivery already confirmed (streamed=True previewed=False content_delivered=True).
    2026-06-22 19:26:42,110 gateway.run: response ready: platform=telegram chat=7276844555 time=90.3s
        api_calls=5 response=3463 chars
    

    Matches the signature in the original issue exactly. Preview shows table correctly; final persists as MarkdownV2.

    Additional finding: MarkdownV2 parse failure during streaming is a secondary trigger

    A few days earlier I hit a related failure mode that's probably the same root cause with a different symptom. Log excerpt from 2026-06-21 06:55:17:

    gateway.platforms.telegram: [Telegram] MarkdownV2 parse failed, falling back to plain text:
        Can't parse entities: character '.' is reserved and must be escaped with the preceding '\'
    gateway.platforms.telegram: [Telegram] Telegram flood control on send (attempt 1/3),
        retrying in 226.0s: Flood control exceeded. Retry in 226 seconds
    

    Sequence in the streaming consumer appears to be:

    1. Draft streaming renders correctly (rich path)
    2. Finalize attempts editMessageText with MarkdownV2 (per prefers_fresh_final_streaming = False at gateway/platforms/telegram.py:1011-1023, see also [Telegram] Markdown pipe tables converted to bullet lists in streaming finalization #49452)
    3. MarkdownV2 parse fails on unescaped characters → fallback to plain-text edit
    4. Flood control kicks in on the plain-text retry
    5. content_delivered=True is set somewhere in the fallback chain
    6. The "real" final sendMessage is suppressed by gateway/run.py

    The fix proposed in #49452 (Option A — wire a send_rich_finalize so the final send uses sendMessage with rich param rather than editMessageText with MarkdownV2) would resolve this whole class.

    Workaround for end users until merged

    Keep individual Telegram replies under ~2200 chars and avoid markdown pipe tables. MarkdownV2 falls back to plain text for tables anyway, so tables buy you nothing in Telegram rendering today — use code fences for tabular data.

    Repro recipe

    1. telegram.extra.rich_messages: true (default)
    2. Trigger a streaming reply that includes a markdown pipe table AND at least one inline period inside a code span or unescaped . in a bold/italic span
    3. Watch the streaming draft — table renders ✅
    4. Watch the finalize — table converts to bullet list, response truncates ❌
    5. gateway.log shows content_delivered=True + a MarkdownV2 parse-failed warning within ~1s of finalize

    Affected version: 36ae95847 feat(gateway): gate message timestamps behind opt-in (default off) on main.
    Platform: Telegram Desktop (latest), python-telegram-bot v22.x, Bot API 10.1.

  3. teknium1 commented on Nov 8, 2026

    @teknium1
    Collaborator

    Thanks for the precise root cause! Fixed on main by 0f22727 ("omit expect_edits on finalized draft sends"): the finalize send now passes expect_edits=not finalize (gateway/stream_consumer_transport.py), so _should_attempt_rich allows sendRichMessage for the final. Please reopen if a current build still falls back to MarkdownV2.

    Triaged in the Telegram backlog sweep against origin/main @ 787ee80. If this still reproduces on a current build, reply with the version and steps and we'll reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium — degraded but workaround existscomp/gatewayGateway runner, session dispatch, deliveryplatform/telegramTelegram bot adaptertype/bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions