rohitg00/agentmemory · error · Error

OpenAI embedding failed (${response.status}): ${err}

Error message

OpenAI embedding failed (${response.status}): ${err}

What it means

The OpenAI embedding endpoint (/v1/embeddings) returned a non-2xx HTTP status. embedBatch reads the raw response body into `err` and throws it with the status code, surfacing provider-side rejections such as auth failures, bad model names, or rate limits.

Source

Thrown at src/providers/embedding/openai.ts:114

  async embedBatch(texts: string[]): Promise<Float32Array[]> {
    const url = buildEmbeddingUrl(
      this.baseUrl,
      this.isAzure,
      this.azureApiVersion,
    );
    const response = await fetchWithTimeout(url, {
      method: "POST",
      headers: buildAuthHeaders(this.apiKey, this.isAzure),
      body: JSON.stringify({
        model: this.model,
        input: texts,
      }),
    });

    if (!response.ok) {
      const err = await response.text();
      throw new Error(`OpenAI embedding failed (${response.status}): ${err}`);
    }

    const data = (await response.json()) as {
      data: Array<{ embedding: number[] }>;
    };

    return data.data.map((d) => new Float32Array(d.embedding));
  }
}

View on GitHub (pinned to e04ba88819)

Solutions

  1. Read the status and body in the message: 401 -> fix OPENAI_API_KEY; 404 -> fix model name; 429 -> add backoff/retry and reduce batch size
  2. Verify the model id against current OpenAI docs and your project's access list
  3. Reduce input batch size and total tokens per request
  4. Check OPENAI_EMBEDDING_BASE_URL — ensure the endpoint actually serves /embeddings

Example fix

// before: bulk embed hits 429
await provider.embedBatch(thousandTexts);
// after: bounded batches + retry on 429
for (const chunk of chunkArray(texts, 100)) {
  await withRetry(() => provider.embedBatch(chunk), { retries: 3, on: 429 });
}
Defensive patterns

Strategy: retry

Validate before calling

if (!process.env.OPENAI_API_KEY && !process.env.OPENAI_EMBEDDING_API_KEY) throw new Error('OpenAI key missing');
if (!/^text-embedding-\d+/.test(model)) console.warn(`Suspicious embedding model id: ${model}`);

Type guard

const isOk = (r: Response): r is Response & { ok: true } => r.ok;

Try / catch

try {
  return await provider.embedBatch(texts);
} catch (err) {
  const msg = String((err as Error).message);
  const m = msg.match(/OpenAI embedding failed \((\d+)\)/);
  if (m) {
    const status = Number(m[1]);
    if (status === 429 || status >= 500) return withBackoff(() => provider.embedBatch(texts));
    if (status === 401) throw new Error('Fix OPENAI_API_KEY');
    if (status === 404) throw new Error(`Unknown embedding model: ${model}`);
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling embed()/embedBatch() when the POST to the embeddings endpoint fails: 401 invalid key, 404 unknown model, 429 quota/rate limit, 400 malformed input (empty array, text too long), 5xx OpenAI outage.

Common situations: Revoked or wrong-project API key; model name typo (text-embedding-3-small vs -001 legacy); exceeding tokens-per-minute limits during bulk embedding; base URL pointing to a proxy that doesn't implement /embeddings; org blocked for region.

Related errors


AI-assisted analysis of rohitg00/agentmemory@e04ba88819 (2026-08-30). Data as JSON: /api/errors/9aa072b77f33d700. Report an issue: GitHub.