fix(openai-embeddings): honor embedding-specific API key in factory and auto-detect#692
Conversation
…nd auto-detect Follow-up to rohitg00#503. PR rohitg00#503 added OPENAI_EMBEDDING_API_KEY / OPENAI_EMBEDDING_BASE_URL to OpenAIEmbeddingProvider but two call sites still bypass them, so the env vars do not take effect when set alongside OPENAI_API_KEY: 1. src/providers/embedding/index.ts:38 — factory passes getEnvVar("OPENAI_API_KEY")! into the constructor's apiKey arg, beating the constructor's documented precedence (apiKey > OPENAI_EMBEDDING_API_KEY > OPENAI_API_KEY). 2. src/config.ts — detectEmbeddingProvider() does not consider OPENAI_EMBEDDING_API_KEY, so an embedding-only OpenAI config paired with a different chat provider (e.g. Gemini) misroutes embeddings to whichever generic provider key is also set. Reproduces against real OpenAI: OPENAI_API_KEY=sk-deepseek-... # chat on a self-hosted/proxy OPENAI_EMBEDDING_API_KEY=sk-openai-real-... OPENAI_EMBEDDING_BASE_URL=https://api.openai.com Embedding requests 401 because the LLM key is sent. The bug is invisible when the embedding endpoint ignores Authorization (local vLLM / Ollama / LM Studio), which is why rohitg00#503's verification missed it. Fix: - index.ts: drop the explicit OPENAI_API_KEY argument; the constructor already documents and implements the correct precedence. - config.ts: check OPENAI_EMBEDDING_API_KEY ahead of generic provider keys so "chat on X, embeddings on OpenAI" auto-detects without forcing EMBEDDING_PROVIDER=openai. - .env.example, README.md, openai.ts JSDoc: sync docs to match the now-functional behavior. - test/embedding-provider.test.ts: 3 regression tests (both-keys factory path, embedding-only detect, gemini+embedding precedence); expand beforeEach to strip embedding-specific env vars. Compatibility: zero behavior change for users not setting OPENAI_EMBEDDING_*; only the two currently-broken configurations above shift, which is the intent. Signed-off-by: Ning <cningwei@gmail.com>
|
@moonshardx is attempting to deploy a commit to the rohitg00's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (6)
📝 WalkthroughWalkthroughThis PR introduces embedding-specific OpenAI configuration variables ( ChangesEmbedding-Specific OpenAI Configuration
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint skipped: no ESLint configuration detected in root package.json. To enable, add 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 |
Follow-up to #503.
Repro
Embedding requests 401 from OpenAI because the LLM key is still sent. The bug is invisible against local endpoints (vLLM / Ollama / LM Studio) that ignore
Authorization, which is why #503's verification didn't surface it.Fix
Two call sites still bypass the embedding-specific env added in #503:
src/providers/embedding/index.ts:38OPENAI_API_KEYargument; let the constructor resolve precedence (apiKey arg > OPENAI_EMBEDDING_API_KEY > OPENAI_API_KEY).src/config.tsdetectEmbeddingProviderOPENAI_EMBEDDING_API_KEYahead of generic provider keys so "chat on X, embeddings on OpenAI" auto-detects without forcingEMBEDDING_PROVIDER=openai.src/providers/embedding/openai.tsJSDoc.env.example,README.mdOPENAI_EMBEDDING_API_KEYandOPENAI_EMBEDDING_BASE_URL; update the detection-order comment.test/embedding-provider.test.tsbeforeEachto strip embedding-specific env.Compatibility
OPENAI_API_KEYsetOPENAI_API_KEYOPENAI_API_KEY— identicalGEMINI_API_KEYset"gemini""gemini"— identicalOPENAI_API_KEY+EMBEDDING_PROVIDER=openaiOPENAI_API_KEYOPENAI_API_KEY— identicalOPENAI_API_KEY+OPENAI_EMBEDDING_API_KEYGEMINI_API_KEY+OPENAI_EMBEDDING_API_KEY(noEMBEDDING_PROVIDER)"gemini")"openai"— fixedThe only behavior shifts are in the two currently-broken rows — those configurations misroute embeddings today, so changing them is the intent. No silent regression for any working config.
Test plan
npm test -- embedding-provider— 20/20 green (17 existing + 3 new regression tests).npm test— full suite green (1241/1241; integration test undertest/integration.test.tsskipped per CONTRIBUTING.md).npm run build—tsdownclean.Summary by CodeRabbit
New Features
Documentation
Tests