Skip to content

Poor performance when using Gemini image output with streamText #5325

@wong2

Description

@wong2

Description

I tried using the Gemini image output, but found that when used with streamText, the response was very slow (tens of seconds to 1 minute).

To figure out where the issue was, I tried using generateText, and this time I got the result quickly (within seconds).

I also tried to call the API (/v1beta/models/gemini-2.0-flash-exp:streamGenerateContent?alt=sse) with curl directly, which also response very fast. So I guess there's something happening inside streamText

Here's the minimal test code:

Code example

import { google } from '@ai-sdk/google'
import { generateText, streamText } from 'ai'

const model = google('gemini-2.0-flash-exp')

const result = await generateText({
  model,
  providerOptions: {
    google: { responseModalities: ['TEXT', 'IMAGE'] },
  },
  prompt: 'Generate an image of a black cat',
})

console.log('non stream result', result)

const streamResult = streamText({
  model,
  providerOptions: {
    google: { responseModalities: ['TEXT', 'IMAGE'] },
  },
  prompt: 'Generate an image of a black cat',
})

for await (const chunk of streamResult.fullStream) {
  console.log(chunk)
}

AI provider

@ai-sdk/google: 1.2.1

Additional context

Node.js 23

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai/providerrelated to a provider package. Must be assigned together with at least one `provider/*` labelbugSomething isn't working as documentedprovider/googleIssues related to the @ai-sdk/google provider

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions