Repository navigation
Closed
Description
Issue draft
Title:
OPENAI_EMBEDDING_API_KEY is ignored when EMBEDDING_PROVIDER=openai and OPENAI_API_KEY is set
Body:
Summary
When EMBEDDING_PROVIDER=openai is used with both:
OPENAI_API_KEYfor an OpenAI-compatible chat providerOPENAI_EMBEDDING_API_KEY/OPENAI_EMBEDDING_BASE_URLfor a separate local OpenAI-compatible embedding endpoint
the embedding provider still uses OPENAI_API_KEY, not OPENAI_EMBEDDING_API_KEY.
This makes it impossible to use a hosted OpenAI-compatible LLM while routing embeddings to a separate local endpoint such as LM Studio, even though OpenAIEmbeddingProvider documents and implements embedding-specific env vars.
Reproduction
Example config:
# Hosted OpenAI-compatible chat provider
OPENAI_API_KEY=hosted-chat-key
OPENAI_BASE_URL=https://example-chat-provider/v1
OPENAI_MODEL=some-chat-model
# Local OpenAI-compatible embedding provider
EMBEDDING_PROVIDER=openai
OPENAI_EMBEDDING_API_KEY=local-embedding-key
OPENAI_EMBEDDING_BASE_URL=http://127.0.0.1:1234/v1
OPENAI_EMBEDDING_MODEL=text-embedding-bge-m3
OPENAI_EMBEDDING_DIMENSIONS=1024
With this configuration, agentmemory sends hosted-chat-key to the local embedding endpoint. LM Studio rejects it with an invalid token error.
Expected behavior
For embeddings, OPENAI_EMBEDDING_API_KEY should take precedence over OPENAI_API_KEY, and OPENAI_EMBEDDING_BASE_URL should route embeddings independently from chat completions.
Actual behavior
createEmbeddingProvider() calls:
new OpenAIEmbeddingProvider(getEnvVar("OPENAI_API_KEY")!)
Because a constructor argument is passed, the provider's internal fallback:
apiKey || getEnvVar("OPENAI_EMBEDDING_API_KEY") || getEnvVar("OPENAI_API_KEY")
never reaches OPENAI_EMBEDDING_API_KEY.
Root cause
src/providers/embedding/index.ts passes OPENAI_API_KEY into OpenAIEmbeddingProvider for the openai embedding provider case.
Suggested fix
Let OpenAIEmbeddingProvider resolve its own env vars:
case "openai":
return withDimensionGuard(new OpenAIEmbeddingProvider());
This keeps the documented precedence inside OpenAIEmbeddingProvider:
constructor override, if explicitly provided
OPENAI_EMBEDDING_API_KEY
OPENAI_API_KEY
Notes
This is especially useful for setups where:
chat/compression uses a hosted OpenAI-compatible provider
embeddings use local LM Studio / vLLM / Ollama / llama.cpp
the two endpoints require different API keys
## PR patch
```diff
diff --git a/src/providers/embedding/index.ts b/src/providers/embedding/index.ts
index 0000000..0000000 100644
--- a/src/providers/embedding/index.ts
+++ b/src/providers/embedding/index.ts
@@
- case "openai": return withDimensionGuard(new OpenAIEmbeddingProvider(getEnvVar("OPENAI_API_KEY")!));
+ case "openai": return withDimensionGuard(new OpenAIEmbeddingProvider());
PR description draft
Title:
Respect OPENAI_EMBEDDING_API_KEY for OpenAI embedding provider
Body:
### What changed
This updates the OpenAI embedding provider factory to let `OpenAIEmbeddingProvider` resolve its own API key from the documented precedence:
1. explicit constructor argument
2. `OPENAI_EMBEDDING_API_KEY`
3. `OPENAI_API_KEY`
### Why
The current factory passes `OPENAI_API_KEY` directly:
```ts
new OpenAIEmbeddingProvider(getEnvVar("OPENAI_API_KEY")!)
That prevents OPENAI_EMBEDDING_API_KEY from being used when users configure:
OPENAI_API_KEY / OPENAI_BASE_URL for hosted OpenAI-compatible chat
OPENAI_EMBEDDING_API_KEY / OPENAI_EMBEDDING_BASE_URL for local embeddings
The provider already supports this split internally, so the factory should not override it.
Validation
The fix is behaviorally scoped to the OpenAI embedding provider construction path.
Existing configs that only set OPENAI_API_KEY continue to work because OpenAIEmbeddingProvider falls back to OPENAI_API_KEY.
Metadata
Metadata
Assignees
Labels
No labels
Activity
skywalker-35 commentedon Jun 12, 2026
Can confirm this issue — hit it hard today while migrating from Zhipu embedding-3 to SiliconFlow BGE-M3.
Our setup
Symptoms
smart-searchreturned results but scores were stuck at1/rankpattern (~0.016) — indicating pure BM25, no vector searchOpenAI embedding failed (401): "Api key is invalid"— DeepSeek key being sent to SiliconFlowcircuit_breaker_opencascaded to kill all LLM compression because we hadn't setOPENAI_API_KEYin.envyet (was injecting via process env)Temporary fix (confirmed working)
Patched the dist bundle directly:
After this, Chinese smart-search went from 0 → 5 results, LLM compression recovered, no more 401s.
Additional silent-failure concern
The
catch {}block aroundthis.embeddingProvider.embed(query)intripleStreamSearchsilently swallows these 401 errors. Users see zero/empty results with no indication that their embedding config is broken.agentmemory doctoralso doesn't catch this — it only tests connectivity, not actual embedding calls.Would be great to see the proposed fix merged (passing no arg to
OpenAIEmbeddingProviderso it follows its own fallback chain).fix(embedding): support EMBEDDING_MODEL for local + restore OPENAI_EM…
fix(embedding): support EMBEDDING_MODEL for local + restore OPENAI_EM…
skywalker-35 commentedon Jun 16, 2026
Closing the loop on this from the agentmemory v0.9.21 side. We've been running a workaround for ~4 days and have confirmed the exact root cause and the fix path. There are now multiple PRs in flight (notably #832, #870, #932, #933) targeting the same
createEmbeddingProvider()factory change, so a separate PR from us would be a duplicate. Recording the validated root cause + minimal fix here for posterity and to help whichever PR lands.Confirmed minimal fix (1 char)
src/providers/embedding/index.tscurrently forcesOPENAI_API_KEYinto the OpenAI embedding provider:OpenAIEmbeddingProvideralready implements the documented precedence chain internally —apiKeyconstructor arg →OPENAI_EMBEDDING_API_KEY→OPENAI_API_KEY— but the factory's forced constructor arg short-circuits that. The fix is to drop the forced arg and let the provider resolve its own env vars:That's a 1-character diff (
!removal) and restores the documented behavior. #832 does exactly this and is the cleanest of the in-flight PRs — recommend merging that one.Our production config (confirmed working after the fix)
After applying the factory fix, the DeepSeek key no longer leaks to the SiliconFlow endpoint,
smart_searchscores normalize (no longer stuck at the1/rankBM25-only pattern), and LLM compression recovers (thecircuit_breaker_opencascade that killed it disappears because embedding 401s stop happening).Silent-failure footnote (related, not blocking)
The
catch {}block aroundembeddingProvider.embed(query)insidetripleStreamSearchswallows 401s silently. Users see zero / empty results with no signal that their embedding config is broken.agentmemory doctoronly tests connectivity, not actual embedding calls — same blind spot. Worth a one-linelog.warnin that catch so misconfig surfaces in stderr. Filing as a follow-up rather than expanding this issue's scope.Related PRs in flight
Boss-Bai commentedon Sep 7, 2026
I've encountered the same issue as well; the EMBEDDING API and the provider for OpenAI are not the same, but only the OPENAI_API_KEY read from the configuration file.
Phototonic commentedon Sep 9, 2026
Confirming this remains reproducible in v0.9.29.
I want to use an OpenAI-compatible Synthetic endpoint for LLM summaries (
OPENAI_API_KEY/OPENAI_BASE_URL) while keeping Mistral formistral-embed(OPENAI_EMBEDDING_API_KEY/OPENAI_EMBEDDING_BASE_URL). The embedding base URL correctly selects Mistral, but the factory passes the globalOPENAI_API_KEYintoOpenAIEmbeddingProvider, so the Synthetic key is sent to Mistral instead.This makes the documented separate embedding credentials ineffective whenever an OpenAI-compatible LLM provider is configured. Removing the factory argument, as proposed above, would preserve the provider's intended
OPENAI_EMBEDDING_API_KEY→OPENAI_API_KEYfallback and enable this supported split-provider setup.Also noting that PR #933 was closed without merge, so the regression is still present.
Phototonic commentedon Sep 9, 2026
Confirmed working on v0.9.29 with the minimal factory fix from #832: instantiate
OpenAIEmbeddingProvider()without forcingOPENAI_API_KEY.Tested a split setup with Synthetic for LLM summaries and Mistral
mistral-embedfor embeddings. After restart, Mistral embedding requests succeeded at 1024 dimensions, and an AgentMemory save/search smoke test completed successfully.This confirms the issue affects real split-provider deployments, not only configuration parsing. PR #832 appears to be the appropriate upstream fix.
7889545 commentedon Sep 29, 2026
Confirming this on 0.9.27 with a local fix that has been working for us (separate chat key on a local gateway, embeddings on SiliconFlow with a dedicated
OPENAI_EMBEDDING_API_KEY).Root cause in dist (0.9.27):
createEmbeddingProvider()passed the chat key into the constructor, and the constructor prefers the constructor argument overOPENAI_EMBEDDING_API_KEY:With the old line,
OPENAI_EMBEDDING_API_KEYwas silently ignored and the embedding endpoint received the chat key → 401s (or, for endpoints that ignore auth entirely, quota billed to the wrong account). Symptom on our side was a degenerate vector index and RRF-fused scores with no discrimination, because every embed call failed and fell back to BM25+graph.Happy to open a PR if helpful — the one-line call-site change above is the whole fix.
rohitg00 commentedon Oct 8, 2026
@clzwqoii fixed in v0.9.30:
OPENAI_EMBEDDING_API_KEYis honored when the embedding provider is auto-detected or chosen and wins overOPENAI_API_KEY; blank keys count as unset. https://github.com/rohitg00/agentmemory/releases/tag/v0.9.30