google-gemini

Why does my code send 429 Quota Exceeds on Google Gemini API with generative UI?


This is my first trying the new Vercel's generative UI with AI SDK, I am using Google's Gemini AI with the gemini-1.5-pro-latest model, It worked flawlessly on my local but when deployed it returns this response:

responseBody: '{\n' +
'  "error": {\n' +
'    "code": 429,\n' +
'    "message": "Resource has been exhausted (e.g. check quota).",\n' +
'    "status": "RESOURCE_EXHAUSTED"\n' +
'  }\n' +
'}\n',

I am using Next 14 with Typescript, so the function is on server side (copied from Vercel's documentation), here is the function to call the SDK:

export async function continueConversation(
  input: string
): Promise<ClientMessage> {
  "use server";

  const history = getMutableAIState();

  const result = await streamUI({
    model: google("models/gemini-1.5-pro-latest"),
    system: `
      You are a general purpose assistant, you can help the user with a variety of tasks. You can tell jokes, give place and song recommendations, and much more. You are a professional, don't use emote.
      `,
    messages: [...history.get(), { role: "user", content: input }],
    text: ({ content, done }) => {
      if (done) {
        history.done((messages: ServerMessage[]) => [
          ...messages,
          { role: "assistant", content },
        ]);
      }
      return (
        <article className="markdown-container">
          <Markdown remarkPlugins={[remarkGfm]}>{content}</Markdown>
        </article>
      );
    },
    tools: {
      getJoke: {
        description:
          "A tool when the user wants a joke. The joke should make the user laugh.",
        parameters: z.object({
          category: z.string().optional().describe("the category of the joke"),
        }),
        generate: async function* ({ category }) {
          yield <LoaderCircle />;
          const joke = await generateObject({
            model: google("models/gemini-1.5-pro-latest"),
            schema: jokeSchema,
            prompt:
              "Generate a joke that will make the user laugh. The joke should be in the category of " +
              category +
              ". If no category is provided, ask the user for a category.",
          });
          return <JokeComponent joke={joke.object} />;
        },
      },
      getPlaces: {
        description:
          "A tool when the user wants place recommendations based on the location and type.",
        parameters: z.object({
          location: z.string().describe("the user's location"),
          type: z.string().optional().describe("the type of place"),
        }),
        generate: async function* ({ location, type }) {
          yield <LoaderCircle className="loader-circle" />;
          const places = await generateObject({
            model: google("models/gemini-1.5-pro-latest"),
            schema: placeSchema,
            prompt:
              "Generate an array of places to visit in " +
              location +
              " with the type of " +
              (type || "any type") +
              ". The array should contain at least 5 places.",
          });
          if (places && places.object && Array.isArray(places.object)) {
            return <PlaceComponent place={places.object} />;
          } else {
            return <p>Something went wrong, please try again later.</p>;
          }
        },
      },
      getSongs: {
        description:
          "A tool when the user wants song recommendations based on the genre.",
        parameters: z.object({
          genre: z.string().optional().describe("the genre of the song"),
          singer: z.string().optional().describe("the singer of the song"),
        }),
        generate: async function* ({ genre, singer }) {
          yield <LoaderCircle />;
          const songs = await generateObject({
            model: google("models/gemini-1.5-pro-latest"),
            schema: songSchema,
            prompt:
              "Generate songs recommendation in the genre of " +
              (genre || "any genres") +
              "or by the singer " +
              (singer || "any singer") +
              ". Return an array of 3 songs.",
          });
          if (songs && songs.object && Array.isArray(songs.object)) {
            return <SongComponent song={songs.object} />;
          } else {
            return <p>Something went wrong, please try again later.</p>;
          }
        },
      },
    },
    
  });

  return {
    id: nanoid(),
    role: "assistant",
    display: result.value,
  };
}

Here is the full actions.tsx and page.tsx:

I tried to change the API key with new accounts to no avail, the thing is, it still works on my local :< Any suggestions will be very appreciated, thanks!


Solution

  • If you are seeing a 429 quota error, then you have run out of quota.

    You can see the default quota here: https://ai.google.dev/pricing (make sure you choose the model you are using).

    As you are using gemini-1.5-pro-latest, and I assume you're using the free tier, your quota is 2 requests per minute. If you try a 2-turn request locally, and then deploy the same API key to another environment and run it within a minute, you won't have any remaining quota.

    Try waiting a minute and see if the error persists. Otherwise try using a model with more quota, like gemini-1.5-flash-latest.