D4Vinci/Scrapling · error · KeyError

Session '{session_id}' not found. Available: {available}

Error message

Session '{session_id}' not found. Available: {available}

What it means

SessionManager.get() raises KeyError when the requested session_id is unknown, and the message helpfully lists all registered ids ('Available: ...'). Requests name a session explicitly; an unregistered name cannot be routed, so the manager refuses rather than guessing.

Source

Thrown at scrapling/spiders/session.py:80

        if session and self._default_session_id == session_id:
            self._default_session_id = next(iter(self._sessions), None)

        return session

    @property
    def default_session_id(self) -> str:
        if self._default_session_id is None:
            raise RuntimeError("No sessions registered")
        return self._default_session_id

    @property
    def session_ids(self) -> list[str]:
        return list(self._sessions.keys())

    def get(self, session_id: str) -> Session:
        if session_id not in self._sessions:
            available = ", ".join(self._sessions.keys())
            raise KeyError(f"Session '{session_id}' not found. Available: {available}")
        return self._sessions[session_id]

    async def start(self) -> None:
        """Start all sessions that aren't already alive."""
        if self._started:
            return

        for sid, session in self._sessions.items():
            if sid not in self._lazy_sessions and not session._is_alive:
                await session.__aenter__()

        self._started = True

    async def close(self) -> None:
        """Close all registered sessions."""
        for sid, session in self._sessions.items():
            if sid in self._lazy_sessions and not session._is_alive:
                continue

View on GitHub (pinned to 5d213a2d47)

Solutions

  1. Read the 'Available:' list in the error message and align the id with what was registered.
  2. Check membership first: if name in manager.session_ids.
  3. Define session ids as constants shared between setup and request code.
  4. When registration is conditional, register an HTTP fallback with the same id so requests never 404 at the session layer.

Example fix

# before
Request(url, session='browser')  # KeyError: only 'http' registered

# after
Request(url, session='http')
Defensive patterns

Strategy: validation

Validate before calling

if session_name not in manager.session_ids:
    raise ValueError(
        f'unknown session {session_name!r}; registered: {manager.session_ids}'
    )
Request(url, session=session_name)

Try / catch

try:
    session = manager.get(session_name)
except KeyError as e:
    logger.error('%s', e)  # message lists available ids
    session = manager.get(manager.default_session_id)  # optional fallback

Prevention

When it happens

Trigger: Request(url, session='browser') when only an 'http' session exists; calling manager.get('default') before any add(); using a session id from old code after it was renamed in config.

Common situations: Renaming sessions in config without updating request code; conditional registration (e.g. only register a browser session when a flag is set) while requests still reference it.

Related errors


AI-assisted analysis of D4Vinci/Scrapling@5d213a2d47 (2026-08-14). Data as JSON: /api/errors/7e0ff837a952374b. Report an issue: GitHub.