{"record":{"id":"9b127093f42ba013","repo":"ATH-MaaS/Pixelle-Video","slug":"openai-api","errorCode":null,"errorMessage":"OpenAI API 返回数据为空","messagePattern":"OpenAI API 返回数据为空","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"pixelle_video/services/api_services/image_gpt.py","lineNumber":101,"sourceCode":"        if image_urls and isinstance(image_urls, list) and len(image_urls) > 0:\n            # 中转站通常支持通过 extra_body 传递 image_url 或 ref_image\n            # 这里我们将第一张图作为参考图\n            ref_images = [self._encode_image_to_base64(image_urls[i]) for i in range(min(len(image_urls), 6))]\n            extra_body = {\"image_url\": ref_images}\n\n        while attempts < self.max_attempts:\n            try:\n                response = self.client.images.generate(\n                    model=model,\n                    prompt=prompt,\n                    size=size,\n                    quality=quality,\n                    n=1,\n                    extra_body=extra_body\n                )\n                \n                if not response or not response.data:\n                    raise RuntimeError(\"OpenAI API 返回数据为空\")\n\n                img_data = response.data[0]\n                file_path = None\n\n                # 1. 处理 Base64 格式 (中转站常用)\n                if hasattr(img_data, 'b64_json') and img_data.b64_json:\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                        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","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/ATH-MaaS/Pixelle-Video/blob/848b054e4fae40dabc62ec58e960b573e83793ac/pixelle_video/services/api_services/image_gpt.py#L83-L119","documentation":"generate_image calls the OpenAI Images API (via client.images.generate with extra_body support) and raises RuntimeError when the SDK returns None or an empty data list. This means the relay/proxy or OpenAI returned a success-shaped response with no image payloads.","triggerScenarios":"client.images.generate(...) returns None, or response.data is an empty list — typically from a third-party relay/中转站 returning 200 with an empty body, or the model returning no images.","commonSituations":"Using an unofficial OpenAI-compatible relay that silently fails, base_url pointing to a proxy with intermittent empty responses, model name not supported by the endpoint, content policy rejection returning empty data instead of an error.","solutions":["Retry the request — this library already retries per model; transient relay failures usually resolve on retry.","Verify base_url points to a working endpoint and the API key is valid for that endpoint.","Try a different supported model (e.g. 'gpt-image-1' vs 'dall-e-3') via the model argument.","Check the relay provider's status/logs; switch to the official API to rule out relay issues."],"exampleFix":"// before\nurl = client.generate_image(prompt, model=\"dall-e-3\")\n\n// after\nfrom tenacity import retry, stop_after_attempt\n@retry(stop=stop_after_attempt(3), wait=wait_fixed(10))\ndef gen():\n    return client.generate_image(prompt, model=\"gpt-image-1\")","handlingStrategy":"retry","validationCode":"assert api_key and api_key.startswith((\"sk-\",)), \"OpenAI/relay key missing\"\nassert base_url, \"base_url for OpenAI-compatible endpoint not configured\"","typeGuard":"def has_image_data(resp) -> bool:\n    return resp is not None and bool(getattr(resp, \"data\", None))","tryCatchPattern":"try:\n    out = client.generate_image(prompt)\nexcept RuntimeError as e:\n    if \"返回数据为空\" in str(e):\n        time.sleep(10)\n        out = client.generate_image(prompt)  # transient relay failure\n    else:\n        raise","preventionTips":["Prefer response_format='b64_json' when using relays","Retry empty responses — they are usually transient relay issues","Monitor relay provider status; fall back to the official API when needed","Pin a model name known to work with your endpoint"],"tags":["openai","empty-response","image-generation"],"backgroundTag":"empty-api-response","analyzedSha":"848b054e4fae40dabc62ec58e960b573e83793ac","analyzedAt":"2026-08-30T03:24:41.468Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}