Skip to content

Bug: OpenAIEmbedder ignores embeddingDims config, doesn't pass dimensions to API #4615

@wobushixiaoj

Description

@wobushixiaoj

Description

The OpenAIEmbedder class stores embeddingDims from config but never passes it to the OpenAI embeddings.create() call. This causes dimension mismatch errors when using embedding models with configurable dimensions (e.g., text-embedding-v4 on DashScope/Aliyun).

Affected Code

File: mem0-ts/src/oss/src/embeddings/openai.ts

export class OpenAIEmbedder implements Embedder {
  private openai: OpenAI;
  private model: string;
  private embeddingDims?: number;  // ✅ Stored

  constructor(config: EmbeddingConfig) {
    this.openai = new OpenAI({
      apiKey: config.apiKey,
      baseURL: config.baseURL || config.url,
    });
    this.model = config.model || "text-embedding-3-small";
    this.embeddingDims = config.embeddingDims || 1536;  // ✅ Read from config
  }

  async embed(text: string): Promise<number[]> {
    const response = await this.openai.embeddings.create({
      model: this.model,
      input: text,
      // ❌ Missing: dimensions: this.embeddingDims
    });
    return response.data[0].embedding;
  }

  async embedBatch(texts: string[]): Promise<number[][]> {
    const response = await this.openai.embeddings.create({
      model: this.model,
      input: texts,
      // ❌ Missing: dimensions: this.embeddingDims
    });
    return response.data.map((item) => item.embedding);
  }
}

Expected Behavior

When user configures embeddingDims: 1536, the embedder should pass dimensions: 1536 to the API call, so models like text-embedding-v4 return 1536-dimensional vectors.

Actual Behavior

API returns model's default dimension (1024 for text-embedding-v4), causing Query dimension mismatch errors:

Memory store failed: Error: Query dimension mismatch. Expected 1536, got 1024

Reproduction

Using OpenClaw plugin with DashScope (Aliyun):

{
  "oss": {
    "embedder": {
      "provider": "openai",
      "config": {
        "model": "text-embedding-v4",
        "embeddingDims": 1536,
        "apiKey": "${DASHSCOPE_API_KEY}",
        "url": "https://dashscope.aliyuncs.com/compatible-mode/v1"
      }
    }
  }
}

Fix

Add dimensions parameter to both embed() and embedBatch():

async embed(text: string): Promise<number[]> {
  const response = await this.openai.embeddings.create({
    model: this.model,
    input: text,
    dimensions: this.embeddingDims,  // Add this
  });
  return response.data[0].embedding;
}

async embedBatch(texts: string[]): Promise<number[][]> {
  const response = await this.openai.embeddings.create({
    model: this.model,
    input: texts,
    dimensions: this.embeddingDims,  // Add this
  });
  return response.data.map((item) => item.embedding);
}

Environment

  • mem0ai: 2.4.4
  • Node.js: v25.8.1
  • Platform: macOS (Darwin 24.6.0 arm64)
  • Provider: DashScope (Aliyun) OpenAI-compatible API

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions