xtekky/gpt4free · error · RuntimeError
Failed to parse image URL: {chunk.decode(errors='replace')}
Error message
Failed to parse image URL: {chunk.decode(errors='replace')} What it means
Raised in the StabilityAI SD3.5 Large Space stream when a 'complete' or 'generating' SSE event's data payload fails to parse as the expected shape: the code expects json.loads(chunk[6:]) to yield a list whose first element has a 'url' key (data[0]['url']). JSONDecodeError, KeyError (no 'url'), or TypeError (data is not a list / None handling aside) all land here, with the raw chunk embedded in the message.
Source
Thrown at g4f/Provider/hf_space/StabilityAI_SD35Large.py:91
) as event_response:
event_response.raise_for_status()
event = None
async for chunk in event_response.content:
if chunk.startswith(b"event: "):
event = chunk[7:].decode(errors="replace").strip()
if chunk.startswith(b"data: "):
if event == "error":
raise ResponseError(
f"GPU token limit exceeded: {chunk.decode(errors='replace')}"
)
if event in ("complete", "generating"):
try:
data = json.loads(chunk[6:])
if data is None:
continue
url = data[0]["url"]
except (json.JSONDecodeError, KeyError, TypeError) as e:
raise RuntimeError(
f"Failed to parse image URL: {chunk.decode(errors='replace')}",
e,
)
if event == "generating":
yield ImagePreview(url, prompt)
else:
yield ImageResponse(url, prompt)
break
View on GitHub (pinned to 973504e177)
Solutions
- Inspect the chunk text in the message to see the actual payload shape; adapt the url extraction (data[0]['url']) to match.
- If the payload is an error object, address the underlying Space error (often ZeroGPU quota — see the event: error path just above).
- Retry — transient malformed frames during Space restarts resolve themselves.
- If the shape permanently changed, update the parser in StabilityAI_SD35Large.py to the new Gradio output format.
Defensive patterns
Strategy: try-catch
Try / catch
try:
...
except RuntimeError as e:
if 'Failed to parse image URL' in str(e):
# inspect chunk in message; transient frames justify one retry
... Prevention
- Retry once on parse failures — Gradio frames are intermittently malformed under load.
- Capture the failing chunk in logs to detect permanent shape changes early.
- Keep g4f updated so Space protocol changes are handled upstream.
When it happens
Trigger: The Space returns a dict instead of a list (Gradio version change), an output entry without a url (e.g. an error/status object), a JSON payload split across SSE lines, or an HTML/HTML-fragment payload in the data frame; also fires if the event variable from a previous 'event:' line mismatches the data frame ordering.
Common situations: Gradio upgrades on the Space changing output payload shape ([{url:...}] vs {url:...}); error frames mislabeled as 'generating'; intermittent malformed frames during Space cold start.
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 message: {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/7652f22323bfead8.
Report an issue: GitHub.