Skip to content

Update Gemini embedding API from v1 to v1beta with correct model name#162

Open
gamal1osama wants to merge 2 commits intoAOSSIE-Org:mainfrom
gamal1osama:fix/get-embedding-gemini-api
Open

Update Gemini embedding API from v1 to v1beta with correct model name#162
gamal1osama wants to merge 2 commits intoAOSSIE-Org:mainfrom
gamal1osama:fix/get-embedding-gemini-api

Conversation

@gamal1osama
Copy link

@gamal1osama gamal1osama commented Feb 9, 2026

Closes #161

📝 Description

This PR fixes a runtime error in the embedding Edge Function caused by using the wrong Gemini API version and model. The get-embedding function previously attempted to call a v1 embedding model (embedding-001) which is not available; this caused embedding generation to fail. The PR updates the function to call the correct v1beta endpoint and model, improves error handling/logging, and makes the summarizer accept camelCase meetingId. It also adds a testing guide for the backend functions.

🔧 Changes Made

  • Updated embedding implementation to use the Gemini v1beta embedding endpoint and model: index.ts
    • Calls https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent and parses embedding.values.
    • Improves error messages when the API responds with an error and logs whether GEMINI_API_KEY is loaded.

How to test (quick examples)

curl -X POST "https://your-project.supabase.co/functions/v1/get-embedding" \
  -H "Authorization: Bearer YOUR_ANON_KEY" \
  -H "Content-Type: application/json" \
  -d '{"text": "Test meeting about AI"}' | jq

Expected response: JSON containing an embedding array of numeric values, e.g. {"embedding":[...numbers...]}.

📷 Screenshots or Visual Changes (if applicable)

Screenshot from 2026-02-09 05-26-55

✅ Checklist

  • I have read the contributing guidelines.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added necessary documentation (if applicable).
  • Any dependent changes have been merged and published in downstream modules.

Summary by CodeRabbit

  • Chores
    • Updated embedding service to use a newer API endpoint and simplified the request payload.
    • Adjusted related error handling; no change to end-user features, with improved reliability and maintainability.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 9, 2026

📝 Walkthrough

Walkthrough

The PR updates two Supabase Edge Functions to use the correct Gemini embedding endpoint and model: switching from /v1/models/embedding-001:embedContent to /v1beta/models/gemini-embedding-001:embedContent, and removing redundant model and taskType fields from the request payloads.

Changes

Cohort / File(s) Summary
Embedding endpoint updates
supabase/functions/get-embedding/index.ts, supabase/functions/generate-embeddings/index.ts
Replaced /v1/models/embedding-001:embedContent with /v1beta/models/gemini-embedding-001:embedContent. Removed model and taskType fields from request bodies; retained content.parts payload structure. Inline comments updated to reference Gemini embedding model.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I hopped to the endpoint, found a name anew,

gemini-embedding-001 now pulls data through.
No stray fields, no missing keys, embeddings sing—
A tiny hop fixed the whole searching thing. 🌱

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed The PR successfully addresses all coding requirements from issue #161: updates API from v1 to v1beta, changes model from embedding-001 to gemini-embedding-001, removes redundant model/taskType fields, and improves error handling/logging.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the Gemini embedding API issue in both get-embedding and generate-embeddings functions as specified in issue #161.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Title check ✅ Passed The title accurately and concisely describes the main change: updating Gemini embedding API from v1 to v1beta with the correct model name, which directly aligns with the core fix across both modified files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
supabase/functions/get-embedding/index.ts (2)

10-12: ⚠️ Potential issue | 🟠 Major

Guard against missing GEMINI_API_KEY before making the API call.

If GEMINI_API_KEY is not set, the fetch URL will contain the literal string "undefined" as the key, resulting in a confusing Google API auth error. Return a clear 500 early instead.

Proposed fix
 const GEMINI_API_KEY = Deno.env.get("GEMINI_API_KEY");
 
 console.log("GEMINI_API_KEY:", GEMINI_API_KEY ? "Loaded" : "Missing");
+
+if (!GEMINI_API_KEY) {
+  console.error("GEMINI_API_KEY is not configured");
+}

Then inside the handler, before the fetch:

if (!GEMINI_API_KEY) {
  return new Response(
    JSON.stringify({ error: "Server misconfiguration: embedding API key not set" }),
    { status: 500, headers: { ...corsHeaders, "Content-Type": "application/json" } }
  );
}

43-46: ⚠️ Potential issue | 🟡 Minor

Error response body may not be valid JSON, causing a masked exception.

If the Gemini API returns a non-JSON error (e.g., HTML 503 page), embeddingResponse.json() will throw, and the catch block will surface a generic parse error instead of the actual HTTP status/reason.

Proposed fix — fall back to text
     if (!embeddingResponse.ok) {
-      const error = await embeddingResponse.json();
-      throw new Error(`Error generating embedding: ${error.error?.message || "Unknown error"}`);
+      let message: string;
+      try {
+        const error = await embeddingResponse.json();
+        message = error.error?.message || "Unknown error";
+      } catch {
+        message = await embeddingResponse.text();
+      }
+      throw new Error(`Gemini API error (${embeddingResponse.status}): ${message}`);
     }

@gamal1osama
Copy link
Author

Hi @jddeep @SharkyBytes

I've fixed the embedding generation issue in this PR. Could you please review when you have a moment?

@gamal1osama gamal1osama changed the title fix the get embedding gemini api problem Update Gemini embedding API from v1 to v1beta with correct model name fixing #161 Feb 9, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
supabase/functions/generate-embeddings/index.ts (1)

73-74: ⚠️ Potential issue | 🟡 Minor

No null-safety on embeddingData.embedding.values.

If the Gemini response shape ever changes or returns an error object that still has a 200 status, accessing .embedding.values throws a TypeError with no actionable message. A quick guard would surface a clearer error.

Proposed fix
     const embeddingData = await embeddingResponse.json();
-    const embedding = embeddingData.embedding.values;
+    const embedding = embeddingData?.embedding?.values;
+    if (!embedding) {
+      throw new Error(`Unexpected embedding response structure: ${JSON.stringify(embeddingData).slice(0, 200)}`);
+    }
🤖 Fix all issues with AI agents
In `@supabase/functions/generate-embeddings/index.ts`:
- Line 52: The code performs the fetch using GEMINI_API_KEY directly (see the
fetch call that builds the URL with "?key=" + GEMINI_API_KEY); add a pre-flight
check that verifies GEMINI_API_KEY is defined and non-empty (e.g., at the start
of the function handling embeddings or before the fetch), and if missing throw
or return an explicit error/HTTP 500 with a clear message like "Missing
GEMINI_API_KEY" so the request never proceeds with key=undefined; update any
surrounding error handling in the embedding generation function to surface this
failure immediately.
🧹 Nitpick comments (1)
supabase/functions/generate-embeddings/index.ts (1)

32-35: Re-reads env vars instead of using the constants already defined at lines 16–17.

SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY are already captured at module scope. Using them directly is consistent and avoids redundant lookups.

Proposed fix
     const supabaseClient = createClient(
-      Deno.env.get("SUPABASE_URL") ?? "",
-      Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") ?? "",
+      SUPABASE_URL ?? "",
+      SUPABASE_SERVICE_ROLE_KEY ?? "",
     );


// Generate embedding using Gemini
const embeddingResponse = await fetch("https://generativelanguage.googleapis.com/v1/models/embedding-001:embedContent?key=" + GEMINI_API_KEY, {
const embeddingResponse = await fetch("https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent?key=" + GEMINI_API_KEY, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

GEMINI_API_KEY is interpolated without a guard — requests silently go out with key=undefined.

If the env var is missing, GEMINI_API_KEY is undefined and string concatenation produces the literal query string ?key=undefined. The request reaches Google, fails with an opaque auth error, and the real cause (missing secret) is hidden. Fail fast before the fetch.

Proposed fix
+    if (!GEMINI_API_KEY) {
+      throw new Error("GEMINI_API_KEY is not configured");
+    }
+
     const embeddingResponse = await fetch(
-      "https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent?key=" + GEMINI_API_KEY,
+      `https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent?key=${GEMINI_API_KEY}`,
       {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const embeddingResponse = await fetch("https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent?key=" + GEMINI_API_KEY, {
if (!GEMINI_API_KEY) {
throw new Error("GEMINI_API_KEY is not configured");
}
const embeddingResponse = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContent?key=${GEMINI_API_KEY}`,
{
🤖 Prompt for AI Agents
In `@supabase/functions/generate-embeddings/index.ts` at line 52, The code
performs the fetch using GEMINI_API_KEY directly (see the fetch call that builds
the URL with "?key=" + GEMINI_API_KEY); add a pre-flight check that verifies
GEMINI_API_KEY is defined and non-empty (e.g., at the start of the function
handling embeddings or before the fetch), and if missing throw or return an
explicit error/HTTP 500 with a clear message like "Missing GEMINI_API_KEY" so
the request never proceeds with key=undefined; update any surrounding error
handling in the embedding generation function to surface this failure
immediately.

@gamal1osama gamal1osama changed the title Update Gemini embedding API from v1 to v1beta with correct model name fixing #161 Update Gemini embedding API from v1 to v1beta with correct model name Feb 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: embeddings generation error "models/embedding-001 is not found"

1 participant