{"record":{"id":"fc9c43cb2b549308","repo":"ATH-MaaS/Pixelle-Video","slug":"url-b64-json","errorCode":null,"errorMessage":"未在响应中找到 url 或 b64_json","messagePattern":"未在响应中找到 url 或 b64_json","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"pixelle_video/services/api_services/image_gpt.py","lineNumber":128,"sourceCode":"                        file_name = f\"gpt_{int(time.time())}_{uuid.uuid4().hex[:6]}.png\"\n                        file_path = os.path.join(save_dir, file_name)\n                        with open(file_path, \"wb\") as f:\n                            f.write(base64.b64decode(img_data.b64_json))\n                        return file_path\n                    return img_data.b64_json\n\n                # 2. 处理 URL 格式\n                elif hasattr(img_data, 'url') and img_data.url:\n                    url = img_data.url\n                    if save_dir:\n                        os.makedirs(save_dir, exist_ok=True)\n                        file_name = f\"gpt_{int(time.time())}_{uuid.uuid4().hex[:6]}.png\"\n                        file_path = os.path.join(save_dir, file_name)\n                        if self.image_processor.download_image(url, file_path):\n                            return file_path\n                        return url\n                \n                raise RuntimeError(\"未在响应中找到 url 或 b64_json\")\n            except Exception as e:\n                last_error = e\n                msg = str(e)\n                # Other errors: wait before retry\n                print(f\"Image generation error: {e}. Retrying in 10 seconds.\")\n                time.sleep(10)\n                break  # Break inner loop to retry all models\n            attempts += 1\n        raise Exception(f\"Max attempts reached, failed to generate image. Last error: {last_error}\")\n\n    def generate_images(self, prompt, count=4, size=\"1024x1024\", quality=\"standard\", model=None):\n        \"\"\"Generate multiple image URLs by calling Images API 'count' times.\"\"\"\n        urls = []\n        for _ in range(count):\n            url = self.generate_image(prompt=prompt, size=size, quality=quality, model=model)\n            urls.append(url)\n        return urls\n","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/ATH-MaaS/Pixelle-Video/blob/848b054e4fae40dabc62ec58e960b573e83793ac/pixelle_video/services/api_services/image_gpt.py#L110-L146","documentation":"generate_image raises RuntimeError when the API response contains image data whose items expose neither a usable url nor b64_json. The code handles b64_json and url cases; anything else (e.g. revised_prompt only, or unexpected relay formats) falls through to this error.","triggerScenarios":"The Images API response data[0] lacks both b64_json and url attributes/values — e.g. a relay returning a non-standard response object, or response_format mismatch (url requested but provider returns only b64, with b64 empty).","commonSituations":"Third-party OpenAI-compatible relays with non-standard response shapes, requesting response_format='url' from a model that only returns base64, SDK version changes altering response attribute names.","solutions":["Log response.data[0] to see what fields the provider actually returns and adapt parsing.","Set response_format explicitly ('b64_json' is safest with relays) or update the client SDK so attributes match.","Switch to a relay/endpoint that returns standard OpenAI response shapes.","Rely on the built-in retry loop (it retries across models) but fix the endpoint for a permanent solution."],"exampleFix":"// before\nresp = client.images.generate(model=\"dall-e-3\", prompt=prompt, n=1)\n\n// after\nresp = client.images.generate(model=\"dall-e-3\", prompt=prompt, n=1,\n                              response_format=\"b64_json\")","handlingStrategy":"type-guard","validationCode":"# After the API call, before consuming the image:\nimg = response.data[0]\nif not (getattr(img, 'url', None) or getattr(img, 'b64_json', None)):\n    print('unexpected response item:', img)","typeGuard":"def extract_image_payload(item):\n    b64 = getattr(item, \"b64_json\", None)\n    if b64:\n        return (\"b64\", b64)\n    url = getattr(item, \"url\", None)\n    if url:\n        return (\"url\", url)\n    return None","tryCatchPattern":"try:\n    out = client.generate_image(prompt)\nexcept RuntimeError as e:\n    if \"未在响应中找到 url 或 b64_json\" in str(e):\n        out = client.generate_image(prompt, model=\"dall-e-3\")  # try another model\n    else:\n        raise","preventionTips":["Set response_format explicitly and match parsing to it","Log the raw response item when the shape is unexpected","Keep the OpenAI SDK updated so attribute names match the API","Test new relay endpoints with a single small request first"],"tags":["openai","response-parsing","image-generation"],"backgroundTag":"unexpected-api-response-shape","analyzedSha":"848b054e4fae40dabc62ec58e960b573e83793ac","analyzedAt":"2026-08-30T03:24:41.468Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}