GPT-5.5 调用 GPT Image 2 生成图片

Posted April 25, 2026 by XAI 技术团队 ‐ 13 min read

GPT-5.5 不只适合处理文字、代码和复杂工具调用,也可以作为多模态工作流里的“调度模型”:由 gpt-5.5 理解用户意图,再通过 image_generation 工具调用图像模型生成图片。

在 XAI Router 上,这个调用可以走 OpenAI 风格的 Responses API。核心组合是:

  • 主模型:gpt-5.5
  • 工具:image_generation
  • 图像模型:gpt-image-2
  • Base URL:https://api.xairouter.com
  • Key 环境变量:XAI_API_KEY

注意模型 ID 是 gpt-image-2,不是 gpt-img-2

本文参考 OpenAI 官方图片生成工具示例,再把请求地址、鉴权环境变量和模型选择改成 XAI Router 版本。你可以把它理解成三层关系:

层级OpenAI 官方写法XAI Router 写法
API Base URLhttps://api.openai.com/v1https://api.xairouter.com/v1
API KeyOPENAI_API_KEYXAI_API_KEY
主模型gpt-5.5gpt-5.5
图像工具image_generationimage_generation
图像模型GPT Image 模型,例如 gpt-image-2gpt-image-2

OpenAI 官方文档里的关键点是:image_generation 是 Responses API 的内置工具;工具调用结果会包含 base64 图片;gpt-5.5 支持这个工具;真正执行图像生成的是 GPT Image 模型,例如 gpt-image-2。迁移到 XAI Router 时,不需要重写业务逻辑,通常只需要换 baseURL、Key 环境变量和请求域名。


XAI Router 实测通过能力

以下结果基于 2026 年 4 月 25 日https://api.xairouter.com 的实测。接口能力会继续演进,生产系统仍建议保留超时、重试和失败记录。

能力实测结果建议
/v1/models 查询 gpt-5.5gpt-image-2成功,均在模型列表中可用于启动前探测
/v1/responses 文本调用 gpt-5.5成功,status=completed可作为基础连通性检查
/v1/responses + image_generation + gpt-image-2 + stream:true成功,返回 response.completed 和图片 base64推荐路径
tool_choice: { type: "image_generation" }成功,能强制走图像工具适合固定“生成图片”按钮
partial_images成功,但请求 2 张 partial 时实际可能只返回 1 张UI 不要假设 partial 数量固定
quality:"high" + output_format:"png"成功可用于成稿质量控制
/v1/responses 非流式图片生成本次成功返回完整图片可用,但仍建议优先用 stream

因此,在 XAI Router 当前行为下,最稳的生产路径是:Responses API + stream:true + image_generation 工具 + gpt-image-2


最小请求体

如果你只想先确认接口能跑,最小请求体可以这样写:

{
  "model": "gpt-5.5",
  "input": "Generate an elegant image of a glass AI studio with soft light.",
  "tools": [
    {
      "type": "image_generation",
      "model": "gpt-image-2",
      "size": "1024x1024"
    }
  ],
  "stream": true
}

这里的 model: "gpt-5.5" 是 Responses API 的主模型。tools 里的 image_generation 负责生成图像,并把图像模型指定为 gpt-image-2

在实际使用中,建议保留 stream: true。流式响应会把生成进度和最终图片结果直接推回来,便于在脚本里拿到 base64 并落盘。


官方示例改成 XAI Router

OpenAI 官方 JavaScript 示例大致是:

import OpenAI from "openai";

const openai = new OpenAI();

const response = await openai.responses.create({
  model: "gpt-5.5",
  input: "Generate an image of a premium AI workspace",
  tools: [{ type: "image_generation" }],
});

改成 XAI Router 时,主要改两处:

  1. apiKeyprocess.env.XAI_API_KEY 读取。
  2. baseURL 改成 https://api.xairouter.com/v1

同时,如果你希望明确使用 gpt-image-2,可以在 image_generation 工具里指定 model

import fs from "node:fs";
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.XAI_API_KEY,
  baseURL: "https://api.xairouter.com/v1",
});

const response = await client.responses.create({
  model: "gpt-5.5",
  input: "Generate an elegant image of a glass AI studio with soft light.",
  tools: [
    {
      type: "image_generation",
      model: "gpt-image-2",
      size: "1024x1024",
    },
  ],
});

const imageData = response.output
  .filter((output) => output.type === "image_generation_call")
  .map((output) => output.result);

if (imageData.length > 0) {
  fs.writeFileSync("xai-image.png", Buffer.from(imageData[0], "base64"));
}

这就是最接近官方文档的改法。它适合普通同步调用;如果图片生成耗时较长,建议改用下面的流式版本。


cURL:生成并保存为 PNG

先设置 API Key:

export XAI_API_KEY="你的 XAI API Key"

下面这个脚本会调用 gpt-5.5,让它通过 image_generation 工具使用 gpt-image-2 生成图片,并把最终 base64 解码为 xai-generated-image.png

out="xai-generated-image.png"

prompt='Create an elegant technical cover image: a refined glass AI studio, a luminous prompt console, and a generated image appearing as a softly glowing framed visual. No words, no logos, no watermark.'

body=$(jq -nc --arg prompt "$prompt" '{
  model: "gpt-5.5",
  input: $prompt,
  tools: [
    {
      type: "image_generation",
      model: "gpt-image-2",
      size: "1024x1024"
    }
  ],
  stream: true
}')

sse=$(mktemp)
b64=$(mktemp)
trap 'rm -f "$sse" "$b64"' EXIT

curl -sS -N --max-time 300 "https://api.xairouter.com/v1/responses" \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary "$body" > "$sse"

awk '/^data: /{
  data=$0
  sub(/^data: /, "", data)
  if (data != "[DONE]") print data
}' "$sse" |
while IFS= read -r json; do
  jq -r '(.item.result? // .result? // empty)' 2>/dev/null <<< "$json"
done |
awk 'length($0) > max {max=length($0); best=$0} END {if (max > 0) print best}' > "$b64"

if [ ! -s "$b64" ]; then
  echo "No image result found."
  exit 1
fi

base64 -d "$b64" > "$out"
file "$out"

成功时,你会看到类似输出:

xai-generated-image.png: PNG image data, 1024 x 1024, 8-bit/color RGB, non-interlaced

这段脚本做了三件事:

  1. jq 组装 JSON 请求体,避免 shell 引号把长提示词弄坏。
  2. curl -N 接收 Server-Sent Events 流式响应。
  3. 从事件里的 image_generation_call.result 提取最长的 base64 字符串并解码成 PNG。

如果你希望看到中间进度,可以在解析 SSE 时打印 event: 行。常见事件包括:

response.created
response.in_progress
response.output_item.added
response.image_generation_call.generating
response.output_item.done
response.completed

Node.js 示例

如果你在 Node.js 项目里使用 OpenAI SDK,可以把 baseURL 指向 XAI Router:

import fs from "node:fs";
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.XAI_API_KEY,
  baseURL: "https://api.xairouter.com/v1",
});

const stream = await client.responses.create({
  model: "gpt-5.5",
  input:
    "Create an elegant technical cover image: a refined glass AI studio, a luminous prompt console, and a generated image appearing as a softly glowing framed visual. No words.",
  tools: [
    {
      type: "image_generation",
      model: "gpt-image-2",
      size: "1024x1024",
    },
  ],
  stream: true,
});

let imageBase64 = "";

for await (const event of stream) {
  if (event.type === "response.output_item.done") {
    const item = event.item;
    if (item?.type === "image_generation_call" && item.result) {
      imageBase64 = item.result;
    }
  }
}

if (!imageBase64) {
  throw new Error("No image result returned");
}

fs.writeFileSync("xai-generated-image.png", Buffer.from(imageBase64, "base64"));

这个版本的重点是监听 response.output_item.done。当 item.typeimage_generation_call 时,item.result 通常就是最终图片的 base64 内容。


Python 示例

Python 版本同样只需要把客户端指向 XAI Router:

import base64
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["XAI_API_KEY"],
    base_url="https://api.xairouter.com/v1",
)

response = client.responses.create(
    model="gpt-5.5",
    input="Generate an elegant image of a glass AI studio with soft light.",
    tools=[
        {
            "type": "image_generation",
            "model": "gpt-image-2",
            "size": "1024x1024",
        }
    ],
)

image_data = [
    output.result
    for output in response.output
    if output.type == "image_generation_call"
]

if image_data:
    with open("xai-generated-image.png", "wb") as f:
        f.write(base64.b64decode(image_data[0]))

如果你要做 Web 服务,建议把保存逻辑换成对象存储上传,例如 S3、R2、OSS 或你自己的 CDN。数据库里只保存图片 URL、提示词、模型、尺寸和生成状态,不要把大段 base64 直接塞进业务表。


强制调用图像工具

默认情况下,主模型会根据用户输入决定是否调用工具。大多数“请生成一张图片”的请求都会触发 image_generation,但如果你的产品按钮就是“生成图片”,可以用 tool_choice 强制调用:

{
  "model": "gpt-5.5",
  "input": "Draw an elegant AI product cover image.",
  "tools": [
    {
      "type": "image_generation",
      "model": "gpt-image-2",
      "size": "1024x1024"
    }
  ],
  "tool_choice": {
    "type": "image_generation"
  }
}

这个写法适合后台任务、批量生成和固定按钮触发的工作流。聊天场景可以不加,让模型自己判断什么时候需要生成图片。


常用工具选项

image_generation 工具除了 model 之外,还可以设置一些输出参数。实际支持情况以 XAI Router 当前模型能力为准,但接口形态可以按 OpenAI 官方风格组织:

{
  "type": "image_generation",
  "model": "gpt-image-2",
  "size": "1024x1024",
  "quality": "high",
  "output_format": "png"
}

常见选项可以这样理解:

参数用途建议
size输出尺寸头像和封面先用 1024x1024,长图再选纵向尺寸
quality渲染质量预览用 lowmedium,成稿用 high
output_format输出格式需要无损后处理用 png,网页大图可考虑 webp
output_compression压缩级别JPEG/WebP 场景再设置
background背景gpt-image-2 当前不适合请求透明背景
action生成或编辑新图用 generate,多轮上下文可保留 auto

如果你需要透明图,建议先生成干净的纯色背景,再用后处理去底;或者确认当前图像模型和路由是否支持原生透明背景后再开启。


流式 partial images

OpenAI 官方示例里,图片生成可以返回 partial images,用于更快给用户展示中间结果。XAI Router 兼容时,可以在工具里加 partial_images

const stream = await client.responses.create({
  model: "gpt-5.5",
  input: "Draw an elegant AI studio with a generated image panel.",
  stream: true,
  tools: [
    {
      type: "image_generation",
      model: "gpt-image-2",
      size: "1024x1024",
      partial_images: 2,
    },
  ],
});

for await (const event of stream) {
  if (event.type === "response.image_generation_call.partial_image") {
    const imageBuffer = Buffer.from(event.partial_image_b64, "base64");
    fs.writeFileSync(`partial-${event.partial_image_index}.png`, imageBuffer);
  }

  if (event.type === "response.output_item.done") {
    const item = event.item;
    if (item?.type === "image_generation_call" && item.result) {
      fs.writeFileSync("final.png", Buffer.from(item.result, "base64"));
    }
  }
}

产品上可以先展示 partial image,再用最终图替换它。这样用户感知延迟会低很多,尤其适合图片生成页、创意工具和聊天式设计助手。


为什么建议使用 stream

图片生成比普通文本响应耗时更长。虽然当前实测中非流式 Responses 图片生成可以直接返回完整结果,但对脚本和服务端任务来说,stream: true 更直接:

  1. 可以看到 response.image_generation_call.generating 等进度事件。
  2. 可以在同一条连接里拿到最终 image_generation_call
  3. 不需要额外管理轮询、任务状态和超时恢复。

如果你只是做一次性验证,可以先把提示词写短,生成一张 1024x1024 的图。等链路稳定后,再增加更详细的画面描述、品牌约束和风格要求。


提示词建议

图像生成提示词不需要很长,但最好明确四件事:

  • 主体:要生成什么,例如技术封面、产品图、角色头像。
  • 构图:居中、半身、俯视、留白、横幅或方图。
  • 风格:写实、半写实、插画、产品渲染、编辑感。
  • 避免项:不要水印、不要文字、不要变形手、不要低质量伪影。

例如:

Create an elegant technical cover image for an article about GPT-5.5 calling GPT Image 2 through an API router.
Show a refined glass AI studio, a luminous prompt console, and a generated image appearing as a softly glowing framed visual.
Square 1024x1024 composition, premium editorial look, graphite, ivory, soft teal and silver accents.
No words, no logos, no watermark, no clutter.

如果你需要图片里出现准确文字,建议谨慎使用。图像模型可以生成文字,但稳定性通常不如在前端、设计软件或后处理流程里单独排版。


适合落地的产品形态

这个组合适合很多常见产品功能:

场景典型输入输出
博客封面生成文章标题、摘要、风格封面图
电商素材商品名、卖点、背景偏好商品场景图
角色头像人设、职业、服装、表情头像或角色卡
广告创意活动主题、品牌色、禁用元素多个方向的视觉草案
设计助手用户自然语言需求可保存和复用的图片资产

一个稳妥的后端流程通常是:

  1. 接收用户输入和画面约束。
  2. gpt-5.5 整理或补全图像提示词。
  3. 调用 image_generation,图像模型指定 gpt-image-2
  4. 把 base64 解码成图片文件。
  5. 上传到对象存储或 CDN。
  6. 返回图片 URL、模型、尺寸、提示词和生成时间。

这样做比把生成逻辑直接塞在前端更稳:Key 不暴露,超时可控,也方便记录失败原因和重试。


常见问题

gpt-image-2 能不能放在 Responses API 的 model 字段?

不要这样写。Responses API 的 model 字段应该放文本/主线模型,例如 gpt-5.5gpt-image-2 是图像模型,应该放在 image_generation 工具配置里。

需要让图片里出现中文标题怎么办?

建议把文字和图片分开处理:图像模型负责生成无字背景或主视觉,前端、Canvas、设计软件或后处理脚本负责排版中文。这样可控性更高,也更容易保持品牌字体和响应式布局。


小结

通过 XAI Router 调用 GPT-5.5 生成图片,推荐使用 Responses API:

gpt-5.5 -> image_generation tool -> gpt-image-2 -> base64 image result

这个模式适合把“理解需求、扩写提示词、选择工具、生成图像”放在同一个多模态工作流里。对应用开发来说,前端只需要提交自然语言需求,后端用 gpt-5.5 组织调用,再把 gpt-image-2 返回的图片保存到对象存储、CDN 或本地文件即可。

参考资料: