Home / Function/ processEmbeddingBatch() — supabase Function Reference

processEmbeddingBatch() — supabase Function Reference

Architecture documentation for the processEmbeddingBatch() function in generate-embeddings.ts from the supabase codebase.

Entity Profile

Dependency Diagram

graph TD
  b6da1e2f_3534_5430_6fdb_1a60bda504b3["processEmbeddingBatch()"]
  e004e86e_d6b3_597d_5a6c_1a898d503de3["processAndInsertEmbeddings()"]
  e004e86e_d6b3_597d_5a6c_1a898d503de3 -->|calls| b6da1e2f_3534_5430_6fdb_1a60bda504b3
  999b9bcb_cbf3_994c_468b_34a61e74d416["withRetry()"]
  b6da1e2f_3534_5430_6fdb_1a60bda504b3 -->|calls| 999b9bcb_cbf3_994c_468b_34a61e74d416
  7e95d843_ee70_bdbc_e71b_eeaab259c8a0["mapEmbeddingsToSections()"]
  b6da1e2f_3534_5430_6fdb_1a60bda504b3 -->|calls| 7e95d843_ee70_bdbc_e71b_eeaab259c8a0
  fc27864f_26ae_78ff_8b98_6c9041e3be13["logFailedSections()"]
  b6da1e2f_3534_5430_6fdb_1a60bda504b3 -->|calls| fc27864f_26ae_78ff_8b98_6c9041e3be13
  style b6da1e2f_3534_5430_6fdb_1a60bda504b3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

apps/docs/scripts/search/generate-embeddings.ts lines 324–392

async function processEmbeddingBatch(
  openai: OpenAI,
  batch: PageSectionForEmbedding[],
  batchIndex: number,
  totalBatches: number
): Promise<BatchEmbeddingResult> {
  const inputs = batch.map((section) => section.input)
  console.log(
    `Processing embedding batch ${batchIndex + 1}/${totalBatches} (${inputs.length} sections)`
  )

  // Helper to identify context length exceeded errors from OpenAI
  const isContextLengthError = (err: unknown) => {
    if (!(err instanceof OpenAI.APIError)) return false

    const message = err.error?.message as string
    const status = err.status
    return status === 400 && message.toLowerCase().includes('context')
  }

  let embeddingResponse: OpenAI.Embeddings.CreateEmbeddingResponse
  try {
    embeddingResponse = await withRetry(
      () =>
        openai.embeddings.create({
          model: CONFIG.EMBEDDING_MODEL,
          input: inputs,
        }),
      CONFIG.OPENAI_MAX_RETRIES,
      CONFIG.OPENAI_BASE_DELAY_MS,
      `OpenAI embedding batch ${batchIndex + 1}`,
      (err) => !isContextLengthError(err)
    )
  } catch (err) {
    if (!isContextLengthError(err)) {
      throw err
    }

    // Context length exceeded: truncate problematic sections and try once more
    const limit = CONFIG.EMBEDDING_TRUNCATE_CHAR_LIMIT
    const truncatedInputs = inputs.map((s) => (s.length > limit ? s.slice(0, limit) : s))
    const truncatedCount = truncatedInputs.filter((s, i) => s !== inputs[i]).length
    console.warn(
      `OpenAI embedding batch ${batchIndex + 1}: context length exceeded. ` +
        `Truncating ${truncatedCount} overly long section(s) to ${limit} chars and retrying once.`
    )

    embeddingResponse = await openai.embeddings.create({
      model: CONFIG.EMBEDDING_MODEL,
      input: truncatedInputs,
    })

    // Replace inputs with truncated inputs for downstream bookkeeping
    for (let i = 0; i < inputs.length; i++) inputs[i] = truncatedInputs[i]
  }

  const { sectionsWithEmbeddings, failedSectionIndexes } = mapEmbeddingsToSections(
    batch,
    embeddingResponse.data,
    batchIndex
  )
  logFailedSections(batch, inputs, failedSectionIndexes)

  return {
    sectionsWithEmbeddings,
    failedSectionIndexes,
    processedCount: inputs.length,
  }
}

Subdomains

Frequently Asked Questions

What does processEmbeddingBatch() do?
processEmbeddingBatch() is a function in the supabase codebase.
What does processEmbeddingBatch() call?
processEmbeddingBatch() calls 3 function(s): logFailedSections, mapEmbeddingsToSections, withRetry.
What calls processEmbeddingBatch()?
processEmbeddingBatch() is called by 1 function(s): processAndInsertEmbeddings.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free