Skip to content

Pickle persistence broke due to telegram.inline #5102

@jack980517

Description

@jack980517

Issue I am facing

I coded a bot in 2021 using the then-latest version (inspecting version history on pypi revealed that it should be 13.x) of PTB. I stored some inline keyboard buttons in chat_data to preserve UI state. On my server I'm using Debian Bookworm which serves version 13.15 as an APT/DEB package, and that still works fine. However when I try to load the file via pickle.load on my local machine with PTB 22.5 which is currently latest, ModuleNotFoundError: No module named 'telegram.inline' occurs.

If I install 13.15 now via pip3 install -U python-telegram-bot==13.15 after I installed 22.5, another cryptic error happens when trying to load the file:

/data/data/com.termux/files/usr/lib/python3.12/site-packages/telegram/utils/request.py:54: UserWarning: python-telegram-bot wasn't properly installed. Please refer to README.rst on how to properly install.
  warnings.warn(
Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/telegram/utils/request.py", line 36, in <module>
    import telegram.vendor.ptb_urllib3.urllib3 as urllib3
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/telegram/vendor/ptb_urllib3/urllib3/__init__.py", line 7, in <module>
    from .connectionpool import (
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/telegram/vendor/ptb_urllib3/urllib3/connectionpool.py", line 11, in <module>
    from .exceptions import (
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/telegram/vendor/ptb_urllib3/urllib3/exceptions.py", line 2, in <module>
    from .packages.six.moves.http_client import (
ModuleNotFoundError: No module named 'telegram.vendor.ptb_urllib3.urllib3.packages.six.moves'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/telegram/__init__.py", line 187, in <module>
    from .bot import Bot
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/telegram/bot.py", line 107, in <module>
    from telegram.utils.request import Request
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/telegram/utils/request.py", line 44, in <module>
    import urllib3.contrib.appengine as appengine  # type: ignore[no-redef]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'urllib3.contrib.appengine'

So it looks like right now I can only load the pickle file on my server.

I don't understand async at all and it looks like it would require a major rewrite of my code, at least to insert async, await and yield, so I'm probably not gonna migrate to v20+.

I read the v20 transition guide which hinted (but did not explicitly state) that members that start with telegram._ are considered private APIs. I then noticed there is a telegram._inline which I guessed was probably formerly telegram.inline. I didn't dig further. Inline keyboards and telegram.inline aren't mentioned in the transition guide at all.

Of course I didn't use telegram.inline directly in my code; I used from telegram import InlineKeyboardMarkup as in the examples. However, what ended up being saved into the pickle file was telegram.inline.inlinekeyboardbutton.InlineKeyboardButton, which made it incompatible with version 22.5.

Since this is a breaking change that affects users that don't use the private APIs directly in their code, this should have been documented in the transition guide. I also think that the old names should have remained as "redirects" (with a deprecation warning if desired), just because they could be stored in pickle files.

And why are the objects' "real identity" a private API name anyway? If it's the other way around, i.e. if the "real identity" is telegram.InlineKeyboardButton, and telegram.inline.inlinekeyboardbutton.InlineKeyboardButton is an alias/redirect, the public API name would be saved into pickle files and there would be no breakage.

Also, I understand that I can still load and modify the pickle file on my server, or (probably) in a venv with only PTB 13.15 and nothing else installed, but can I even load the pickle file in my local environment that has been "contaminated" by having previously installed v22.5? If so, how?

Traceback to the issue

Related part of your code

Operating System

Server: Debian Bookworm; Local: Android 10 with Termux

Version of Python, python-telegram-bot & dependencies

Server:
/usr/lib/python3/dist-packages/telegram/utils/request.py:49: UserWarning: python-telegram-bot is using upstream urllib3. This is allowed but not supported by python-telegram-bot maintainers.
  warnings.warn(
python-telegram-bot 13.15
Bot API 6.3
certifi2022.09.24
Python 3.11.2 (main, Apr 28 2025, 14:11:48) [GCC 12.2.0]

Local:
Traceback (most recent call last):
  File "<frozen runpy>", line 189, in _run_module_as_main
  File "<frozen runpy>", line 148, in _get_module_details
  File "<frozen runpy>", line 112, in _get_module_details
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/telegram/__init__.py", line 335, in <module>
    from . import _version, constants, error, helpers, request, warnings
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/telegram/request/__init__.py", line 21, in <module>
    from ._httpxrequest import HTTPXRequest
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/telegram/request/_httpxrequest.py", line 24, in <module>
    import httpx
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/httpx/__init__.py", line 2, in <module>
    from ._api import *
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/httpx/_api.py", line 6, in <module>
    from ._client import Client
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/httpx/_client.py", line 13, in <module>
    from ._auth import Auth, BasicAuth, FunctionAuth
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/httpx/_auth.py", line 9, in <module>
    from urllib.request import parse_http_list
  File "/data/data/com.termux/files/usr/lib/python3.12/urllib/request.py", line 88, in <module>
    import http.client
  File "/data/data/com.termux/files/usr/lib/python3.12/http/client.py", line 71, in <module>
    import email.parser
  File "/data/data/com.termux/files/usr/lib/python3.12/email/parser.py", line 12, in <module>
    from email.feedparser import FeedParser, BytesFeedParser
  File "/data/data/com.termux/files/usr/lib/python3.12/email/feedparser.py", line 27, in <module>
    from email._policybase import compat32
  File "/data/data/com.termux/files/usr/lib/python3.12/email/_policybase.py", line 9, in <module>
    from email.utils import _has_surrogates
  File "/data/data/com.termux/files/usr/lib/python3.12/email/utils.py", line 28, in <module>
    import random
  File "/data/data/com.termux/files/usr/lib/python3.12/random.py", line 53, in <module>
    from warnings import warn as _warn
ImportError: cannot import name 'warn' from 'warnings' (/data/data/com.termux/files/usr/lib/python3.12/site-packages/telegram/warnings.py)

Seems like another weird problem here? Anyway, here's my output of `pip3 list installed` instead:
Package             Version
------------------- ------------
anyio               4.12.1
APScheduler         3.6.3
beautifulsoup4      4.13.4
Brotli              1.1.0
cachetools          4.2.2
certifi             2024.8.30
charset-normalizer  3.4.0
h11                 0.16.0
httpcore            1.0.9
httpx               0.28.1
idna                3.10
mutagen             1.47.0
pillow              12.1.0
pip                 25.3
pycryptodomex       3.21.0
python-telegram-bot 22.5
pytz                2025.2
requests            2.32.3
setuptools          80.9.0
six                 1.17.0
soupsieve           2.7
tornado             6.1
typing_extensions   4.14.1
tzlocal             5.3.1
unicodedataplus     16.0.0.post1
urllib3             2.2.3
websockets          13.1
yt-dlp              2025.11.12
yt-dlp-ejs          0.3.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions