Gemini Completion - how to save image from response

I have Gemini Completion step and it returns a generated image response in form of string:

[
    {
        "parts": [
            {
                "inlineData": {
                    "mimeType": "image/png",
                    "data": "IMTString(1583820): long_string_here
                }
            }
        ]
    }
]

How to save that response to Google Drive?
I’ve tried to pass it as is and {{base64(27.parts.inlineData.data)}}
But the generated image is always broken.

try to strip out the “IMTString(1583820):” with like

RegEx: `^IMTString\d+:`

I think the rest will contain the valid base64 string.

No, the correct way to make it work is to convert it to binary:

{{toBinary(27.parts.inlineData.data; “base64”)}}

This is the only working solution!

The answer was in Gemini Docs:

for (const part of response.candidates[0].content.parts) {
    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");
    }
  }
2 Likes