Textualize/textual · error · KeyError

No mode called {mode!r}

Error message

No mode called {mode!r}

What it means

KeyError raised by App.get_screen_stack(mode) when the requested mode name has no associated screen stack — i.e. the mode was never registered via MODES or add_mode, or was removed.

Source

Thrown at src/textual/app.py:1267

    def get_screen_stack(self, mode: str | None = None) -> list[Screen]:
        """Get the screen stack for the given mode, or the current mode if no mode is specified.

        Args:
            mode: Name of a model

        Raises:
            KeyError: If there is no mode.

        Returns:
            A list of screens. Note that this is a copy, and modifying the list will not impact the app's screen stack.
        """
        if mode is None:
            mode = self._current_mode
        try:
            stack = self._screen_stacks[mode]
        except KeyError:
            raise KeyError(f"No mode called {mode!r}") from None
        return stack.copy()

    def exit(
        self,
        result: ReturnType | None = None,
        return_code: int = 0,
        message: RenderableType | None = None,
    ) -> None:
        """Exit the app, and return the supplied result.

        Args:
            result: Return value.
            return_code: The return code. Use non-zero values for error codes.
            message: Optional message to display on exit.
        """
        self._exit = True
        self._return_value = result
        self._return_code = return_code

View on GitHub (pinned to 06dbeef4bb)

Solutions

  1. Check mode in app._modes / use switch_mode first so the stack exists
  2. Verify the exact spelling against MODES keys or add_mode calls

Example fix

# before
stack = app.get_screen_stack("edtior")  # typo

# after
await app.switch_mode("editor")
stack = app.get_screen_stack("editor")
Defensive patterns

Strategy: validation

Validate before calling

if mode not in app._modes:
    raise ValueError(f'unknown mode {mode}')
# else stack exists after switch

Try / catch

try:
    stack = app.get_screen_stack(mode)
except KeyError:
    await app.switch_mode(mode)
    stack = app.get_screen_stack(mode)

Prevention

When it happens

Trigger: Calling app.get_screen_stack("editor") when 'editor' is not in app._screen_stacks; querying a removed mode.

Common situations: Typos in mode names; querying a mode before switching to it; accessing a mode removed with remove_mode().

Related errors


AI-assisted analysis of Textualize/textual@06dbeef4bb (2026-08-27). Data as JSON: /api/errors/334b85ee13bdbd18. Report an issue: GitHub.