You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用OpenAI Responses API(视觉输入)遇TypeScript类型错误求助

OpenAI Responses API(Vision)TypeScript类型错误解决

按照官方文档编写带图像输入的OpenAI Responses API代码时,出现如下TypeScript类型错误:

Type '{ type: "input_text"; text: string; } | { type: "input_image"; image_url: string; }' is not assignable to type 'ResponseInputContent'.

引发错误的代码示例:

import OpenAI from "openai";
const client = new OpenAI();

const response = await client.responses.create({
    model: "gpt-5",
    input: [
        {
            role: "user",
            content: [
                {
                    type: "input_text",
                    text: "What is in this image?",
                },
                {
                    type: "input_image",
                    image_url: "https://openai-documentation.vercel.app/images/cat_and_otter.png",
                },
            ],
        },
    ],
});

console.log(response.output_text);

解决方法

  • 更新OpenAI SDK版本:Responses API属于较新接口,旧版本SDK的类型定义未同步更新。执行以下命令升级依赖:

    npm update openai
    # 或使用yarn
    yarn upgrade openai
    
  • 显式断言类型:若升级后仍报错,可对content数组添加类型断言,强制匹配ResponseInputContent[]类型:
    修改后的代码片段:

    content: [
        {
            type: "input_text",
            text: "What is in this image?",
        },
        {
            type: "input_image",
            image_url: "https://openai-documentation.vercel.app/images/cat_and_otter.png",
        },
    ] as OpenAI.ResponseInputContent[],
    
  • 验证模型可用性:确认gpt-5为当前支持Vision功能的可用模型,若模型未正式开放,可替换为gpt-4o等已稳定支持多模态的模型进行测试。

内容的提问来源于stack exchange,提问作者James

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 03:13:13