Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

dims: thread Matryoshka dimensions for Qwen3-Embedding on Ollama (#1072)

Michael Gandal02ba4b4fc2cc
diff --git a/src/core/ai/dims.ts b/src/core/ai/dims.ts
index 7961516a57..b88a4e175f 100644
--- a/src/core/ai/dims.ts
+++ b/src/core/ai/dims.ts
@@ -263,6 +263,15 @@ export function dimsProviderOptions(
if (modelId === 'text-embedding-v3' || modelId === 'embedding-3') {
return { openaiCompatible: { dimensions: dims } };
}
+ // Qwen3-Embedding family on Ollama (and any other openai-compatible
+ // 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:')) {
+ return { openaiCompatible: { dimensions: dims } };
+ }
// MiniMax embo-01 takes a `type: 'db' | 'query'` field for asymmetric
// retrieval. Today still hardcoded to 'db' for back-compat — opting
// into the new inputType seam is a follow-up (see plan's deferred
diff --git a/test/ai/recipe-ollama-dims.test.ts b/test/ai/recipe-ollama-dims.test.ts
new file mode 100644
index 0000000000..4f1d86ce8b
--- /dev/null
+++ b/test/ai/recipe-ollama-dims.test.ts
@@ -0,0 +1,41 @@
+/**
+ * Ollama Matryoshka dims passthrough.
+ *
+ * Several embedding models served via Ollama (Qwen3-Embedding family) support
+ * Matryoshka truncation through the `dimensions` field on /v1/embeddings.
+ * Without this passthrough, gbrain ignores user-selected reduced dims and the
+ * provider returns its native size, causing dim-mismatch failures against
+ * brains configured for smaller widths.
+ */
+
+import { describe, expect, test } from 'bun:test';
+import { dimsProviderOptions } from '../../src/core/ai/dims.ts';
+
+describe('dims: ollama Matryoshka models', () => {
+ test('qwen3-embedding:4b threads dimensions=1536', () => {
+ expect(dimsProviderOptions('openai-compatible', 'qwen3-embedding:4b', 1536))
+ .toEqual({ openaiCompatible: { dimensions: 1536 } });
+ });
+
+ test('qwen3-embedding:0.6b threads dimensions=512', () => {
+ expect(dimsProviderOptions('openai-compatible', 'qwen3-embedding:0.6b', 512))
+ .toEqual({ openaiCompatible: { dimensions: 512 } });
+ });
+
+ test('qwen3-embedding:8b threads dimensions=2048', () => {
+ expect(dimsProviderOptions('openai-compatible', 'qwen3-embedding:8b', 2048))
+ .toEqual({ openaiCompatible: { dimensions: 2048 } });
+ });
+
+ test('bare qwen3-embedding (no quant tag) also recognized', () => {
+ expect(dimsProviderOptions('openai-compatible', 'qwen3-embedding', 1024))
+ .toEqual({ openaiCompatible: { dimensions: 1024 } });
+ });
+
+ test('unrelated openai-compat model returns undefined (regression guard)', () => {
+ expect(dimsProviderOptions('openai-compatible', 'nomic-embed-text', 768))
+ .toBeUndefined();
+ expect(dimsProviderOptions('openai-compatible', 'mxbai-embed-large', 1024))
+ .toBeUndefined();
+ });
+});