fix: report MTProto schema drift instead of a generic error code - #188
Merged
Merged
Conversation
When the server sends an object whose constructor the installed Telethon
schema does not know, Telethon raises TypeNotFoundError. Today that is
swallowed by log_and_format_error and surfaces as
An error occurred (code: CHAT-ERR-418). Check mcp_errors.log for details.
which is indistinguishable from "no such user/chat".
That confusion is expensive, because schema drift does not fail cleanly:
an unknown constructor desynchronises the whole read buffer, so only some
calls break while their neighbours keep working. In our case list_chats,
get_common_chats, resolve_username and get_full_user were failing while
get_chats, search_contacts, list_messages and send_message were fine —
which reads exactly like "that user does not exist". The reported
constructor id is often garbage from the desynchronised stream and appears
in no schema at all, so searching for it leads nowhere too.
This names the condition, states that the data did arrive and only parsing
failed, and points at the fix. Every other error keeps the existing format.
Tested: both branches covered; the new test was run against a sabotaged
implementation first and fails there, so it can actually catch a regression.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
chigwell
approved these changes
Aug 11, 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.
Problem
When Telegram sends an object whose constructor the installed Telethon schema does not know, Telethon raises
TypeNotFoundError. Right nowlog_and_format_errorswallows it and the user sees:That is indistinguishable from "no such user/chat" — and it sends debugging in exactly the wrong direction.
Why this one deserves a named message
Schema drift does not fail cleanly. An unknown constructor desynchronises the whole read buffer, so the failure is partial and looks arbitrary: in our case
list_chats,get_common_chats,resolve_usernameandget_full_userwere failing whileget_chats,search_contacts,list_messagesandsend_messagekept working normally.Two things made it worse:
mcp_errors.logcontained the person's name and username in plain text, meaning the data had arrived and only parsing had failed.api.tl), because it is garbage read from the desynchronised stream. So searching for it leads nowhere either.Root cause in our case: the installed Telethon was built for LAYER 227 while the server had moved to 228, where the
userconstructor changed (0x31774388→0xb1b8cc83).Change
TypeNotFoundErrornow returns a message that names the condition, states that the data arrived and only parsing failed, and points at the fix. Every other error keeps the existing generic format — the helper's behaviour is unchanged for them.The telethon import is done lazily inside a helper and guarded, so the module does not gain a hard import-time dependency.
Tests
tests/test_schema_drift.pycovers both branches.The new test was first run against a sabotaged implementation (the branch disabled) and fails there, so it can actually catch a regression rather than passing vacuously. Full suite green.
Separately we needed to fix the drift, not just report it, and did so by regenerating Telethon's TL classes against the current layer. That part is more invasive (it vendors a schema and the generator), so I'm not proposing it here — happy to open it as a second PR if you'd find it useful.