Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google model content caching stopped working since yesterday #4664

Open
dheerajsarwaiya opened this issue Feb 2, 2025 · 0 comments
Open
Labels
bug Something isn't working

Comments

@dheerajsarwaiya
Copy link

Description

I started getting this error since yesterday's afternoon. Before that it was working fine. Do I need to change model or update some setting?

[GoogleGenerativeAI Error]: Error fetching from https://generativelanguage.googleapis.com/v1beta/cachedContents: [503 Service Unavailable] The service is currently unavailable.. The service may be experiencing high load or maintenance. Please try again later

Code example

import { google } from "@ai-sdk/google";
import {
GoogleAICacheManager,
GoogleAIFileManager,
} from "@google/generative-ai/server";
import { generateObject, generateText } from "ai";

const cacheManager = new GoogleAICacheManager(
process.env.GOOGLE_GENERATIVE_AI_API_KEY
);

const fileManager = new GoogleAIFileManager(
process.env.GOOGLE_GENERATIVE_AI_API_KEY
);

const model: GoogleModelCacheableId = "models/gemini-1.5-flash-001";

export async function createCacheableText(sourceUrl: string): Promise {
const maxRetries = 3;
const baseDelay = 1000; // 1 second

let lastError: Error | null = null;

for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
const uploadResult = await fileManager.uploadFile(sourceUrl, {
mimeType: "application/pdf",
});

  const { name: cachedContent } = await cacheManager.create({
    model,
    contents: [
      {
        role: "user",
        parts: [
          {
            fileData: {
              fileUri: uploadResult.file.uri,
              mimeType: uploadResult.file.mimeType,
            },
          },
        ],
      },
    ],
    ttlSeconds: 60 * 60,
  });

  if (!cachedContent) {
    throw new Error("Failed to cache text - received empty response");
  }

  return cachedContent;
} catch (error: unknown) {
  // Type guard for Error objects
  if (!(error instanceof Error)) {
    throw new Error("An unknown error occurred");
  }

  lastError = error;

  // Check if it's a 503 error
  if (error.message.includes("503 Service Unavailable")) {
    console.warn(
      `Attempt ${attempt + 1}/${maxRetries} failed with 503 error`
    );

    // If we haven't exhausted all retries, wait and try again
    if (attempt < maxRetries - 1) {
      const delay = baseDelay * Math.pow(2, attempt); // Exponential backoff
      console.log(`Retrying in ${delay / 1000} seconds...`);
      await new Promise((resolve) => setTimeout(resolve, delay));
      continue;
    }
  } else {
    // If it's not a 503 error, throw immediately
    throw error;
  }
}

}

// If we've exhausted all retries
throw new Error(
Failed to cache text after ${maxRetries} attempts. Last error: ${lastError?.message}. +
The service may be experiencing high load or maintenance. Please try again later.
);
}

AI provider

"@ai-sdk/google": "^1.1.8",

Additional context

This was working fine earlier. I used it at least 100 times. But then it started throwing this error. I checked my usage, everything was fine.

@dheerajsarwaiya dheerajsarwaiya added the bug Something isn't working label Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant