{"record":{"id":"047c7748b16f1597","repo":"abi/screenshot-to-code","slug":"unexpected-response-from-context-result","errorCode":null,"errorMessage":"Unexpected response from {context}: {result}","messagePattern":"Unexpected response from (.+?): (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"backend/image_generation/replicate.py","lineNumber":122,"sourceCode":"def _extract_output_url(result: Any, context: str) -> str:\n    if isinstance(result, str):\n        return result\n\n    if isinstance(result, dict):\n        url = cast(Any, result.get(\"url\"))\n        if isinstance(url, str) and url:\n            return url\n\n    if isinstance(result, list) and len(result) > 0:\n        first = cast(Any, result[0])\n        if isinstance(first, str) and first:\n            return first\n        if isinstance(first, Mapping):\n            url = cast(Any, first.get(\"url\"))\n            if isinstance(url, str) and url:\n                return url\n\n    raise ValueError(f\"Unexpected response from {context}: {result}\")\n\n\nasync def call_replicate_model(\n    model_path: str, input: dict[str, Any], api_token: str\n) -> Any:\n    return await _run_prediction(\n        f\"{REPLICATE_API_BASE_URL}/models/{model_path}/predictions\",\n        {\"input\": input},\n        api_token,\n    )\n\n\nasync def call_replicate_version(\n    version: str, input: dict[str, Any], api_token: str\n) -> Any:\n    return await _run_prediction(\n        f\"{REPLICATE_API_BASE_URL}/predictions\",\n        {\"version\": version, \"input\": input},","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/abi/screenshot-to-code/blob/d026163f586dfa8c5c10d28c36edd59a9d3b0e88/backend/image_generation/replicate.py#L104-L140","documentation":"Raised by _extract_output_url when the prediction's `output` field doesn't contain anything URL-shaped. The function accepts: a plain string, a dict with a non-empty `url` key, or a non-empty list whose first element is a string or a url-bearing mapping. Anything else — None, an empty list, a list of non-strings, a dict without url — raises this ValueError with the offending payload embedded.","triggerScenarios":"The prediction succeeded but output is null (some models emit output artifacts elsewhere, e.g. only in `data` or `logs`); output is a dict with the image under a different key (e.g. \"image\" instead of \"url\"); output is an empty list because the model returned zero images.","commonSituations":"Switching call_replicate_model to a new model whose output schema differs (e.g. returns a list of dicts without \"url\", or a nested structure); models that stream results to a different field; output empty because the model produced no image.","solutions":["Look at the embedded `result` in the message — it shows exactly what the model returned.","Check the model's docs on Replicate for its output schema and adapt _extract_output_url if it uses a different key.","If output is empty/None, treat it as a model-side failure and retry or adjust input.","Test the model once via the Replicate playground to see its real output shape."],"exampleFix":"# before — model returns {\"image\": \"https://...\"}\n# _extract_output_url raises because there is no \"url\" key\n\n# after — extend the dict branch\nurl = cast(Any, result.get(\"url\") or result.get(\"image\"))","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"from typing import Any, Mapping\n\ndef output_has_extractable_url(output: Any) -> bool:\n    if isinstance(output, str) and output:\n        return True\n    if isinstance(output, Mapping):\n        return isinstance(output.get(\"url\"), str) and bool(output[\"url\"])\n    if isinstance(output, list) and output:\n        first = output[0]\n        if isinstance(first, str) and first:\n            return True\n        if isinstance(first, Mapping):\n            return isinstance(first.get(\"url\"), str) and bool(first[\"url\"])\n    return False","tryCatchPattern":"try:\n    url = _extract_output_url(result, context=\"sdxl\")\nexcept ValueError as e:\n    if \"Unexpected response\" in str(e):\n        log.error(\"Model output schema not URL-shaped: %r\", result)\n    raise","preventionTips":["Inspect the model's documented output schema before switching models","Log the raw `output` payload once when integrating a new model","Extend _extract_output_url for non-\"url\" keys rather than string-matching error text"],"tags":["replicate","image-generation","response-validation","output-parsing"],"backgroundTag":null,"analyzedSha":"d026163f586dfa8c5c10d28c36edd59a9d3b0e88","analyzedAt":"2026-08-14T22:02:06.951Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}