Skip to content

fix: resolve OpenRouter embedding dimensions from model table + env override#1004

Open
kagura-agent wants to merge 3 commits into
rohitg00:mainfrom
kagura-agent:fix/openrouter-embedding-dimensions-1002
Open

fix: resolve OpenRouter embedding dimensions from model table + env override#1004
kagura-agent wants to merge 3 commits into
rohitg00:mainfrom
kagura-agent:fix/openrouter-embedding-dimensions-1002

Conversation

@kagura-agent

@kagura-agent kagura-agent commented Jul 3, 2026

Copy link
Copy Markdown

Summary

OpenRouterEmbeddingProvider hardcoded dimensions = 1536 regardless of the configured model. Models with different native dimensions (e.g. qwen/qwen3-embedding-8b at 4096) produced vectors silently rejected by the index, causing semantic recall to return 0 hits with no logged error.

Changes

src/providers/embedding/openrouter.ts:

  • Add MODEL_DIMENSIONS table for known OpenRouter-accessible embedding models, following the pattern established by OpenAIEmbeddingProvider
  • Support OPENROUTER_EMBEDDING_DIMENSIONS env var override for custom/unlisted models
  • Extract resolveDimensions() helper (same shape as the OpenAI provider)
  • Default behavior unchanged: openai/text-embedding-3-small still resolves to 1536

test/openrouter-embedding.test.ts (new):

  • Default dimensions (1536)
  • Model-derived dimensions (qwen3-embedding-8b → 4096, text-embedding-3-large → 3072)
  • Env override takes precedence
  • Unknown model fallback (1536)
  • Invalid override rejection
  • Correct model in request body

How it works

  1. Check OPENROUTER_EMBEDDING_DIMENSIONS env var — if set, use it (explicit override always wins)
  2. Look up the configured model in the MODEL_DIMENSIONS table
  3. Fall back to 1536 (default model dimensions) for unknown models

The existing withDimensionGuard wrapper (applied in createEmbeddingProvider) will catch any remaining mismatches at runtime.

Fixes #1002

Summary by CodeRabbit

  • New Features
    • Embedding settings now adapt to the selected OpenRouter model, with support for a custom dimension override.
  • Bug Fixes
    • Added validation for embedding dimension values to prevent invalid configuration.
    • Unknown models now safely fall back to a standard default dimension.
  • Tests
    • Added coverage for model-based dimensions, environment overrides, missing API keys, invalid values, and request payloads.

Add MODEL_DIMENSIONS table for known OpenRouter embedding models and
support OPENROUTER_EMBEDDING_DIMENSIONS env override, matching the
pattern established by OpenAIEmbeddingProvider.

Previously dimensions was hardcoded to 1536 regardless of configured
model. Models with different native dimensions (e.g. qwen3-embedding-8b
at 4096) would produce vectors silently rejected by the index, causing
semantic recall to return 0 hits with no logged error.

Fixes rohitg00#1002
@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

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: 9fba480d-632c-4149-989b-8e7701bdba42

📥 Commits

Reviewing files that changed from the base of the PR and between 29522ed and b50cafe.

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

📝 Walkthrough

Walkthrough

OpenRouterEmbeddingProvider now resolves embedding dimensions from the configured model or an override env var, and the new Vitest suite covers the constructor and request wiring behavior.

Changes

OpenRouter embedding dimensions

Layer / File(s) Summary
Dimension resolution logic and constructor wiring
src/providers/embedding/openrouter.ts
Adds DEFAULT_MODEL, MODEL_DIMENSIONS, DEFAULT_DIMENSIONS, and resolveDimensions() that parses OPENROUTER_EMBEDDING_DIMENSIONS or derives dimensions from the model; dimensions field becomes typed number set in constructor.
Test coverage for dimension resolution
test/openrouter-embedding.test.ts
New Vitest suite covering default/model-derived/override/unknown-model dimension resolution, invalid override errors, missing API key error, and request body model wiring in embed().

Estimated code review effort: 2 (Simple) | ~15 minutes

Possibly related issues

Suggested reviewers: rohitg00

🚥 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 clearly summarizes the main change: resolving OpenRouter embedding dimensions from model data with an env override.
Linked Issues check ✅ Passed The PR addresses the issue by replacing the hardcoded dimensions with model-based resolution and an override for custom or unlisted models.
Out of Scope Changes check ✅ Passed The changes stay focused on OpenRouter embedding dimension resolution and its tests, with no obvious unrelated additions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

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

Inline comments:
In `@src/providers/embedding/openrouter.ts`:
- Around line 28-39: The resolveDimensions helper in openrouter.ts silently
falls back to DEFAULT_DIMENSIONS when MODEL_DIMENSIONS has no entry for the
given model and no override is set. Add a warning log in resolveDimensions (or
its caller) when this fallback happens so unknown or custom models are clearly
surfaced, and keep the existing override validation path intact. Use the
resolveDimensions function and MODEL_DIMENSIONS lookup to locate the change.
- Around line 28-37: The override parsing in resolveDimensions allows malformed
OPENROUTER_EMBEDDING_DIMENSIONS values to pass because parseInt truncates
trailing characters and decimals. Update resolveDimensions in openrouter.ts to
validate the override string strictly before converting it to a number, so only
whole positive integers are accepted and values like “100abc” or “10.5” are
rejected with the existing error message.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b76640bd-a540-4324-b341-47178f80332d

📥 Commits

Reviewing files that changed from the base of the PR and between 93ae9bc and 29522ed.

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

Comment thread src/providers/embedding/openrouter.ts
Comment thread src/providers/embedding/openrouter.ts
…back

- Replace parseInt() with Number() + isInteger check to reject malformed
  overrides like '100abc' (trailing garbage) and '10.5' (decimals)
- Add console.warn when model is not in MODEL_DIMENSIONS and no override
  is set, so users know they're using the default 1536 dimensions

Addresses CodeRabbit review feedback on rohitg00#1004.
@vercel

vercel Bot commented Jul 3, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

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.

OpenRouterEmbeddingProvider hardcodes dimensions=1536 — vectors from 4096-dim models (qwen3-embedding-8b) silently dropped

1 participant