xtekky/gpt4free · error · ValueError
Unexpected response: {result}
Error message
Unexpected response: {result} What it means
Raised by HuggingFaceMedia when the JSON response for a media task could not be converted into an ImageResponse — the except Exception around image extraction (from result['images'] / ['data'] / ['output']) fired. ValueError with the full result payload, meaning the router returned JSON in an unexpected shape for an image task.
Source
Thrown at g4f/Provider/needs_auth/hf/HuggingFaceMedia.py:288
)
elif task == "text-to-image":
try:
return provider_info, ImageResponse(
[
item["url"]
if isinstance(item, dict)
else item
for item in result.get(
"images",
result.get(
"data", result.get("output")
),
)
],
prompt,
)
except Exception:
raise ValueError(f"Unexpected response: {result}")
elif (
task == "text-to-video"
and result.get("output") is not None
):
return provider_info, VideoResponse(
result["output"], prompt
)
raise ValueError(f"Unexpected response: {result}")
async for chunk in save_response_media(
response, prompt, [aspect_ratio, model]
):
return provider_info, chunk
await raise_for_status(last_response)
background_tasks = set()
running_tasks = set()
started = time.time()View on GitHub (pinned to 973504e177)
Solutions
- Inspect the result payload in the message — it shows exactly which shape came back
- Retry — some providers intermittently return empty bodies with success status
- Pin a different provider via 'model:provider_key' to avoid the misbehaving one
- Update g4f / report the provider's new response schema
Defensive patterns
Strategy: retry
Try / catch
try:
provider_info, media = await generate(extra_body)
except ValueError as e:
if 'Unexpected response' in str(e):
provider_info, media = await generate(extra_body) # empty shape is often transient
else:
raise Prevention
- Retry once on malformed image payloads before failing over
- Pin providers with known-stable response shapes via 'model:provider'
- Log the embedded result payload to identify which provider misbehaved
When it happens
Trigger: text-to-image (or similar) task returns JSON where the images/data/output fields are missing, null, or malformed so the list comprehension inside ImageResponse construction throws.
Common situations: A provider returns a nested error object or an empty generation with 200 status; provider API schema changes; non-image JSON (e.g. a text blob) routed to an image task.
Related errors
- Invalid image element: {element}
- No images found
- {item}
- Failed to parse message: {chunk.decode(errors='replace')}
- No media files provided for image generation.
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/66ae86c65e3e060f.
Report an issue: GitHub.