feat(embedding): support dimensions for openai-compatible Matryoshka models (#405)
Fixes #405
Summary
Add optional embedding.dimensions for the openai-compatible provider to support Matryoshka models like Qwen3-Embedding-8B (768/1024/2048/4096 via POST {dimensions}).
Omitted keeps current behavior (byte-identical identity, no re-embed). Setting triggers per-model coexistence re-embed.
Changes
- Schema
packages/plugin/src/config/schema/magic-context.ts:BaseEmbeddingConfigSchema.dimensions(max 8192) + include inEmbeddingConfigSchema.transformforopenai-compatible/synapse - Identity
embedding-identity.ts:getEmbeddingProviderIdentityfolds...dimensions?{dimensions}:{} - Runtime
embedding.ts/project-embedding-registry.ts:resolveEmbeddingConfigpreservesdimensions;getChunkEmbeddingModelIdincludesdimensions;EMBEDDING_AFFECTING_KEYSaddsembedding.dimensions - Transport
embedding-openai.ts:OpenAICompatibleEmbeddingProviderOptions.dimensions,this.dimensions,modelIdincludes dimensions,embedBatchadds...this.dimensions?{dimensions}:{}toPOST /v1/embeddings - Schema JSON
assets/magic-context.schema.jsonregenerated viabun build-schema.tspattern (manual edit matching generator output)
Mirrors existing truncate/input_type/local_dtype conditional-fold pattern (#127, #155, #259).
Testing
- Local
pi-magic-context@0.41.1(Pi 0.84.4) withQwen3-Embedding-8Bvia openai-compatible:no dimensions→ 768 (en) / 1024 (zh) dimsdimensions:4096→ 16384 bytes / 4096 dims (memory 2 → 0f395...), verified viamemory_embeddingsandembedding_registrationsdoctorpasses;build-schemaguard would pass after regeneration
Checklist
- Follows
truncatefold pattern (conditional spread, no global re-embed when omitted) - Updates
EMBEDDING_AFFECTING_KEYSfor untrusted-load GC guard - Regenerates
assets/magic-context.schema.json
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.
Summary by cubic
Fixes #405 by adding optional embedding.dimensions for the openai-compatible provider so Matryoshka models like Qwen3-Embedding-8B can be pinned to 768/1024/2048/4096. Omitting it keeps the current byte-identical identity; setting it sends dimensions in the POST /v1/embeddings body and folds it into the chunk identity, so changing it re-embeds existing vectors. The synapse fallback also carries dimensions so fallback requests and identities use the configured value instead of the provider default.
- Validates
dimensionsin the config schema with a max of 8192. - Adds
embedding.dimensionstoEMBEDDING_AFFECTING_KEYSso config changes trip the GC guard on untrusted loads. - Regenerates
assets/magic-context.schema.json.
Greptile Summary
The PR adds configurable output dimensions for OpenAI-compatible Matryoshka embedding models while preserving the existing provider-default behavior when omitted.
- Adds schema validation and generated JSON Schema coverage for
embedding.dimensions. - Includes configured dimensions in embedding identities so vector widths can coexist safely.
- Propagates dimensions through direct and Synapse fallback routing into OpenAI-compatible embedding requests.
- Treats dimensions as embedding-affecting configuration for registry and garbage-collection safeguards.
Confidence Score: 5/5
The PR appears safe to merge.
No blocking failure remains.
Important Files Changed
| Filename | Overview |
|---|---|
| packages/plugin/src/config/schema/magic-context.ts | Adds bounded positive-integer validation and preserves dimensions for OpenAI-compatible and Synapse configurations. |
| packages/plugin/src/plugin/embedding-routing.ts | Completes the prior fix by carrying Synapse dimensions into OpenAI-compatible fallback configurations. |
| packages/plugin/src/features/magic-context/memory/embedding-openai.ts | Normalizes dimensions, includes them in provider identity, and sends them in embedding requests. |
| packages/plugin/src/features/magic-context/memory/embedding-identity.ts | Incorporates explicitly configured dimensions into OpenAI-compatible provider identities. |
| packages/plugin/src/features/magic-context/project-embedding-registry.ts | Preserves dimensions during registry resolution and includes them in chunk-model identity. |
| packages/plugin/src/plugin/embedding-bootstrap-helpers.ts | Marks dimensions as embedding-affecting configuration for guarded registry updates. |
| assets/magic-context.schema.json | Publishes the new optional dimensions field with the same validation constraints as the source schema. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart LR
Config[Embedding configuration] --> Schema[Schema validation]
Schema --> Routing{Provider routing}
Routing -->|OpenAI-compatible| Resolve[Resolved embedding config]
Routing -->|Synapse fallback| Fallback[OpenAI-compatible fallback config]
Fallback --> Resolve
Resolve --> Identity[Provider and chunk identities]
Resolve --> Provider[OpenAI-compatible provider]
Provider --> Request[POST /v1/embeddings with optional dimensions]
Reviews (2): Last reviewed commit: "fix(embedding): propagate dimensions thr..." | Re-trigger Greptile
Context used:
- Knowledge Base — Configuration and harness compatibility