xtekky/gpt4free · error · RuntimeError

Expected SSE response but got content-type: {content_type}

Error message

Expected SSE response but got content-type: {content_type}

What it means

RuntimeError raised while consuming the DeepSeek chat completion response: the request returned HTTP success but the Content-Type header is not text/event-stream. The provider's streaming parser only understands SSE; a JSON body (usually an inline error or a non-streaming reply) means the conversation flow diverged from expectations, so it fails loudly rather than parsing garbage.

Source

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

            and conversation.parent_message_id
        ):
            json_data["parent_message_id"] = conversation.parent_message_id

        # debug.log(f"DeepSeekAuth: Sending request to {CHAT_COMPLETION_ENDPOINT}")

        async with StreamSession(
            headers=headers, cookies=cookies, proxy=proxy, impersonate="chrome"
        ) as session:
            async with session.post(
                CHAT_COMPLETION_ENDPOINT, json=json_data
            ) as response:
                # debug.log(f"DeepSeekAuth: Processing response... status={response.status}, content-type={response.headers.get('content-type', 'unknown')}")
                await raise_for_status(response)

                # Check if response is actually SSE or regular JSON
                content_type = response.headers.get("content-type", "")
                if "text/event-stream" not in content_type.lower():
                    raise RuntimeError(
                        f"Expected SSE response but got content-type: {content_type}"
                    )

                is_thinking = False
                async for stream_data in sse_stream(response):
                    # Handle different stream data formats
                    if isinstance(stream_data, dict):
                        # Handle first chunk with message IDs (for conversation continuity)
                        if "response_message_id" in stream_data:
                            conversation.parent_message_id = stream_data[
                                "response_message_id"
                            ]
                            # debug.log(f"DeepSeekAuth: Set parent_message_id to {conversation.parent_message_id}")

                        # Handle initial response with fragments (most common case)
                        # Format: {'v': {'response': {'fragments': [{'content': '42', ...}]}}}
                        if "v" in stream_data and isinstance(stream_data["v"], dict):
                            response_obj = stream_data["v"].get("response", {})

View on GitHub (pinned to 973504e177)

Solutions

  1. Re-capture/refresh the HAR and cookies so the request looks like a genuine browser session.
  2. Update g4f to the latest version — content-type handling changes when DeepSeek tweaks its API.
  3. Check any corporate proxy in the path for content-type mangling.
  4. Retry with a fresh conversation object; long-lived sessions can fall out of streaming eligibility.

Example fix

# before
resp = await client.chat.completions.create(model=..., provider=DeepSeek, messages=msgs)
# RuntimeError: Expected SSE response but got content-type: application/json

# after
# refresh har_and_cookies + update g4f, then start a fresh conversation
conversation = None
resp = await client.chat.completions.create(model=..., provider=DeepSeek, messages=msgs, conversation=conversation)
Defensive patterns

Strategy: try-catch

Try / catch

try:
    async for chunk in stream:
        handle(chunk)
except RuntimeError as e:
    if "Expected SSE response" in str(e):
        refresh_har_cookies()
        stream = retry_with_fresh_conversation()

Prevention

When it happens

Trigger: DeepSeek answers with application/json instead of SSE — commonly an error payload delivered with HTTP 200, a PoW/auth check that redirected to a non-stream endpoint, or a backend change. raise_for_status passed, so only the content-type betrays the problem.

Common situations: Expired or weak authorization token causing JSON error bodies; anti-bot challenge interposed (PoW not satisfied); DeepSeek A/B change disabling streaming for some accounts; proxies stripping or rewriting content-type.

Related errors


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