xtekky/gpt4free · error · Exception

Failed to parse session response: {session_data}

Error message

Failed to parse session response: {session_data}

What it means

Generic Exception raised in DeepSeekAuth when creating a new chat session: the POST response parsed as JSON does not match the expected shape data.biz_data.id, so no chat_session_id can be extracted. The entire session response is embedded in the message, which usually reveals an auth or API-version problem disguised as a parse failure.

Source

Thrown at g4f/Provider/needs_auth/DeepSeek.py:509

                async with session.post(CHAT_SESSION_CREATE_ENDPOINT) as response:
                    await raise_for_status(response)
                    session_data = await response.json()
                    # ID is nested in data.biz_data.id
                    if (
                        session_data.get("data")
                        and "biz_data" in session_data["data"]
                        and "id" in session_data["data"]["biz_data"]
                    ):
                        chat_session_id = session_data["data"]["biz_data"]["id"]
                        conversation.chat_session_id = chat_session_id
                        debug.log(
                            f"DeepSeekAuth: Chat session created: {chat_session_id}"
                        )
                    else:
                        debug.error(
                            f"DeepSeekAuth: Unexpected session response: {session_data}"
                        )
                        raise Exception(
                            f"Failed to parse session response: {session_data}"
                        )
        else:
            debug.log(
                f"DeepSeekAuth: Reusing existing chat session: {conversation.chat_session_id}"
            )

        # Yield conversation object so caller can reuse it for subsequent messages
        yield conversation

        # Upload file if provided - use HTTP/1.1 to avoid HTTP/2 stream errors
        ref_file_ids = []
        if media is not None and len(media) > 0:
            # Take first file from media list
            file_bytes, filename = media[0]
            async with StreamSession(
                headers=headers,
                cookies=cookies,

View on GitHub (pinned to 973504e177)

Solutions

  1. Read the embedded session_data payload — codes like 401/429 pinpoint expired auth vs rate limiting.
  2. Re-export a fresh HAR with a valid authorization token and retry.
  3. Update g4f to the latest release so the parser matches DeepSeek's current response schema.
  4. If rate-limited, back off and retry new conversations later.

Example fix

# before
resp = await client.chat.completions.create(model=..., provider=DeepSeek, messages=[{"role": "user", "content": "hi"}])
# Exception: Failed to parse session response: {'code': 401, 'msg': 'unauthorized'}

# after
# refresh har_and_cookies/deepseek.har with a logged-in capture, then retry
resp = await client.chat.completions.create(model=..., provider=DeepSeek, messages=[{"role": "user", "content": "hi"}])
Defensive patterns

Strategy: try-catch

Try / catch

try:
    resp = await client.chat.completions.create(model=m, provider=DeepSeek, messages=msgs)
except Exception as e:
    if "Failed to parse session response" in str(e):
        if "401" in str(e) or "unauthorized" in str(e).lower():
            refresh_har_and_retry()
        else:
            update_g4f_and_report(e)

Prevention

When it happens

Trigger: First message of a conversation (no existing chat_session_id) where DeepSeek returns an error object or changed schema — e.g. expired token returning {"code": 401, ...}, rate-limit body, or an API revision that moved the session id.

Common situations: Stale HAR token that still passes the local check but is rejected server-side; DeepSeek A/B-changing its session-creation payload; g4f version lagging behind an API change.

Understand the failure class

Related errors


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