SupabaseS
Supabase6mo ago
M

Type error when storing embeddings with OpenAI

I get type errors when attempting to store OpenAI embeddings via the JS sdk.

I ran this code to enable embedding storage:
ALTER TABLE table_name
ADD COLUMN openai_embedding vector(3072);


Then I attempt to to update the column with the embedding:

import OpenAI from "openai";
import { createClient } from "@/utils/supabase/server";

const generateEmbedding = async (input: string, id: number) => {
  const supabase = await createClient();
  const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY,
  });

  const response = await openai.embeddings.create({
    model: "text-embedding-3-large",
    input: input,
  });

  // embedding is a number[]
  const embedding = response.data[0].embedding;

  const { data, error } = await supabase
    .from("table_name")
    // openai_embedding is typed as a string
    .update({ openai_embedding: embedding })
    .eq("id", id);
};


The issue is that embedding ends up being of type number[], but openai_embedding is expects type string. I get the error Type 'number[]' is not assignable to type 'string'.
Was this page helpful?