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
- 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.
- 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.
- Pass a valid api_key/zerogpu token (or cookies) so the Space does not emit auth-error frames into the data stream.
- 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
- Prefer raise_for_status-visible HTTP errors over stream parsing by validating the event_id assert path.
- Pin to known-good HF Space revisions when possible.
- Retry image requests once — Gradio streams are occasionally malformed during Space cold start.
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
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Failed to parse image URL: {chunk.decode(errors='replace')}
- GPU token limit exceeded: {chunk.decode(errors='replace')}
- No media files provided for image generation.
- Failed to read response: {chunk.decode(errors='replace')}
- Invalid JSON data: {rest}
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/7075b7012f1e7f58.
Report an issue: GitHub.