xtekky/gpt4free · error · RuntimeError

Failed to parse message: {chunk.decode(errors='replace')}

Error message

Failed to parse message: {chunk.decode(errors='replace')}

What it means

Raised while streaming the Gradio SSE feed of the black-forest-labs/FLUX.1-dev HF Space: a 'data: ' line's payload failed json.loads, or the parsed object lacked an expected key (output/data/progress structure). The except clause catches json.JSONDecodeError, KeyError, and TypeError from the whole processing block, and re-raises with the raw chunk text so you can see exactly what the Space sent.

Source

Thrown at g4f/Provider/hf_space/BlackForestLabs_Flux1Dev.py:175

                                        json_data["output"]["error"] = json_data[
                                            "output"
                                        ]["error"].split(" <a ")[0]
                                        raise ResponseError(
                                            json_data["output"]["error"]
                                        )
                                    if (
                                        "output" in json_data
                                        and "data" in json_data["output"]
                                    ):
                                        yield Reasoning(status="")
                                        if len(json_data["output"]["data"]) > 0:
                                            yield ImageResponse(
                                                json_data["output"]["data"][0]["url"],
                                                prompt,
                                            )
                                    break
                            except (json.JSONDecodeError, KeyError, TypeError) as e:
                                raise RuntimeError(
                                    f"Failed to parse message: {chunk.decode(errors='replace')}",
                                    e,
                                )

View on GitHub (pinned to 973504e177)

Solutions

  1. Look at the chunk text in the message — if it is HTML or an error string, the Space itself is down/paused; retry later or pin a working Space snapshot.
  2. If the chunk is valid JSON with a new shape, the parser needs updating for the new Gradio event format in BlackForestLabs_Flux1Dev.create_async_generator.
  3. Pass a valid api_key/zerogpu token (or cookies) so the Space does not emit auth-error frames into the data stream.
  4. Retry the request — Space cold starts and restarts often produce one-off malformed streams.
Defensive patterns

Strategy: try-catch

Validate before calling

def is_valid_gradio_data(chunk: bytes) -> bool:
    return chunk.startswith(b'data: ') and chunk[6:].strip().startswith((b'{', b'['))

Try / catch

try:
    async for r in BlackForestLabs_Flux1Dev.create_async_generator(model=model, messages=messages, prompt=p):
        ...
except RuntimeError as e:
    if 'Failed to parse message' in str(e):
        # Space sent an unexpected frame: safe to retry once, then give up
        ...

Prevention

When it happens

Trigger: The Gradio queue sends an unexpected message type whose shape lacks keys the code indexes (e.g. progress_data entries without desc/index/length); the Space returns an HTML error page or plain-text error inside the SSE stream (JSON decode fails); the Space's Gradio version changed its queue/join queue/data protocol; or a 'process_completed' event arrives with an empty output.data list shape the code does not expect.

Common situations: HF Space upgraded to a newer Gradio version changing event payloads; the Space being paused/rebuilding and serving an error page on the SSE endpoint; ZeroGPU quota errors surfacing as non-JSON data frames; transient Space restarts mid-stream.

Understand the failure class

Related errors


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