Skip to content

Returning texts and images from latest gemini-2.0-flash-exp-image-generation #5302

Closed
@wx0165927473

Description

@wx0165927473

Feature Description

Hi there, in Google's new gemini-2.0-flash-exp-image-generation release, it can return both texts and images like this

    const response = await model.generateContent(contents);
    for (const part of  response.response.candidates[0].content.parts) {
      // Based on the part type, either show the text or save the image
      if (part.text) {
        console.log(part.text);
      } else if (part.inlineData) {
        const imageData = part.inlineData.data;
        const buffer = Buffer.from(imageData, 'base64');
        fs.writeFileSync('gemini-native-image.png', buffer);
        console.log('Image saved as gemini-native-image.png');
      }
    }

However, in generateText function, it can only fetch each image. by looping through like this

const result = await generateText({
          model: google('gemini-2.0-flash-exp-image-generation'),
          providerOptions: {
            google: { responseModalities: ['TEXT', 'IMAGE'] },
          },
          prompt: prompt,
        });

        for (const file of result.files) {
          if (file.mimeType.startsWith('image/')) {

          }
        } 

Something like this would be great:


        for (const content of result.contents) {
          if (content.mimeType.startsWith('image/')) {
             // each image
          }
          content.text // each corresponding text
        } 

Use Cases

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions