xtekky/gpt4free · error · RuntimeError

Failed to read response: {chunk.decode(errors='replace')}

Error message

Failed to read response: {chunk.decode(errors='replace')}

What it means

Raised in the Cohere Command HF Space provider while reading the streamed NDJSON response: response.content is iterated as raw bytes and each chunk is fed directly to json.loads. aiohttp yields chunks at network-buffer boundaries, not line boundaries, so a chunk can contain half a JSON object, multiple objects, or trailing partial data — any of which makes json.loads raise JSONDecodeError. The raw chunk is included in the message for diagnosis.

Source

Thrown at g4f/Provider/hf_space/CohereForAI_C4AI_Command.py:161

                        "is_retry": False,
                        "is_continue": False,
                        "web_search": False,
                        "tools": [],
                    }
                ),
                content_type="application/json",
            )
            async with session.post(
                f"{cls.conversation_url}/{conversation.conversationId}",
                data=data,
                proxy=proxy,
            ) as response:
                await raise_for_status(response)
                async for chunk in response.content:
                    try:
                        data = json.loads(chunk)
                    except json.JSONDecodeError as e:
                        raise RuntimeError(
                            f"Failed to read response: {chunk.decode(errors='replace')}",
                            e,
                        )
                    if data["type"] == "stream":
                        yield data["token"].replace("\u0000", "")
                    elif data["type"] == "title":
                        yield TitleGeneration(data["title"])
                    elif data["type"] == "finalAnswer":
                        break

View on GitHub (pinned to 973504e177)

Solutions

  1. Buffer reads until newline before parsing: iterate with readline() (response.content is an aiohttp StreamReader) or accumulate a bytes buffer and split on b'\n'.
  2. If it fires on the very first chunk, check the chunk text in the message for an HTML error/Cloudflare page — then the fix is the Space being up / cookies, not parsing.
  3. Retry the request: boundary-split failures are intermittent, but the correct fix is line-buffered parsing.

Example fix

# before
async for chunk in response.content:
    try:
        data = json.loads(chunk)
    except json.JSONDecodeError as e:
        raise RuntimeError(f"Failed to read response: {chunk.decode(errors='replace')}", e)

# after (line-buffered NDJSON parsing)
buffer = b""
async for chunk in response.content:
    buffer += chunk
    while b"\n" in buffer:
        line, buffer = buffer.split(b"\n", 1)
        if not line.strip():
            continue
        data = json.loads(line)
Defensive patterns

Strategy: try-catch

Try / catch

try:
    ...
except RuntimeError as e:
    if 'Failed to read response' in str(e):
        await asyncio.sleep(1)  # chunk-boundary flakes: single retry usually succeeds
        ...

Prevention

When it happens

Trigger: Any stream where an NDJSON line crosses a TCP/TLS buffer boundary (essentially every non-trivial response — the bug fires intermittently whenever a token line is split across reads); responses where the server sends multiple small JSON objects in one read; error pages/HTML injected mid-stream.

Common situations: Long generations on the cohere-for-ai-c4ai-command Space failing partway through with this error; the same code working for short replies (single small chunk) but failing for long ones — a classic symptom of missing line buffering.

Related errors


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