python-telegram-bot/python-telegram-bot · warning · RuntimeError
Cannot set name for filters.{self.__class__.__name__}
Error message
Cannot set name for filters.{self.__class__.__name__} What it means
Filters like `filters.Chat` and `filters.User` generate their `name` from their current chat_ids/usernames. Since the name is derived state, assigning `.name` on these filter instances raises RuntimeError.
Source
Thrown at src/telegram/ext/filters.py:815
chat_or_user = self._get_chat_or_user(message)
if chat_or_user:
if self.chat_ids:
return chat_or_user.id in self.chat_ids
if self.usernames:
return bool(chat_or_user.username and chat_or_user.username in self.usernames)
return self.allow_empty
return False
@property
def name(self) -> str:
return (
f"filters.{self.__class__.__name__}("
f"{', '.join(str(s) for s in (self.usernames or self.chat_ids))})"
)
@name.setter
def name(self, _: str) -> NoReturn:
raise RuntimeError(f"Cannot set name for filters.{self.__class__.__name__}")
class Chat(_ChatUserBaseFilter):
"""Filters messages to allow only those which are from a specified chat ID or username.
Examples:
``MessageHandler(filters.Chat(-1234), callback_method)``
Warning:
:attr:`chat_ids` will give a *copy* of the saved chat ids as :class:`frozenset`. This
is to ensure thread safety. To add/remove a chat, you should use :meth:`add_chat_ids`, and
:meth:`remove_chat_ids`. Only update the entire set by ``filter.chat_ids = new_set``,
if you are entirely sure that it is not causing race conditions, as this will complete
replace the current set of allowed chats.
Args:
chat_id(:obj:`int` | Collection[:obj:`int`], optional):
Which chat ID(s) to allow through.View on GitHub (pinned to d3b69d2e9f)
Solutions
- Remove the `.name` assignment for built-in Chat/User filters; the name auto-reflects its configured IDs/usernames.
- For a custom label, subclass the filter and override the `name` property instead of assigning it.
Example fix
# before f = filters.Chat(chat_id=123) f.name = "my-chat" # RuntimeError # after f = filters.Chat(chat_id=123) # name auto: filters.Chat(123)
Defensive patterns
Strategy: validation
Validate before calling
# do not assign .name to Chat/User filters; subclass instead
class MyChat(filters.Chat):
@property
def name(self):
return 'my-chat' Try / catch
try:
f.name = 'x'
except RuntimeError as e:
if 'Cannot set name' in str(e):
pass Prevention
- Reserve name assignment for custom filter subclasses.
When it happens
Trigger: Executing `filters.Chat(chat_id=1).name = "x"` — any assignment to `.name` on `_ChatUserBaseFilter` subclasses (Chat, User, ForwardedFrom, and similar).
Common situations: Applying the custom-filter naming convention (`self.name = ...`) to built-in filters, or generic code that labels every filter object it receives.
Related errors
- Cannot set name for combined filters.
- Can't set {self._chat_id_name} in conjunction with (already
- Can't set {self._username_name} in conjunction with (already
- The `year` argument is required if the `year` attribute was
- Base URL string contains unsupported insertion: {key}
AI-assisted analysis of python-telegram-bot/python-telegram-bot@d3b69d2e9f (2026-08-28).
Data as JSON: /api/errors/c374ad11e106db8f.
Report an issue: GitHub.