Update Gemini embedding API from v1 to v1beta with correct model name#162
Update Gemini embedding API from v1 to v1beta with correct model name#162gamal1osama wants to merge 2 commits intoAOSSIE-Org:mainfrom
Conversation
📝 WalkthroughWalkthroughThe PR updates two Supabase Edge Functions to use the correct Gemini embedding endpoint and model: switching from Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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 | 🟠 MajorGuard against missing
GEMINI_API_KEYbefore making the API call.If
GEMINI_API_KEYis 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 | 🟡 MinorError 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}`); }
|
I've fixed the embedding generation issue in this PR. Could you please review when you have a moment? |
There was a problem hiding this comment.
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 | 🟡 MinorNo 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.valuesthrows aTypeErrorwith 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_URLandSUPABASE_SERVICE_ROLE_KEYare 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, { |
There was a problem hiding this comment.
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.
| 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.
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-embeddingfunction 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 camelCasemeetingId. It also adds a testing guide for the backend functions.🔧 Changes Made
https://generativelanguage.googleapis.com/v1beta/models/gemini-embedding-001:embedContentand parsesembedding.values.GEMINI_API_KEYis loaded.How to test (quick examples)
Expected response: JSON containing an
embeddingarray of numeric values, e.g.{"embedding":[...numbers...]}.📷 Screenshots or Visual Changes (if applicable)
✅ Checklist
Summary by CodeRabbit