github/copilot-sdk · error · ValueError

invalid auto mode switch request payload

Error message

invalid auto mode switch request payload

What it means

Raised by the auto-mode-switch callback handler when params contain no 'sessionId'. Without a session id the request cannot be routed to any session, so the payload is considered invalid and a ValueError is thrown.

Solutions

  1. Upgrade CLI and SDK to compatible versions so autoModeSwitch.request includes sessionId
  2. Log params before the raise to confirm which key is absent
  3. If emitting the request yourself, always set params['sessionId'] to the active session id
  4. Catch ValueError in the dispatch layer and reply with an error response rather than propagating

Example fix

// before
await client._handle_auto_mode_switch_request({})
// after
await client._handle_auto_mode_switch_request({"sessionId": sid})
Defensive patterns

Strategy: validation

Validate before calling

def valid_auto_mode_params(p):
    return isinstance(p, dict) and bool(p.get("sessionId"))

Type guard

def has_session_id(p) -> bool:
    return isinstance(p, dict) and isinstance(p.get("sessionId"), str) and bool(p["sessionId"])

Try / catch

try:
    await client._handle_auto_mode_switch_request(params)
except ValueError as e:
    logger.error("bad auto-mode-switch payload: %s | params=%r", e, params)

Prevention

When it happens

Trigger: The CLI server sends an autoModeSwitch.request callback whose params dict is empty or lacks the 'sessionId' key (or it is falsy, e.g. None or empty string).

Common situations: CLI/SDK version skew where the server uses a different params schema, a misbehaving CLI build, or a synthesized event dict missing 'sessionId'.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/906bdefc68dcd451. Report an issue: GitHub.

Appendix: source

Thrown at python/copilot/client.py:4978

        recommended_action = params.get("recommendedAction")

        if not session_id or not isinstance(summary, str):
            raise ValueError("invalid exit plan mode request payload")
        if not isinstance(actions, list) or not isinstance(recommended_action, str):
            raise ValueError("invalid exit plan mode request payload")

        with self._sessions_lock:
            session = self._sessions.get(session_id)
        if not session:
            raise ValueError(f"unknown session {session_id}")

        return dict(await session._handle_exit_plan_mode_request(params))

    async def _handle_auto_mode_switch_request(self, params: dict) -> dict:
        """Handle an autoModeSwitch.request callback from the CLI server."""
        session_id = params.get("sessionId")
        if not session_id:
            raise ValueError("invalid auto mode switch request payload")

        with self._sessions_lock:
            session = self._sessions.get(session_id)
        if not session:
            raise ValueError(f"unknown session {session_id}")

        response = await session._handle_auto_mode_switch_request(params)
        return {"response": response}

    async def _handle_system_message_transform(self, params: dict) -> dict:
        """Handle a systemMessage.transform request from the CLI server."""
        session_id = params.get("sessionId")
        sections = params.get("sections")

        if not session_id or not sections:
            raise ValueError("invalid systemMessage.transform payload")

        with self._sessions_lock:

View on GitHub (pinned to cd8cf15dc3)