Skip to content

fix: prefer embedding API key for OpenAI embeddings#802

Open
FrankZ20 wants to merge 1 commit into
rohitg00:mainfrom
FrankZ20:fix/openai-embedding-api-key
Open

fix: prefer embedding API key for OpenAI embeddings#802
FrankZ20 wants to merge 1 commit into
rohitg00:mainfrom
FrankZ20:fix/openai-embedding-api-key

Conversation

@FrankZ20
Copy link
Copy Markdown

@FrankZ20 FrankZ20 commented Jun 3, 2026

Summary

Fix createEmbeddingProvider() so OpenAI-compatible embeddings prefer OPENAI_EMBEDDING_API_KEY when it is set.

OpenAIEmbeddingProvider already supports a dedicated embedding key internally, but the factory currently passes OPENAI_API_KEY directly into the constructor. That caller-provided text LLM key wins over the provider's internal fallback chain, so deployments with separate chat and embedding providers can accidentally send embedding requests with the chat key.

Changes

  • Pass OPENAI_EMBEDDING_API_KEY || OPENAI_API_KEY from the OpenAI embedding factory branch.
  • Add a regression test that sets both keys and verifies the embedding request uses the embedding-specific key.

Why

This matters for OpenAI-compatible deployments where chat and embeddings live on different providers/endpoints. For example, a chat proxy key in OPENAI_API_KEY plus DashScope/Bailian in OPENAI_EMBEDDING_API_KEY currently causes vector indexing to call the embedding endpoint with the chat key and fail with 401 invalid_api_key.

Tests

  • Not run locally in this environment; change is covered by the added embedding-provider.test.ts regression test.

Summary by CodeRabbit

  • New Features

    • OpenAI embedding operations now support a dedicated API key configuration for enhanced credential management and security.
  • Tests

    • Added test coverage to verify proper API key handling for embedding requests.

@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 3, 2026

@FrankZ20 is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 3, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

The PR adds support for a dedicated OpenAI embedding API key environment variable. The embedding provider now checks OPENAI_EMBEDDING_API_KEY first and falls back to OPENAI_API_KEY if unavailable, with a new test validating the Authorization header uses the correct embedding key.

Changes

Embedding API key selection

Layer / File(s) Summary
OpenAI embedding API key selection with fallback
src/providers/embedding/index.ts
createEmbeddingProvider() now constructs OpenAIEmbeddingProvider preferring OPENAI_EMBEDDING_API_KEY and falling back to OPENAI_API_KEY, wrapped by withDimensionGuard as before.
Embedding API key integration test
test/embedding-provider.test.ts
New test verifies that when OPENAI_EMBEDDING_API_KEY is set, provider.embed("hello") sends an Authorization: Bearer <embedding-key> header with the embedding-specific key to fetch.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • rohitg00/agentmemory#503: Both PRs update the OpenAI embedding credential selection to prioritize OPENAI_EMBEDDING_API_KEY with fallback to OPENAI_API_KEY (and align embedding-provider tests around that header/env behavior).

Suggested reviewers

  • rohitg00

Poem

🐰 A key by any other name would authenticate just as sweet,
Now embeddings have their own credential to complete the feat.
With fallback grace when custom keys are sparse,
The API flows like water, never sparse.
Huzzah! 🌟

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: prefer embedding API key for OpenAI embeddings' accurately summarizes the main change: updating the OpenAI embedding provider to prioritize OPENAI_EMBEDDING_API_KEY over OPENAI_API_KEY.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
test/embedding-provider.test.ts (1)

13-21: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

beforeEach doesn't clear OPENAI_EMBEDDING_API_KEY.

This block resets several provider keys but not OPENAI_EMBEDDING_API_KEY. If the test environment has it set, the new test as well as returns null when no API keys are set could behave non-hermetically depending on detectEmbeddingProvider(). Add the delete for consistency.

💚 Suggested isolation fix
     delete process.env["OPENROUTER_API_KEY"];
     delete process.env["EMBEDDING_PROVIDER"];
+    delete process.env["OPENAI_EMBEDDING_API_KEY"];
   });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/embedding-provider.test.ts` around lines 13 - 21, The beforeEach setup
in embedding-provider.test.ts fails to clear OPENAI_EMBEDDING_API_KEY, which can
make detectEmbeddingProvider() and the "returns null when no API keys are set"
test non-hermetic; update the beforeEach block (the one in
test/embedding-provider.test.ts) to also delete
process.env["OPENAI_EMBEDDING_API_KEY"] alongside the other deletes so the
environment is fully reset before each test.
src/providers/embedding/index.ts (1)

30-42: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Update detectEmbeddingProvider() to support embedding-only OpenAI configs (OPENAI_EMBEDDING_API_KEY)
detectEmbeddingProvider() in src/config.ts returns "openai" only when OPENAI_API_KEY is set, ignoring OPENAI_EMBEDDING_API_KEY; this makes createEmbeddingProvider() return null for deployments that provide embeddings via OPENAI_EMBEDDING_API_KEY but not OPENAI_API_KEY.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/providers/embedding/index.ts` around lines 30 - 42,
detectEmbeddingProvider currently only returns "openai" when OPENAI_API_KEY is
present, causing createEmbeddingProvider (which expects "openai") to miss setups
that only provide OPENAI_EMBEDDING_API_KEY; update detectEmbeddingProvider in
src/config.ts to check for either OPENAI_EMBEDDING_API_KEY or OPENAI_API_KEY and
return "openai" if either env var is set (use getEnvVar or equivalent), ensuring
createEmbeddingProvider's case "openai" is reachable when only the
embedding-specific key is configured.
🧹 Nitpick comments (1)
src/providers/embedding/index.ts (1)

38-42: 💤 Low value

Simplify OpenAIEmbeddingProvider construction to avoid duplicated key precedence

OpenAIEmbeddingProvider’s constructor already applies apiKey || OPENAI_EMBEDDING_API_KEY || OPENAI_API_KEY and throws if the resulting key is empty, so passing getEnvVar("OPENAI_EMBEDDING_API_KEY") || getEnvVar("OPENAI_API_KEY")! duplicates that logic.

♻️ Proposed simplification
     case "openai":
       return withDimensionGuard(
-        new OpenAIEmbeddingProvider(
-          getEnvVar("OPENAI_EMBEDDING_API_KEY") || getEnvVar("OPENAI_API_KEY")!,
-        ),
+        new OpenAIEmbeddingProvider(),
       );
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/providers/embedding/index.ts` around lines 38 - 42, The code duplicates
API-key precedence logic by passing getEnvVar("OPENAI_EMBEDDING_API_KEY") ||
getEnvVar("OPENAI_API_KEY") into the OpenAIEmbeddingProvider constructor even
though OpenAIEmbeddingProvider already implements apiKey ||
OPENAI_EMBEDDING_API_KEY || OPENAI_API_KEY and throws on missing keys; fix this
by constructing OpenAIEmbeddingProvider without providing the env key (i.e., new
OpenAIEmbeddingProvider()) and keep it wrapped in withDimensionGuard so the
provider's internal key resolution and validation are used.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/providers/embedding/index.ts`:
- Around line 30-42: detectEmbeddingProvider currently only returns "openai"
when OPENAI_API_KEY is present, causing createEmbeddingProvider (which expects
"openai") to miss setups that only provide OPENAI_EMBEDDING_API_KEY; update
detectEmbeddingProvider in src/config.ts to check for either
OPENAI_EMBEDDING_API_KEY or OPENAI_API_KEY and return "openai" if either env var
is set (use getEnvVar or equivalent), ensuring createEmbeddingProvider's case
"openai" is reachable when only the embedding-specific key is configured.

In `@test/embedding-provider.test.ts`:
- Around line 13-21: The beforeEach setup in embedding-provider.test.ts fails to
clear OPENAI_EMBEDDING_API_KEY, which can make detectEmbeddingProvider() and the
"returns null when no API keys are set" test non-hermetic; update the beforeEach
block (the one in test/embedding-provider.test.ts) to also delete
process.env["OPENAI_EMBEDDING_API_KEY"] alongside the other deletes so the
environment is fully reset before each test.

---

Nitpick comments:
In `@src/providers/embedding/index.ts`:
- Around line 38-42: The code duplicates API-key precedence logic by passing
getEnvVar("OPENAI_EMBEDDING_API_KEY") || getEnvVar("OPENAI_API_KEY") into the
OpenAIEmbeddingProvider constructor even though OpenAIEmbeddingProvider already
implements apiKey || OPENAI_EMBEDDING_API_KEY || OPENAI_API_KEY and throws on
missing keys; fix this by constructing OpenAIEmbeddingProvider without providing
the env key (i.e., new OpenAIEmbeddingProvider()) and keep it wrapped in
withDimensionGuard so the provider's internal key resolution and validation are
used.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1dd046f6-04aa-4d3c-8397-c49ca5f7ed39

📥 Commits

Reviewing files that changed from the base of the PR and between d442fee and 91efb16.

📒 Files selected for processing (2)
  • src/providers/embedding/index.ts
  • test/embedding-provider.test.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant