xtekky/gpt4free · error · RuntimeError

CDP error in {method}: {response['error']}

Error message

CDP error in {method}: {response['error']}

What it means

Raised by SyncCDPSession.call() in g4f/requests/cdp.py when the browser answered a CDP command with a protocol-level error object ({'error': ...}) instead of a result. This is Chrome DevTools Protocol reporting that the command itself was rejected — invalid parameters, wrong target state, or an unknown/disabled domain — surfaced verbatim by g4f as RuntimeError.

Source

Thrown at g4f/requests/cdp.py:778

        self.call("Page.enable")
        self.call("DOM.enable")
        self.call("Runtime.enable")
        self.call("Network.enable")
        self.call("Emulation.setFocusEmulationEnabled", enabled=True)

    def call(self, method: str, **params) -> dict:
        """Send a CDP command and block until the matching response arrives, logging events."""
        self.id_counter += 1
        payload = {"id": self.id_counter, "method": method, "params": params}
        self.ws.send(json.dumps(payload))

        # Blocking loop with a 60s socket timeout — won't hang forever if browser exits
        while True:
            response = json.loads(self.ws.recv())
            if "id" in response:
                if response.get("id") == self.id_counter:
                    if "error" in response:
                        raise RuntimeError(
                            f"CDP error in {method}: {response['error']}"
                        )
                    return response.get("result", {})
            else:
                # Event
                event_method = response.get("method")
                event_params = response.get("params", {})
                if event_method == "Network.requestWillBeSent":
                    self.network_requests.append(event_params)
                elif event_method == "Network.responseReceived":
                    self.network_responses.append(event_params)

    def evaluate_js(self, expression: str) -> Any:
        """Execute JS on the page and return the primitive result value."""
        res = self.call("Runtime.evaluate", expression=expression, returnByValue=True)
        return res.get("result", {}).get("value")

    def get_cookies(self) -> dict:

View on GitHub (pinned to 973504e177)

Solutions

  1. Read the error text embedded in the message — CDP errors (e.g. 'Invalid parameters', 'Not attached to target') name the exact problem.
  2. Enable the required domain first (Page.enable, Network.enable, Runtime.enable...) before its methods.
  3. Re-fetch volatile ids (targetId, requestId) immediately before use rather than caching them.
  4. Match method/params against the Chrome version actually installed; update Chrome or the call accordingly.

Example fix

// before
session.call("Network.getCookies", urls=["https://x.com"])  # before Network.enable

// after
session.call("Network.enable")
cookies = session.call("Network.getCookies", urls=["https://x.com"])
Defensive patterns

Strategy: try-catch

Try / catch

try:
    result = session.call(method, **params)
except RuntimeError as e:
    if "CDP error" in str(e):
        log.error(f"Protocol rejected {method}: {e}")  # inspect embedded error text
        raise CommandRejected(method) from e
    raise

Prevention

When it happens

Trigger: Calling a method of a domain that was never enabled (e.g. Network.getCookies before Network.enable); passing invalid params (bad targetId, malformed expression); calling Page.* on a destroyed target; using a method name not supported by the installed Chrome version.

Common situations: Version skew between the client's assumed protocol and an older/newer Chrome; races where the tab navigated or closed between getting a handle and using it; copy-pasted CDP snippets with wrong parameter names.

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/f7ff4ffc7ba8988e. Report an issue: GitHub.