Skip to content

feat(embedding): support configurable vector dimensions for OpenRouter provider#756

Open
Chewji9875 wants to merge 1 commit into
rohitg00:mainfrom
Chewji9875:feat/openrouter-dimensions
Open

feat(embedding): support configurable vector dimensions for OpenRouter provider#756
Chewji9875 wants to merge 1 commit into
rohitg00:mainfrom
Chewji9875:feat/openrouter-dimensions

Conversation

@Chewji9875
Copy link
Copy Markdown

@Chewji9875 Chewji9875 commented Jun 1, 2026

WHAT

Adds dynamic configuration support for OpenRouter embedding vector dimensions via the OPENROUTER_EMBEDDING_DIMENSIONS environment variable, validating that inputs are positive finite integers, and defaulting to 1536.

WHERE

  • src/providers/embedding/openrouter.ts (Class properties and constructor parsing/validation).
  • test/embedding-provider.test.ts (Unit tests for default, custom, and invalid values, plus a node:fs mock to isolate testing).

WHEN

When initialized via environment variables or explicitly instantiated, especially when users target custom or newer models on OpenRouter (e.g., NVIDIA's 2048-dim models) that deviate from OpenAI's standard 1536 dimensions.

WHY

To prevent runtime dimension mismatches and crashes when developers use non-1536 dimension models through the OpenRouter embedding provider, enhancing flexibility and type-safe integration.

Summary by CodeRabbit

  • New Features

    • Embedding provider now supports configurable embedding dimensions via an environment setting (defaults to 1536) with validation and clearer error handling for invalid values.
  • Tests

    • Added test coverage for default behavior, configuration overrides, and invalid-dimension validation.

@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 1, 2026

@Chewji9875 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 1, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 37c8dacf-fc5b-4e44-9020-c22b55838aea

📥 Commits

Reviewing files that changed from the base of the PR and between 9456c9d and 08879c7.

📒 Files selected for processing (2)
  • src/providers/embedding/openrouter.ts
  • test/embedding-provider.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/providers/embedding/openrouter.ts
  • test/embedding-provider.test.ts

📝 Walkthrough

Walkthrough

OpenRouter embedding provider now reads OPENROUTER_EMBEDDING_DIMENSIONS, validates it as a positive integer (fallback 1536), stores it on the instance, and sends it as dimensions in embedding requests. Tests cover default, override, and invalid-value cases.

Changes

OpenRouter Embedding Dimensions Configuration

Layer / File(s) Summary
Property declaration and constructor initialization
src/providers/embedding/openrouter.ts
dimensions declared as readonly number and initialized in constructor by reading OPENROUTER_EMBEDDING_DIMENSIONS, parsing and validating as a finite positive integer, defaulting to 1536 when unset. Also wired into embedBatch request payload as dimensions.
Test suite and validation
test/embedding-provider.test.ts
Tests add a node:fs mock to avoid .env loading and include cases for default dimensions (1536), env-var override, and validation errors for invalid/non-positive OPENROUTER_EMBEDDING_DIMENSIONS values.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Suggested reviewers

  • rohitg00

Poem

🐰 I nibble env vars near and far,
Numbers parsed beneath the star.
If blank, I seed one five-three-six,
If wrong, I shout — “that’s not the fix!”
Tests hop in, all checks in place, hooray for this small race.

🚥 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 'feat(embedding): support configurable vector dimensions for OpenRouter provider' clearly and specifically summarizes the main change: adding configurable embedding vector dimensions for the OpenRouter provider via environment variable, which matches the changeset.
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.

@Chewji9875 Chewji9875 changed the title feat(embedding): support configurable dimensions for OpenRouter embedding provider feat(embedding): support configurable vector dimensions for OpenRouter provider Jun 1, 2026
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.

🧹 Nitpick comments (2)
test/embedding-provider.test.ts (1)

196-206: ⚡ Quick win

Add test case for zero dimensions to match OpenAI provider test coverage.

The OpenAIEmbeddingProvider test suite includes a test case that rejects "0" as an invalid dimension value (lines 165-168). The OpenRouterEmbeddingProvider suite should include the same test for consistency, since zero dimensions are also invalid for OpenRouter.

➕ Add missing test case
   process.env["OPENROUTER_EMBEDDING_DIMENSIONS"] = "-10";
   expect(() => new OpenRouterEmbeddingProvider("test-key")).toThrow(
     /OPENROUTER_EMBEDDING_DIMENSIONS must be a positive integer/,
   );
+
+  process.env["OPENROUTER_EMBEDDING_DIMENSIONS"] = "0";
+  expect(() => new OpenRouterEmbeddingProvider("test-key")).toThrow(
+    /OPENROUTER_EMBEDDING_DIMENSIONS must be a positive integer/,
+  );
 });
🤖 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 196 - 206, Add a test asserting
that OPENROUTER_EMBEDDING_DIMENSIONS="0" is treated as invalid in the
OpenRouterEmbeddingProvider tests: update the test block around the invalid
dimensions (the one creating new OpenRouterEmbeddingProvider("test-key")) to
include a case that sets process.env["OPENROUTER_EMBEDDING_DIMENSIONS"]="0" and
expects new OpenRouterEmbeddingProvider("test-key") to throw the same
/OPENROUTER_EMBEDDING_DIMENSIONS must be a positive integer/ error, mirroring
the OpenAIEmbeddingProvider coverage.
src/providers/embedding/openrouter.ts (1)

20-31: ⚡ Quick win

Consider stricter integer validation for the environment variable.

The current validation uses parseInt(dimStr, 10), which silently truncates decimals and ignores trailing non-numeric characters:

  • "768.5" → parsed as 768
  • "768abc" → parsed as 768

This may surprise users who mistype the value. For clearer error messages and to match the "must be a positive integer" expectation, validate that the trimmed string contains only digits before parsing.

🔒 Stricter validation example
 const dimStr = getEnvVar("OPENROUTER_EMBEDDING_DIMENSIONS");
 if (dimStr !== undefined && dimStr.trim().length > 0) {
-  const parsed = parseInt(dimStr, 10);
-  if (!Number.isFinite(parsed) || parsed <= 0) {
+  const trimmed = dimStr.trim();
+  if (!/^\d+$/.test(trimmed)) {
+    throw new Error(
+      `OPENROUTER_EMBEDDING_DIMENSIONS must be a positive integer, got: ${dimStr}`,
+    );
+  }
+  const parsed = parseInt(trimmed, 10);
+  if (parsed <= 0) {
     throw new Error(
       `OPENROUTER_EMBEDDING_DIMENSIONS must be a positive integer, got: ${dimStr}`,
     );

Alternatively, use Number() with Number.isInteger():

 const dimStr = getEnvVar("OPENROUTER_EMBEDDING_DIMENSIONS");
 if (dimStr !== undefined && dimStr.trim().length > 0) {
-  const parsed = parseInt(dimStr, 10);
-  if (!Number.isFinite(parsed) || parsed <= 0) {
+  const parsed = Number(dimStr.trim());
+  if (!Number.isInteger(parsed) || parsed <= 0) {
     throw new Error(
       `OPENROUTER_EMBEDDING_DIMENSIONS must be a positive integer, got: ${dimStr}`,
     );
🤖 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/openrouter.ts` around lines 20 - 31, The validation
for OPENROUTER_EMBEDDING_DIMENSIONS (dimStr) is too permissive—replace the
parseInt-based check in the block that reads getEnvVar and sets this.dimensions
with a stricter digit-only validation: trim dimStr, ensure it matches only
digits (e.g., /^\d+$/) before parsing to an integer, then verify >0; if it
fails, throw the existing error message; otherwise set this.dimensions to the
parsed integer (falling back to 1536 when undefined). Use the existing
identifiers dimStr, getEnvVar, parseInt (or Number) and this.dimensions to
locate and update the code.
🤖 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.

Nitpick comments:
In `@src/providers/embedding/openrouter.ts`:
- Around line 20-31: The validation for OPENROUTER_EMBEDDING_DIMENSIONS (dimStr)
is too permissive—replace the parseInt-based check in the block that reads
getEnvVar and sets this.dimensions with a stricter digit-only validation: trim
dimStr, ensure it matches only digits (e.g., /^\d+$/) before parsing to an
integer, then verify >0; if it fails, throw the existing error message;
otherwise set this.dimensions to the parsed integer (falling back to 1536 when
undefined). Use the existing identifiers dimStr, getEnvVar, parseInt (or Number)
and this.dimensions to locate and update the code.

In `@test/embedding-provider.test.ts`:
- Around line 196-206: Add a test asserting that
OPENROUTER_EMBEDDING_DIMENSIONS="0" is treated as invalid in the
OpenRouterEmbeddingProvider tests: update the test block around the invalid
dimensions (the one creating new OpenRouterEmbeddingProvider("test-key")) to
include a case that sets process.env["OPENROUTER_EMBEDDING_DIMENSIONS"]="0" and
expects new OpenRouterEmbeddingProvider("test-key") to throw the same
/OPENROUTER_EMBEDDING_DIMENSIONS must be a positive integer/ error, mirroring
the OpenAIEmbeddingProvider coverage.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 097bc5b2-21e7-461f-a4f6-aa728e485c8d

📥 Commits

Reviewing files that changed from the base of the PR and between fd9e3bd and bf9e791.

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

@Chewji9875 Chewji9875 force-pushed the feat/openrouter-dimensions branch from 9456c9d to 08879c7 Compare June 4, 2026 16:14
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