Skip to content

fix(gateway): prefer Telegram native partial quote text in reply context - #22633

Closed
KhanCold wants to merge 1 commit into
NousResearch:mainfrom
KhanCold:fix/telegram-partial-quote-reply
Closed

fix(gateway): prefer Telegram native partial quote text in reply context#22633
KhanCold wants to merge 1 commit into
NousResearch:mainfrom
KhanCold:fix/telegram-partial-quote-reply

Conversation

@KhanCold

@KhanCold KhanCold commented May 9, 2026

Copy link
Copy Markdown

Bug Description

When a Telegram user replies using the native quote feature to select only part of a previous message, the adapter was ignoring the selected quote and injecting the entire replied-to message as reply_to_text. This can materially change the user's intent — the model sees the full previous message instead of just the quoted portion.

Root Cause

The reply-context extraction in _build_message_event() only checked message.reply_to_message.text / .caption, which represents the full replied-to message. Telegram's python-telegram-bot library exposes Message.quote / TextQuote for the selected quoted portion, but the adapter never checked it.

Fix

Prefer native quote text when available:

quote = getattr(message, "quote", None)
if quote and getattr(quote, "text", None):
    reply_to_text = quote.text
else:
    reply_to_text = message.reply_to_message.text or message.reply_to_message.caption or None
  • If message.quote exists and has text, use quote.text as reply_to_text
  • Preserve message.reply_to_message.message_id as reply_to_message_id
  • Fall back to the full replied-to message text only when no native quote object is present

Testing

This should be covered by a regression test that builds a Telegram Message with both reply_to_message.text and quote.text, then verifies the resulting MessageEvent.reply_to_text uses the quote text, not the full replied-to message.

Related

Fixes #22619
Related to reply-context behavior added in #1594

When a Telegram user replies using the native quote feature to select
only part of a previous message, the adapter was ignoring the selected
quote and injecting the entire replied-to message as reply_to_text.

Now checks message.quote (TextQuote) first and uses quote.text when
available, falling back to the full reply_to_message text only when
no native quote exists.

Fixes NousResearch#22619
@teknium1

teknium1 commented May 9, 2026

Copy link
Copy Markdown
Contributor

Closing in favor of #22676 (salvage of #22632). Same core fix; #22632 had regression tests and stayed scoped (your PR bundled an unrelated table-rendering change already merged via #22610). Your work is credited in the salvage PR body.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Telegram native partial quotes are expanded to the full replied-to message

2 participants