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

使用GPT-4-Turbo调用image_url报400错误,求解决方法

解决OpenAI图像转文本的BadRequestError问题

出现BadRequestError: 400 Invalid content type. image_url is only supported by certain models错误的核心原因是:你使用的gpt-4-turbo模型不支持图像输入,只有OpenAI的特定多模态模型才具备处理image_url类型内容的能力。

解决步骤:

  • 更换为支持图像分析的多模态模型,推荐使用gpt-4o(最新通用多模态模型)或gpt-4-turbo-vision-preview(专门的视觉模型)。
  • 修改代码中的model参数,替换为对应的模型ID。

修改后的代码示例:

app.post('/analyze-image', cors(corsOptions), async (req, res) => {
  try {
    const { image } = req.body;
    const base64Image = await convertBufferToBase64(image);
    // Pass base64Image to OpenAI API
    const response = await openai.chat.completions.create({
      model: "gpt-4o", // 替换为支持图像的模型
      messages: [
        {
          role: "user",
          content: [
            { type: "text", text: "What's in this image?" },
            { type: "image_url", url: `data:image/jpeg;base64,${base64Image}` },
          ],
        },
      ],
    });
    res.json(response.choices[0]);
  } catch (error) {
    console.error(error);
    res.status(500).send('Error analyzing image');
  }
});

额外说明:

  • 若使用图片URL而非base64,确保URL是公开可访问的(OpenAI服务器需要能抓取到该图片),但核心前提还是必须使用支持图像的模型。
  • OpenAI的纯文本模型(如gpt-4-turbo、gpt-3.5-turbo)均不支持图像输入,只有标注了“Vision”或“o”(代表omni,全模态)的模型才具备视觉分析能力。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 05:42:13