Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"You uploaded an unsupported image" when trying to streamText with image data #1544

Closed
lucasishuman opened this issue May 9, 2024 · 8 comments

Comments

@lucasishuman
Copy link

Description

When I attempt to stream a text response from OpenAI and image data is present in the outgoing messages, I get an error. (see below for code sample)

Model is gpt-4-turbo

I'm confident it is a valid image format and under size limit.

I see the same behavior whether the image content is a URL or base64 encoded string.

Error:

web-app:dev:  ⨯ Error [AI_APICallError]: You uploaded an unsupported image. Please make sure your image is below 20 MB in size and is of one the following formats: ['png', 'jpeg', 'gif', 'webp'].
web-app:dev:     at eval (webpack-internal:///(rsc)/../../node_modules/@ai-sdk/provider-utils/dist/index.mjs:324:14)
web-app:dev:     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
web-app:dev:     at async postToApi (webpack-internal:///(rsc)/../../node_modules/@ai-sdk/provider-utils/dist/index.mjs:231:28)
web-app:dev:     at async OpenAIChatLanguageModel.doStream (webpack-internal:///(rsc)/../../node_modules/@ai-sdk/openai/dist/index.mjs:277:50)
web-app:dev:     at async _retryWithExponentialBackoff (webpack-internal:///(rsc)/../../node_modules/ai/dist/index.mjs:369:12)
web-app:dev:     at async streamText (webpack-internal:///(rsc)/../../node_modules/ai/dist/index.mjs:1432:45)
web-app:dev:     at async POST (webpack-internal:///(rsc)/./src/app/api/chat/route.ts:40:20)
web-app:dev:     at async eval (webpack-internal:///(rsc)/../../node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:238:37)
web-app:dev:     at async AppRouteRouteModule.execute (webpack-internal:///(rsc)/../../node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:153:26)
web-app:dev:     at async AppRouteRouteModule.handle (webpack-internal:///(rsc)/../../node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:300:30)

I'm using:

  • next: 14.2.3 (app router)
  • ai: 3.1.3
  • @ai-sdk/openai: 0.0.10

Code example

Example user messages with image data included

const messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "What’s in this image?"
            },
            {
                "type": "image",
                "image": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAgEASABIAAD/2wCEA..."
            }
        ]
    }
]
const messages = [
    {
        "role": "user",
        "content": [
            {
                "type": "text",
                "text": "What’s in this image?"
            },
            {
                "type": "image",
                "image": "https://static01.nyt.com/images/2024/05/12/business/12Roose-AIFriends..."
            }
        ]
    }
]

Local API route

import { StreamingTextResponse, streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

export const runtime = 'edge';

export async function POST(req: Request) {
  const {
    messages,
    model = 'gpt-4-turbo',
  } = await req.json();

  const result = await streamText({
    messages,
    model: openai(model),
  });

  return new StreamingTextResponse(result.toAIStream());
}

Server returns error

web-app:dev:  ⨯ Error [AI_APICallError]: You uploaded an unsupported image. Please make sure your image is below 20 MB in size and is of one the following formats: ['png', 'jpeg', 'gif', 'webp'].
web-app:dev:     at eval (webpack-internal:///(rsc)/../../node_modules/@ai-sdk/provider-utils/dist/index.mjs:324:14)
web-app:dev:     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
web-app:dev:     at async postToApi (webpack-internal:///(rsc)/../../node_modules/@ai-sdk/provider-utils/dist/index.mjs:231:28)
web-app:dev:     at async OpenAIChatLanguageModel.doStream (webpack-internal:///(rsc)/../../node_modules/@ai-sdk/openai/dist/index.mjs:277:50)
web-app:dev:     at async _retryWithExponentialBackoff (webpack-internal:///(rsc)/../../node_modules/ai/dist/index.mjs:369:12)
web-app:dev:     at async streamText (webpack-internal:///(rsc)/../../node_modules/ai/dist/index.mjs:1432:45)
web-app:dev:     at async POST (webpack-internal:///(rsc)/./src/app/api/chat/route.ts:40:20)
web-app:dev:     at async eval (webpack-internal:///(rsc)/../../node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:238:37)
web-app:dev:     at async AppRouteRouteModule.execute (webpack-internal:///(rsc)/../../node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:153:26)
web-app:dev:     at async AppRouteRouteModule.handle (webpack-internal:///(rsc)/../../node_modules/next/dist/esm/server/future/route-modules/app-route/module.js:300:30)

Additional context

No response

@lgrammel
Copy link
Collaborator

The error comes from the OpenAI API. Can you test whether you also receive it when using the OpenAI fetch API directly?

@lucasishuman
Copy link
Author

When I use the OpenAI SDK directly (as shown below) - I get a valid response from OpenAI.

Note - I need to change the message > content > image props to match OpenAI's message schema - maybe there is an issue mapping AI SDK image props to OpenAI image props in the AI SDK?

Using
"openai": "4.44.0"

Sent message (using OpenAI message schema)

    const messages = [
    {
      role: 'user',
      content: [
        {
          type: 'text',
          text: 'What is in this image',
        },
        {
          type: 'image_url',
          image_url: {
            url: 'https://static01.nyt.com/images/2024/05/12/.....',
          },
        },
      ],
    }
];

Route handler

import OpenAI from "openai";

export async function POST(req: Request) {
  const { messages } = await req.json();

  const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY,
  });

  const response = await openai.chat.completions.create({
    model = 'gpt-4-turbo',
    messages,
  });

  console.log(response.choices[0]);
}

I feel like I must be missing something very obvious if no one else sees this behavior - but I also don't see an example in the AI SDK for messages with image content.

My OPENAI_API_KEY is in my env file and is valid.

@lucasishuman
Copy link
Author

Maybe worth also noting is that client-side I am using the useChat hook, manually constructing the user message with image data/URL shown in original post, and then using append to send it to my app API.

@lucasishuman
Copy link
Author

This is related to #1555 because when I remove the data:image/jpeg;base64, from the beginning of my base64 encoded image data string, I get a valid response.

However, when I pass a URL I still get an error.

So part of the fix may be just a matter of providing better documentation for messages with image content, but there's still something funky when sending an image URL.

@lgrammel
Copy link
Collaborator

@lucasishuman can you share the img?

@lucasishuman
Copy link
Author

lucasishuman commented May 10, 2024

You can use any valid image URL to test, such as...

  const result = await streamText({
    messages: [
      {
        role: 'user',
        content: [
          {
            type: 'text',
            text: 'What’s in this image?',
          },
          {
            type: 'image',
            image:
              'https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg',
          },
        ],
      },
    ],
    model: openai('gpt-4-turbo'),
  });

@lgrammel
Copy link
Collaborator

@lucasishuman try using a URL

image: new URL('https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg')

See examples/ai-core/src/generate-text/openai-multimodal-url.ts for a full example

@lucasishuman
Copy link
Author

@lgrammel - Great, thanks. That was the missing info I needed.

I feel like adding the image data and image URL examples to the AI SDK docs would be helpful.

You can close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants