Skip to content

Fix OpenAI embedding provider key selection#832

Open
jinzheng8115 wants to merge 2 commits into
rohitg00:mainfrom
jinzheng8115:fix/openai-embedding-key-decoupling
Open

Fix OpenAI embedding provider key selection#832
jinzheng8115 wants to merge 2 commits into
rohitg00:mainfrom
jinzheng8115:fix/openai-embedding-key-decoupling

Conversation

@jinzheng8115
Copy link
Copy Markdown

@jinzheng8115 jinzheng8115 commented Jun 5, 2026

Summary

  • prefer OPENAI_EMBEDDING_API_KEY when creating the OpenAI-compatible embedding provider
  • keep OPENAI_API_KEY as the fallback for existing setups
  • add a regression test covering split LLM key and embedding key setups

Why

When agentmemory is configured with different OpenAI-compatible providers for chat and embeddings, createEmbeddingProvider() was still passing OPENAI_API_KEY into OpenAIEmbeddingProvider. In practice this breaks setups like LLM=DeepSeek and embeddings=DashScope, because the DeepSeek key gets sent to the embedding endpoint.

Verification

  • env -i HOME=/tmp/agentmemory-test-home PATH="$PATH" TERM="$TERM" npm test -- --run test/embedding-provider.test.ts
  • env -i HOME=/tmp/agentmemory-test-home PATH="$PATH" TERM="$TERM" npm run build
  • local runtime verification against a real split-provider setup: LLM=DeepSeek, embeddings=DashScope text-embedding-v4

Summary by CodeRabbit

  • New Features

    • Added support for dedicated OpenAI embedding API key configuration, taking precedence over the general OpenAI API key when both are set.
  • Tests

    • Added test coverage for embedding provider API key precedence behavior.

Copilot AI review requested due to automatic review settings June 5, 2026 05:11
@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 5, 2026

@jinzheng8115 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 5, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

The pull request updates the OpenAI embedding provider initialization to prefer OPENAI_EMBEDDING_API_KEY when set, falling back to OPENAI_API_KEY otherwise. A new test verifies this preference behavior.

Changes

OpenAI Embedding API Key Preference

Layer / File(s) Summary
Embedding API key resolution with test coverage
src/providers/embedding/index.ts, test/embedding-provider.test.ts
createEmbeddingProvider() now uses OPENAI_EMBEDDING_API_KEY as the primary source for OpenAI embedding authentication, falling back to OPENAI_API_KEY if the specialized key is not set. A new test verifies that the provider correctly prefers the embedding-specific key when both are configured.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

  • rohitg00/agentmemory#503: Both PRs update the OpenAI embedding API key resolution to prefer OPENAI_EMBEDDING_API_KEY over OPENAI_API_KEY (main PR in the provider factory/test, retrieved PR in OpenAIEmbeddingProvider constructor/test).

Suggested reviewers

  • rohitg00

Poem

🐰 A key preference emerges,
Embedding-wise it surges,
Fallback flows with care,
Tests ensure it's fair,
Configuration merge purges! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and clearly describes the main change: fixing the OpenAI embedding provider to use the correct API key (OPENAI_EMBEDDING_API_KEY with fallback to OPENAI_API_KEY), which is the core modification across both the implementation and test files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Updates OpenAI embedding provider initialization to prefer a dedicated embedding API key, and adds test coverage for the new precedence behavior.

Changes:

  • Prefer OPENAI_EMBEDDING_API_KEY over OPENAI_API_KEY when creating the OpenAI embedding provider.
  • Add a unit test validating the environment variable precedence for OpenAI embeddings.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
test/embedding-provider.test.ts Adds a test ensuring OPENAI_EMBEDDING_API_KEY takes precedence over OPENAI_API_KEY.
src/providers/embedding/index.ts Updates OpenAI provider creation to use the embedding-specific API key when present.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +47 to +48
process.env["OPENAI_API_KEY"] = "llm-key-123";
process.env["OPENAI_EMBEDDING_API_KEY"] = "embedding-key-456";
Comment on lines +49 to +53
const provider = createEmbeddingProvider() as OpenAIEmbeddingProvider & {
apiKey: string;
};
expect(provider).toBeInstanceOf(OpenAIEmbeddingProvider);
expect(provider.apiKey).toBe("embedding-key-456");
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 (1)
test/embedding-provider.test.ts (1)

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

Reset OPENAI_EMBEDDING_API_KEY in test setup to avoid env-dependent flakes.

beforeEach clears OPENAI_API_KEY but not OPENAI_EMBEDDING_API_KEY. If that variable exists in the runner environment, the “returns null when no API keys are set” test can fail nondeterministically.

Suggested fix
  beforeEach(() => {
    process.env = { ...originalEnv };
    delete process.env["GEMINI_API_KEY"];
    delete process.env["OPENAI_API_KEY"];
+   delete process.env["OPENAI_EMBEDDING_API_KEY"];
    delete process.env["VOYAGE_API_KEY"];
    delete process.env["COHERE_API_KEY"];
    delete process.env["OPENROUTER_API_KEY"];
    delete process.env["EMBEDDING_PROVIDER"];
  });

Also applies to: 46-54

🤖 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 hook is
clearing several API key environment variables but is missing
OPENAI_EMBEDDING_API_KEY, which can cause nondeterministic test failures if this
variable exists in the runner environment. Add a line to delete
process.env["OPENAI_EMBEDDING_API_KEY"] in the beforeEach method alongside the
other API key deletions, and apply the same fix to the other beforeEach hook
mentioned at lines 46-54 to ensure consistent test isolation across all test
setup methods.
🤖 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 `@test/embedding-provider.test.ts`:
- Around line 13-21: The beforeEach hook is clearing several API key environment
variables but is missing OPENAI_EMBEDDING_API_KEY, which can cause
nondeterministic test failures if this variable exists in the runner
environment. Add a line to delete process.env["OPENAI_EMBEDDING_API_KEY"] in the
beforeEach method alongside the other API key deletions, and apply the same fix
to the other beforeEach hook mentioned at lines 46-54 to ensure consistent test
isolation across all test setup methods.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bb6cd875-c1ee-436d-a264-0af025cf7272

📥 Commits

Reviewing files that changed from the base of the PR and between 2a58140 and 20e1477.

📒 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.

2 participants