1

I followed the steps in the Genkit docs and deployed a Cloud function using the Gemini 1.5 Pro model. When I try to hit the function from a Flutter app however, I get this error:

Unhandled Exception: [firebase_functions/internal] [GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent: [400 Bad Request] API key not valid. Please pass a valid API key. [{"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"API_KEY_INVALID","domain":"googleapis.com","metadata":{"service":"generativelanguage.googleapis.com"}}]

I tried several ways to set the API key env variable.

I added it as an environment variable before deploying the cloud function:

export GOOGLE_GENAI_API_KEY=<your API key>

I also added my API key in the GCP Secret Manager and set it in a .env file in my functions folder. Nothing seems to fix this error.

Here's my function:

import { generate } from "@genkit-ai/ai";
import { configureGenkit } from "@genkit-ai/core";
import { noAuth, onFlow } from "@genkit-ai/firebase/functions";
import { gemini15Pro } from "@genkit-ai/googleai";
import * as z from "zod";
import { firebase } from "@genkit-ai/firebase";
import { googleAI } from "@genkit-ai/googleai";
import { defineSecret } from "firebase-functions/params";
defineSecret("GOOGLE_GENAI_API_KEY");

configureGenkit({
  plugins: [firebase(), googleAI({ apiVersion: "v1beta" })],
  logLevel: "debug",
  enableTracingAndMetrics: true,
});

const InventoryItem = z.object({
  name: z.string().describe("the name of the item"),
  description: z.string().describe("a brief description of the item"),
  value: z.number().describe("the monetary value of the item in bottlecaps"),
  funFact: z.string().optional().describe("a fun fact about the item"),
});

export const identifyItemFromUrl = onFlow(
  {
    name: "identifyItemFromUrl",
    inputSchema: z.string().url(),
    outputSchema: InventoryItem,
    authPolicy: noAuth(),
  },
  async (subject) => {
    const prompt = "Identify this item.";

    const llmResponse = await generate({
      model: gemini15Pro,
      prompt: [
        { text: prompt },
        {
          media: {
            url: "https://plus.unsplash.com/premium_photo-1699467558340-e04dd429a314?q=80&w=3870&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
            contentType: "image/jpeg",
          },
        },
      ],
      output: { schema: InventoryItem },
    });

    return llmResponse.output()!;
  }
);

2 Answers 2

2

It turns out the issue was related to the defineSecret method at the top of the file. When you use defineSecret, it creates a list instead of a single string.

enter image description here

Instead, you need to use defineString:

import { defineString } from "firebase-functions/params";
defineString("GOOGLE_GENAI_API_KEY");

You can also just remove the defineSecret/defineString code altogether. As long as the environment variables are defined in the .env file in your functions folder they will be detected when the function is deployed.

2
0

You need to run the below code as in terminal

export GOOGLE_GENAI_API_KEY=<your API key>

Make sure you put your api key inside single quotes. eg. 'ABCHDHDUFAUD-DASIOUFAO'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.