fix(ai): match hyphenated Qwen3-Embedding ids for dimensions passthrough (#3909)

Wave-assembled from PR #3909 by @mikez93.

Co-Authored-By: Mike Williamson <mike@imekka.com>
This commit is contained in:
Garry Tan
2026-08-12 14:38:36 -07:00
committed by Sina Matian
co-authored by Mike Williamson
parent e795324ec5
commit 8ecd52022e
2 changed files with 35 additions and 4 deletions
+10 -4
View File
@@ -309,9 +309,12 @@ export function dimsProviderOptions(
// provider serving it) supports Matryoshka truncation via `dimensions`.
// Native sizes: 0.6B=1024, 4B=2560, 8B=4096. Without `dimensions`,
// Ollama returns the native size and brains configured for narrower
// widths hard-fail with a dim-mismatch error. Pattern match the bare
// model name + any `:tag` (e.g. `qwen3-embedding:4b`, `qwen3-embedding:0.6b`).
if (modelId === 'qwen3-embedding' || modelId.startsWith('qwen3-embedding:')) {
// widths hard-fail with a dim-mismatch error. Two naming schemes reach
// this path: Ollama's colon-tag form (`qwen3-embedding:4b`) and the
// hyphenated hub form used by OpenRouter/HF-style routers
// (`qwen/qwen3-embedding-8b` — org prefix stripped to
// `qwen3-embedding-8b` above). Match both.
if (bareModelId === 'qwen3-embedding' || bareModelId.startsWith('qwen3-embedding:') || bareModelId.startsWith('qwen3-embedding-')) {
// Only send `dimensions` when it actually differs from the model's
// native width. Fixed-dim OpenAI-compatible backends serving this
// family (e.g. vLLM) reject the parameter outright with HTTP 400
@@ -323,8 +326,11 @@ export function dimsProviderOptions(
'qwen3-embedding:0.6b': 1024,
'qwen3-embedding:4b': 2560,
'qwen3-embedding:8b': 4096,
'qwen3-embedding-0.6b': 1024,
'qwen3-embedding-4b': 2560,
'qwen3-embedding-8b': 4096,
};
if (QWEN3_EMBEDDING_NATIVE_DIMS[modelId] === dims) return undefined;
if (QWEN3_EMBEDDING_NATIVE_DIMS[bareModelId] === dims) return undefined;
return { openaiCompatible: { dimensions: dims } };
}
// MiniMax embo-01 takes a `type: 'db' | 'query'` field for asymmetric
+25
View File
@@ -38,3 +38,28 @@ describe('qwen3-embedding native-width suppression', () => {
.toEqual({ openaiCompatible: { dimensions: 1024 } });
});
});
describe('qwen3-embedding hyphenated hub-form ids (OpenRouter et al.)', () => {
test('org-prefixed hub id requests Matryoshka truncation at non-native dim', () => {
expect(dimsProviderOptions('openai-compatible', 'qwen/qwen3-embedding-8b', 1536))
.toEqual({ openaiCompatible: { dimensions: 1536 } });
expect(dimsProviderOptions('openai-compatible', 'qwen/qwen3-embedding-4b', 1024))
.toEqual({ openaiCompatible: { dimensions: 1024 } });
});
test('hub id at native width emits no dimensions param', () => {
expect(dimsProviderOptions('openai-compatible', 'qwen/qwen3-embedding-8b', 4096)).toBeUndefined();
expect(dimsProviderOptions('openai-compatible', 'qwen/qwen3-embedding-4b', 2560)).toBeUndefined();
expect(dimsProviderOptions('openai-compatible', 'qwen/qwen3-embedding-0.6b', 1024)).toBeUndefined();
});
test('bare hyphenated id (no org prefix) also matches', () => {
expect(dimsProviderOptions('openai-compatible', 'qwen3-embedding-8b', 2000))
.toEqual({ openaiCompatible: { dimensions: 2000 } });
});
test('unknown hyphenated variant falls through to sending the configured dim', () => {
expect(dimsProviderOptions('openai-compatible', 'qwen3-embedding-32b', 1024))
.toEqual({ openaiCompatible: { dimensions: 1024 } });
});
});