Category: provider
Model Studio Qwen Image
Build consistent image generation behavior for the video-agent pipeline by standardizing image.generate inputs/outputs and using DashScope SDK (Python) with the exact model name.
Prerequisites
- - Install SDK (recommended in a venv to avoid PEP 668 limits):
CODEBLOCK0
- - Set
DASHSCOPE_API_KEY in your environment, or add dashscope_api_key to ~/.alibabacloud/credentials (env takes precedence).
Critical model names
Use one of these exact model strings:
Normalized interface (image.generate)
Request
- -
prompt (string, required) - INLINECODE7 (string, optional)
- INLINECODE8 (string, required) e.g.
1024*1024, INLINECODE10 - INLINECODE11 (string, optional)
- INLINECODE12 (int, optional)
- INLINECODE13 (string | bytes, optional)
Response
- -
image_url (string) - INLINECODE15 (int)
- INLINECODE16 (int)
- INLINECODE17 (int)
Quickstart (normalized request + preview)
Minimal normalized request body:
CODEBLOCK1
Preview workflow (download then open):
CODEBLOCK2
Local helper script (JSON request -> image file):
CODEBLOCK3
Parameters at a glance
| Field | Required | Notes |
|---|
| INLINECODE18 | yes | Describe a scene, not just keywords. |
| INLINECODE19 |
no | Best-effort, may be ignored by backend. |
|
size | yes |
WxH format, e.g.
1024*1024,
768*1024. |
|
style | no | Optional stylistic hint. |
|
seed | no | Use for reproducibility when supported. |
|
reference_image | no | URL/file/bytes, SDK-specific mapping. |
Quick start (Python + DashScope SDK)
Use the DashScope SDK and map the normalized request into the SDK call.
Note: For qwen-image-max, the DashScope SDK currently succeeds via ImageGeneration (messages-based) rather than ImageSynthesis.
If the SDK version you are using expects a different field name for reference images, adapt the input mapping accordingly.
CODEBLOCK4
Error handling
| Error | Likely cause | Action |
|---|
| 401/403 | Missing or invalid INLINECODE31 | Check env var or ~/.alibabacloud/credentials, and access policy. |
| 400 |
Unsupported size or bad request shape | Use common
WxH and validate fields. |
| 429 | Rate limit or quota | Retry with backoff, or reduce concurrency. |
| 5xx | Transient backend errors | Retry with backoff once or twice. |
Output location
- - Default output: INLINECODE34
- Override base dir with
OUTPUT_DIR.
Operational guidance
- - Store the returned image in object storage and persist only the URL in metadata.
- Cache results by
(prompt, negative_prompt, size, seed, reference_image hash) to avoid duplicate costs. - Add retries for transient 429/5xx responses with exponential backoff.
- Some backends ignore
negative_prompt, style, or seed; treat them as best-effort inputs. - If the response contains no image URL, surface a clear error and retry once with a simplified prompt.
Size notes
- - Use
WxH format (e.g. 1024*1024, 768*1024). - Prefer common sizes; unsupported sizes can return 400.
Telegram / channel delivery
When the user requests image generation via Telegram (or other channels), after generating and saving the image to workspace output/ai-image-qwen-image/images/, use the message tool to send the image: action=send, target=telegram, media=<file:// URL>. Always pass explicit target when the session may have mixed sources (e.g. control-ui): extract sender_id from Conversation info metadata in user messages and use target: "<sender_id>" (e.g. target: "6869266119") to ensure delivery to the correct Telegram DM and avoid "bot is not a member of the channel chat" errors. Use file:// absolute paths (e.g. file:///Users/fresh/.openclaw/workspace/output/ai-image-qwen-image/images/xxx.png). Do not use ~/ paths.
Anti-patterns
- - Do not invent model names or aliases; use official model IDs only.
- Do not store large base64 blobs in DB rows; use object storage.
- Do not omit user-visible progress for long generations.
References
- - See
references/api_reference.md for a more detailed DashScope SDK mapping and response parsing tips. - See
references/prompt-guide.md for prompt patterns and examples. - For edit workflows, use
skills/ai/image/alicloud-ai-image-qwen-image-edit/.
- - Source list: INLINECODE58
技能名称: taizi-alicloud-ai-image
详细描述:
分类: provider
Model Studio Qwen Image
通过标准化 image.generate 的输入/输出,并使用 DashScope SDK(Python)配合精确模型名称,为视频智能体管道构建一致的图像生成行为。
前提条件
- - 安装 SDK(建议在虚拟环境中安装,以避免 PEP 668 限制):
bash
python3 -m venv .venv
. .venv/bin/activate
python -m pip install dashscope
- - 在环境中设置 DASHSCOPEAPIKEY,或将 dashscopeapikey 添加到 ~/.alibabacloud/credentials 文件中(环境变量优先级更高)。
关键模型名称
使用以下精确模型字符串之一:
- - qwen-image-max
- qwen-image-plus-2026-01-09
标准化接口 (image.generate)
请求
- - prompt(字符串,必填)
- negativeprompt(字符串,可选)
- size(字符串,必填)例如 10241024、7681024
- style(字符串,可选)
- seed(整数,可选)
- referenceimage(字符串 | 字节,可选)
响应
- - image_url(字符串)
- width(整数)
- height(整数)
- seed(整数)
快速开始(标准化请求 + 预览)
最小化的标准化请求体:
json
{
prompt: a cinematic portrait of a cyclist at dusk, soft rim light, shallow depth of field,
negative_prompt: blurry, low quality, watermark,
size: 1024*1024,
seed: 1234
}
预览工作流(下载后打开):
bash
curl -L -o output/ai-image-qwen-image/images/preview.png URLFROM_RESPONSE> && open output/ai-image-qwen-image/images/preview.png
本地辅助脚本(JSON 请求 -> 图像文件):
bash
python skills/ai/image/alicloud-ai-image-qwen-image/scripts/generate_image.py \\
--request {prompt:a studio product photo of headphones,size:1024*1024} \\
--output output/ai-image-qwen-image/images/headphones.png \\
--print-response
参数一览
| 字段 | 必填 | 说明 |
|---|
| prompt | 是 | 描述场景,而不仅仅是关键词。 |
| negative_prompt |
否 | 尽力而为,后端可能忽略。 |
| size | 是 | WxH 格式,例如 1024
1024、7681024。 |
| style | 否 | 可选的风格提示。 |
| seed | 否 | 在支持时用于结果可复现性。 |
| reference_image | 否 | URL/文件/字节,SDK 特定的映射方式。 |
快速入门(Python + DashScope SDK)
使用 DashScope SDK,并将标准化请求映射到 SDK 调用中。
注意:对于 qwen-image-max,DashScope SDK 当前通过 ImageGeneration(基于消息)成功执行,而不是 ImageSynthesis。
如果您使用的 SDK 版本对参考图像期望不同的字段名称,请相应地调整 input 映射。
python
import os
from dashscope.aigc.image_generation import ImageGeneration
优先使用环境变量进行身份验证:export DASHSCOPEAPIKEY=...
或者使用 ~/.alibabacloud/credentials 文件,在 [default] 下包含 dashscopeapikey。
def generate_image(req: dict) -> dict:
messages = [
{
role: user,
content: [{text: req[prompt]}],
}
]
if req.get(reference_image):
# 某些 SDK 版本在消息内容中接受 {image: }。
messages[0][content].insert(0, {image: req[reference_image]})
response = ImageGeneration.call(
model=req.get(model, qwen-image-max),
messages=messages,
size=req.get(size, 1024*1024),
apikey=os.getenv(DASHSCOPEAPI_KEY),
# 如果后端支持,传递可选参数。
negativeprompt=req.get(negativeprompt),
style=req.get(style),
seed=req.get(seed),
)
# 响应是生成风格的信封;提取第一个图像 URL。
content = response.output[choices][0][message][content]
image_url = None
for item in content:
if isinstance(item, dict) and item.get(image):
image_url = item[image]
break
return {
imageurl: imageurl,
width: response.usage.get(width),
height: response.usage.get(height),
seed: req.get(seed),
}
错误处理
| 错误 | 可能原因 | 操作 |
|---|
| 401/403 | DASHSCOPEAPIKEY 缺失或无效 | 检查环境变量或 ~/.alibabacloud/credentials 文件,以及访问策略。 |
| 400 |
不支持的尺寸或错误的请求格式 | 使用常见的 WxH 格式并验证字段。 |
| 429 | 速率限制或配额不足 | 使用退避策略重试,或降低并发。 |
| 5xx | 临时后端错误 | 使用退避策略重试一两次。 |
输出位置
- - 默认输出:output/ai-image-qwen-image/images/
- 通过 OUTPUT_DIR 覆盖基础目录。
操作指南
- - 将返回的图像存储在对象存储中,仅在元数据中持久化 URL。
- 通过 (prompt, negativeprompt, size, seed, referenceimage hash) 缓存结果,以避免重复费用。
- 为临时性的 429/5xx 响应添加指数退避重试。
- 某些后端会忽略 negative_prompt、style 或 seed;将其视为尽力而为的输入。
- 如果响应中不包含图像 URL,则显示明确的错误,并使用简化的提示重试一次。
尺寸说明
- - 使用 WxH 格式(例如 10241024、7681024)。
- 优先使用常见尺寸;不支持的尺寸可能返回 400 错误。
Telegram / 频道投递
当用户通过 Telegram(或其他渠道)请求图像生成时,在生成图像并将其保存到工作区 output/ai-image-qwen-image/images/ 后,使用 message 工具发送图像:action=send,target=telegram,media=。当会话可能包含混合来源(例如 control-ui)时,始终传递明确的 target:从用户消息的 Conversation info 元数据中提取 senderid,并使用 target: id>(例如 target: 6869266119)以确保投递到正确的 Telegram 私聊,并避免出现“bot is not a member of the channel chat”错误。使用 file:// 绝对路径(例如 file:///Users/fresh/.openclaw/workspace/output/ai-image-qwen-image/images/xxx.png)。不要使用 ~/ 路径。
反模式
- - 不要发明模型名称或别名;仅使用官方模型 ID。
- 不要在数据库行中存储大型 base64 数据;请使用对象存储。
- 对于长时间生成,不要省略用户可见的进度。
参考
- - 有关更详细的 DashScope SDK 映射和响应解析技巧,请参阅 references/api_reference.md。
- 有关提示模式和示例,请参阅 references/prompt-guide.md。
- 对于编辑工作流,请使用 skills/ai/image/alicloud-ai-image-qwen-image-edit/。
- - 来源列表:references/sources.md