": "status_code=400, body={\"code\":305000002,\"message\":\"Function execution f...
🚨 错误信息
{
"$error": "status_code=400, body={\"code\":305000002,\"message\":\"Function execution failed, please check the code of the function. Detail: UserFuncExecErr: API request failed with status code 451. Details: {\\\"error\\\":{\\\"message\\\":\\\"{\\\\n \\\\\\\"data\\\\\\\": [],\\\\n \\\\\\\"errors\\\\\\\": [\\\\n {\\\\n \\\\\\\"code\\\\\\\": \\\\\\\"invalidReferenceImage\\\\\\\",\\\\n \\\\\\\"message\\\\\\\": \\\\\\\"Invalid value for an element in the 'referenceImages' parameter. Each reference image must be specified in one of the following formats: a UUID v4 string of a previously uploaded or generated image, a data URI string, a base64-encoded image, or a publicly accessible URL. Supported formats are: PNG, JPG, and WEBP.\\\\\\\",\\\\n \\\\\\\"parameter\\\\\\\": \\\\\\\"referenceImages\\\\\\\",\\\\n \\\\\\\"type\\\\\\\": \\\\\\\"string[]\\\\\\\",\\\\n \\\\\\\"documentation\\\\\\\": \\\\\\\"https://***.***.ai/***/***/***/***\\\\n \\\\\\\"taskUUID\\\\\\\": \\\\\\\"390e2593-4541-4909-a9b0-c00685287795\\\\\\\"\\\\n }\\\\n ]\\\\n}\\\",\\\"type\\\":\\\"rix_api_error\\\",\\\"param\\\":\\\"\\\",\\\"code\\\":null}}\\n File \\\"/opt/bytefaas/packages/requests/models.py\\\", line 1026, in raise_for_status\\n raise HTTPError(http_error_msg, response=self)\\nrequests.exceptions.HTTPError: 451 Client Error: Unavailable For Legal Reasons for url: https://www.dmxapi.cn/v1/images/generations\\n\\nDuring handling of the above exception, another exception occurred:\\n\\nTraceback (most recent call last):\\n File \\\"/opt/bytefaas/runtime/core/_app.py\\\", line 88, in invoke\\n data = self.user_function(args)\\n ^^^^^^^^^^^^^^^^^^^^^^^^\\n Merge_three_images_CN/Merge_three_images_CN.py\\\", line 79, in handler\\n raise Exception(f\\\"API request failed with status code {response.status_code}. Details: {error_details}\\\")\\nException: API request failed with status code 451. Details: {\\\"error\\\":{\\\"message\\\":\\\"{\\\\n \\\\\\\"data\\\\\\\": [],\\\\n \\\\\\\"errors\\\\\\\": [\\\\n {\\\\n \\\\\\\"code\\\\\\\": \\\\\\\"invalidReferenceImage\\\\\\\",\\\\n \\\\\\\"message\\\\\\\": \\\\\\\"Invalid value for an element in the 'referenceImages' parameter. Each reference image must be specified in one of the following formats: a UUID v4 string of a previously uploaded or generated image, a data URI string, a base64-encoded image, or a publicly accessible URL. Supported formats are: PNG, JPG, and WEBP.\\\\\\\",\\\\n \\\\\\\"parameter\\\\\\\": \\\\\\\"referenceImages\\\\\\\",\\\\n \\\\\\\"type\\\\\\\": \\\\\\\"string[]\\\\\\\",\\\\n \\\\\\\"documentation\\\\\\\": \\\\\\\"https://***.***.ai/***/***/***/***\\\\n \\\\\\\"taskUUID\\\\\\\": \\\\\\\"390e2593-4541-4909-a9b0-c00685287795\\\\\\\"\\\\n }\\\\n ]\\\\n}\\\",\\\"type\\\":\\\"rix_api_error\\\",\\\"param\\\":\\\"\\\",\\\"code\\\":null}}\\n\"}"
}
🤖 AI解决方案
代码报错分析
1. 错误翻译
原文:Invalid value for an element in the 'referenceImages' parameter. Each reference image must be specified in one of the following formats: a UUID v4 string of a previously uploaded or generated image, a data URI string, a base64-encoded image, or a publicly accessible URL. Supported formats are: PNG, JPG, and WEBP.
译文:'referenceImages'参数中的元素值无效。每个参考图像必须以以下格式之一指定:先前上传或生成的图像的UUID v4字符串、数据URI字符串、base64编码图像或可公开访问的URL。支持的格式为:PNG、JPG和WEBP。
2. 原因分析
这是API调用参数验证错误,具体问题出现在referenceImages参数上:
- 根本原因:传递给API的参考图像格式不符合要求
- 错误位置:
Merge_three_images_CN.py文件的handler函数第79行 - 状态码:451(因法律原因不可用)表明请求被服务器拒绝
3. 解决方案
检查并修正referenceImages参数的格式:
# 正确示例 - 使用base64编码
import base64
def read_image_to_base64(image_path):
with open(image_path, 'rb') as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
# 或者使用数据URI格式
def create_data_uri(image_path):
import mimetypes
mime_type, _ = mimetypes.guess_type(image_path)
with open(image_path, 'rb') as image_file:
base64_data = base64.b64encode(image_file.read()).decode('utf-8')
return f"data:{mime_type};base64,{base64_data}"
# 在API调用中使用
reference_images = [
read_image_to_base64('image1.png'), # base64格式
create_data_uri('image2.jpg'), # 数据URI格式
"https://example.com/image3.webp" # 公开URL格式
]
4. ️ 预防措施
referenceImages数组中的每个元素格式是否正确import re
import urllib.parse
def validate_reference_image(image_input):
# 检查UUID格式
uuid_pattern = re.compile(r'^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$', re.I)
if uuid_pattern.match(image_input):
return True
# 检查数据URI格式
if image_input.startswith('data:image/'):
return True
# 检查base64编码(简单的格式检查)
if re.match(r'^[A-Za-z0-9+/]*={0,2}$', image_input) and len(image_input) > 100:
return True
# 检查URL格式
try:
result = urllib.parse.urlparse(image_input)
return all([result.scheme, result.netloc])
except:
return False
return False
---
请确保您的参考图像格式符合上述要求后重新调用API。