Compare commits

..
Author SHA1 Message Date
kartik-mem0 00354cc74d Merge branch 'main' into fix/ts-oss-camelcase-filters 2026-08-14 17:13:37 +05:30
kartik-mem0 ac76eb7568 fix(ts-oss): accept camelCase entity ids inside search/getAll filters
Both methods reject top-level entity params with "Use filters: { userId:
"..." } instead", and docs/open-source/node-quickstart.mdx teaches that same
shape. But search() and getAll() only read filters.user_id / .agent_id /
.run_id, so following either one throws a second, contradictory error:
"filters must contain at least one of: user_id, agent_id, run_id".

Normalize the three camelCase aliases to snake_case before validation in both
methods, so the shape the SDK tells callers to use actually works. An explicit
snake_case value wins if both are supplied, and the camelCase key is dropped
rather than passed through to the vector store as a stray metadata field.
2026-08-13 18:24:40 +05:30
12 changed files with 111 additions and 192 deletions
+1 -5
View File
@@ -12,11 +12,7 @@ export class LMStudioEmbedder implements Embedder {
private model: string;
constructor(config: EmbeddingConfig) {
const baseURL =
config.baseURL ??
config.url ??
process.env.LMSTUDIO_BASE_URL ??
DEFAULT_BASE_URL;
const baseURL = config.baseURL ?? config.url ?? DEFAULT_BASE_URL;
const apiKey = config.apiKey || DEFAULT_LMSTUDIO_API_KEY;
this.openai = new OpenAI({ apiKey, baseURL: String(baseURL) });
this.model = config.model || DEFAULT_MODEL;
+1 -2
View File
@@ -12,8 +12,7 @@ export class LMStudioLLM extends OpenAILLM {
super({
...config,
apiKey: config.apiKey || DEFAULT_LMSTUDIO_API_KEY,
baseURL:
config.baseURL ?? process.env.LMSTUDIO_BASE_URL ?? DEFAULT_BASE_URL,
baseURL: config.baseURL ?? DEFAULT_BASE_URL,
model: config.model || DEFAULT_MODEL,
});
}
+37 -8
View File
@@ -149,6 +149,27 @@ function rejectTopLevelEntityParams(
}
}
const ENTITY_ID_ALIASES: Record<string, string> = {
userId: "user_id",
agentId: "agent_id",
runId: "run_id",
};
/** Rewrites camelCase entity ids inside filters to the snake_case keys vector stores index on. */
function normalizeEntityFilterKeys(
filters: Record<string, any>,
): Record<string, any> {
const normalized = { ...filters };
for (const [camelKey, snakeKey] of Object.entries(ENTITY_ID_ALIASES)) {
if (!(camelKey in normalized)) continue;
if (normalized[snakeKey] === undefined) {
normalized[snakeKey] = normalized[camelKey];
}
delete normalized[camelKey];
}
return normalized;
}
/**
* Validates and normalizes an entity ID.
* - Coerces non-string ids (e.g. numeric database keys) to string
@@ -1357,16 +1378,20 @@ export class Memory {
// receive `agent_id: undefined` / `run_id: undefined` and fail
// (Qdrant rejects the malformed match, pgvector binds NULL, Redis
// emits a literal "undefined" string in TAG filters).
const requestedFilters = normalizeEntityFilterKeys(config.filters ?? {});
const normalizedFilters: Record<string, any> = config.filters
? Object.fromEntries(
Object.entries({
...config.filters,
user_id: validateAndTrimEntityId(config.filters.user_id, "user_id"),
...requestedFilters,
user_id: validateAndTrimEntityId(
requestedFilters.user_id,
"user_id",
),
agent_id: validateAndTrimEntityId(
config.filters.agent_id,
requestedFilters.agent_id,
"agent_id",
),
run_id: validateAndTrimEntityId(config.filters.run_id, "run_id"),
run_id: validateAndTrimEntityId(requestedFilters.run_id, "run_id"),
}).filter(([, v]) => v !== undefined),
)
: {};
@@ -1853,12 +1878,16 @@ export class Memory {
// Validate and trim entity IDs in filters. Drop keys that resolve to
// undefined so downstream vector stores don't receive
// `agent_id: undefined` / `run_id: undefined` and fail.
const requestedFilters = normalizeEntityFilterKeys(config.filters || {});
const filters: Record<string, any> = Object.fromEntries(
Object.entries({
...(config.filters || {}),
user_id: validateAndTrimEntityId(config.filters?.user_id, "user_id"),
agent_id: validateAndTrimEntityId(config.filters?.agent_id, "agent_id"),
run_id: validateAndTrimEntityId(config.filters?.run_id, "run_id"),
...requestedFilters,
user_id: validateAndTrimEntityId(requestedFilters.user_id, "user_id"),
agent_id: validateAndTrimEntityId(
requestedFilters.agent_id,
"agent_id",
),
run_id: validateAndTrimEntityId(requestedFilters.run_id, "run_id"),
}).filter(([, v]) => v !== undefined),
);
@@ -10,64 +10,14 @@ const mockCreate = jest.fn().mockResolvedValue({
data: [{ embedding: mockEmbedding }],
});
let capturedConstructorArgs: any;
jest.mock("openai", () => {
return jest.fn().mockImplementation((args: any) => {
capturedConstructorArgs = args;
return {
embeddings: { create: mockCreate },
};
});
return jest.fn().mockImplementation(() => ({
embeddings: { create: mockCreate },
}));
});
describe("LMStudioEmbedder (unit)", () => {
beforeEach(() => {
mockCreate.mockClear();
capturedConstructorArgs = undefined;
delete process.env.LMSTUDIO_BASE_URL;
});
afterEach(() => {
delete process.env.LMSTUDIO_BASE_URL;
});
it("uses the default base URL when neither config nor env var is set", async () => {
const embedder = new LMStudioEmbedder({ model: "test-model" });
await embedder.embed("hello");
expect(capturedConstructorArgs).toMatchObject({
baseURL: "http://localhost:1234/v1",
});
});
it("uses LMSTUDIO_BASE_URL when config does not provide a base URL", async () => {
process.env.LMSTUDIO_BASE_URL = "http://example.test:5678/v1";
const embedder = new LMStudioEmbedder({ model: "test-model" });
await embedder.embed("hello");
expect(capturedConstructorArgs).toMatchObject({
baseURL: "http://example.test:5678/v1",
});
});
it("prefers an explicit config baseURL over LMSTUDIO_BASE_URL", async () => {
process.env.LMSTUDIO_BASE_URL = "http://example.test:5678/v1";
const embedder = new LMStudioEmbedder({
model: "test-model",
baseURL: "http://config.test:9999/v1",
});
await embedder.embed("hello");
expect(capturedConstructorArgs).toMatchObject({
baseURL: "http://config.test:9999/v1",
});
});
beforeEach(() => mockCreate.mockClear());
it("embed() calls OpenAI with encoding_format float and returns vector", async () => {
const embedder = new LMStudioEmbedder({
+4 -44
View File
@@ -5,56 +5,16 @@
import { LMStudioLLM } from "../src/llms/lmstudio";
let capturedConstructorArgs: any;
const mockCreate = jest.fn();
jest.mock("openai", () => {
return jest.fn().mockImplementation((args: any) => {
capturedConstructorArgs = args;
return {
chat: { completions: { create: mockCreate } },
};
});
return jest.fn().mockImplementation(() => ({
chat: { completions: { create: mockCreate } },
}));
});
describe("LMStudioLLM (unit)", () => {
beforeEach(() => {
mockCreate.mockClear();
capturedConstructorArgs = undefined;
delete process.env.LMSTUDIO_BASE_URL;
});
afterEach(() => {
delete process.env.LMSTUDIO_BASE_URL;
});
it("uses the default base URL when neither config nor env var is set", () => {
new LMStudioLLM({});
expect(capturedConstructorArgs).toMatchObject({
baseURL: "http://localhost:1234/v1",
});
});
it("uses LMSTUDIO_BASE_URL when config does not provide a base URL", () => {
process.env.LMSTUDIO_BASE_URL = "http://example.test:5678/v1";
new LMStudioLLM({});
expect(capturedConstructorArgs).toMatchObject({
baseURL: "http://example.test:5678/v1",
});
});
it("prefers an explicit config baseURL over LMSTUDIO_BASE_URL", () => {
process.env.LMSTUDIO_BASE_URL = "http://example.test:5678/v1";
new LMStudioLLM({ baseURL: "http://config.test:9999/v1" });
expect(capturedConstructorArgs).toMatchObject({
baseURL: "http://config.test:9999/v1",
});
});
beforeEach(() => mockCreate.mockClear());
it("generateResponse() returns a text response", async () => {
mockCreate.mockResolvedValueOnce({
@@ -281,6 +281,66 @@ describe("Memory Input Validation", () => {
});
});
describe("camelCase entity IDs in filters", () => {
it("maps camelCase filter keys to snake_case in search", async () => {
const searchSpy = jest
.spyOn((memory as any).vectorStore, "search")
.mockResolvedValue([]);
await memory.search("q", {
filters: { userId: "alice", agentId: "bot", runId: "run-1" },
});
const passedFilters = searchSpy.mock.calls[0][2] as Record<string, any>;
expect(passedFilters).toMatchObject({
user_id: "alice",
agent_id: "bot",
run_id: "run-1",
});
expect(passedFilters.userId).toBeUndefined();
expect(passedFilters.agentId).toBeUndefined();
expect(passedFilters.runId).toBeUndefined();
searchSpy.mockRestore();
});
it("maps camelCase filter keys to snake_case in getAll", async () => {
const listSpy = jest
.spyOn((memory as any).vectorStore, "list")
.mockResolvedValue([[], 0]);
await memory.getAll({ filters: { userId: "alice" } });
const passedFilters = listSpy.mock.calls[0][0] as Record<string, any>;
expect(passedFilters.user_id).toBe("alice");
expect(passedFilters.userId).toBeUndefined();
listSpy.mockRestore();
});
it("prefers an explicit snake_case value over its camelCase alias", async () => {
const listSpy = jest
.spyOn((memory as any).vectorStore, "list")
.mockResolvedValue([[], 0]);
await memory.getAll({
filters: { user_id: "snake", userId: "camel" },
});
const passedFilters = listSpy.mock.calls[0][0] as Record<string, any>;
expect(passedFilters.user_id).toBe("snake");
expect(passedFilters.userId).toBeUndefined();
listSpy.mockRestore();
});
it("validates camelCase filter values like their snake_case counterparts", async () => {
await expect(
memory.search("q", { filters: { userId: "user 123" } }),
).rejects.toThrow("Invalid user_id: cannot contain whitespace");
});
});
describe("search() filter entity ID validation", () => {
it("should throw error when user_id in filters is whitespace-only", async () => {
await expect(
+2 -3
View File
@@ -34,7 +34,7 @@ class BaseEmbedderConfig(ABC):
# Gemini specific
output_dimensionality: Optional[str] = None,
# LM Studio specific
lmstudio_base_url: Optional[str] = None,
lmstudio_base_url: Optional[str] = "http://localhost:1234/v1",
# AWS Bedrock specific
aws_access_key_id: Optional[str] = None,
aws_secret_access_key: Optional[str] = None,
@@ -70,8 +70,7 @@ class BaseEmbedderConfig(ABC):
:type memory_update_embedding_type: Optional[str], optional
:param memory_search_embedding_type: The type of embedding to use for the search memory action, defaults to None
:type memory_search_embedding_type: Optional[str], optional
:param lmstudio_base_url: LM Studio base URL to be use, defaults to None
(resolved by the provider from LMSTUDIO_BASE_URL, then "http://localhost:1234/v1")
:param lmstudio_base_url: LM Studio base URL to be use, defaults to "http://localhost:1234/v1"
:type lmstudio_base_url: Optional[str], optional
"""
+1 -2
View File
@@ -39,7 +39,6 @@ class LMStudioConfig(BaseLlmConfig):
vision_details: Vision detail level, defaults to "auto"
http_client_proxies: HTTP client proxy settings, defaults to None
lmstudio_base_url: LM Studio base URL, defaults to None
(resolved by the provider from LMSTUDIO_BASE_URL, then "http://localhost:1234/v1")
lmstudio_response_format: LM Studio response format, defaults to None
"""
# Initialize base parameters
@@ -56,5 +55,5 @@ class LMStudioConfig(BaseLlmConfig):
)
# LM Studio-specific parameters
self.lmstudio_base_url = lmstudio_base_url
self.lmstudio_base_url = lmstudio_base_url or "http://localhost:1234/v1"
self.lmstudio_response_format = lmstudio_response_format
-4
View File
@@ -1,4 +1,3 @@
import os
from typing import Literal, Optional
from openai import OpenAI
@@ -14,9 +13,6 @@ class LMStudioEmbedding(EmbeddingBase):
self.config.model = self.config.model or "nomic-ai/nomic-embed-text-v1.5-GGUF/nomic-embed-text-v1.5.f16.gguf"
self.config.embedding_dims = self.config.embedding_dims or 1536
self.config.api_key = self.config.api_key or "lm-studio"
self.config.lmstudio_base_url = (
self.config.lmstudio_base_url or os.getenv("LMSTUDIO_BASE_URL") or "http://localhost:1234/v1"
)
self.client = OpenAI(base_url=self.config.lmstudio_base_url, api_key=self.config.api_key)
-4
View File
@@ -1,5 +1,4 @@
import json
import os
from typing import Dict, List, Optional, Union
from openai import OpenAI
@@ -38,9 +37,6 @@ class LMStudioLLM(LLMBase):
or "lmstudio-community/Meta-Llama-3.1-70B-Instruct-GGUF/Meta-Llama-3.1-70B-Instruct-IQ2_M.gguf"
)
self.config.api_key = self.config.api_key or "lm-studio"
self.config.lmstudio_base_url = (
self.config.lmstudio_base_url or os.getenv("LMSTUDIO_BASE_URL") or "http://localhost:1234/v1"
)
self.client = OpenAI(base_url=self.config.lmstudio_base_url, api_key=self.config.api_key)
+1 -28
View File
@@ -6,6 +6,7 @@ from mem0.configs.embeddings.base import BaseEmbedderConfig
from mem0.embeddings.lmstudio import LMStudioEmbedding
@pytest.fixture
def mock_lm_studio_client():
with patch("mem0.embeddings.lmstudio.OpenAI") as mock_openai:
@@ -79,31 +80,3 @@ def test_embed_batch_count_mismatch_raises(mock_lm_studio_client):
with pytest.raises(ValueError, match="returned 1 embeddings for 2 texts"):
embedder.embed_batch(["first text", "second text"])
def test_base_url_honors_lmstudio_base_url_env(mock_lm_studio_client, monkeypatch):
monkeypatch.setenv("LMSTUDIO_BASE_URL", "http://lmstudio.internal:9000/v1")
with patch("mem0.embeddings.lmstudio.OpenAI") as mock_openai:
LMStudioEmbedding(BaseEmbedderConfig())
assert mock_openai.call_args.kwargs["base_url"] == "http://lmstudio.internal:9000/v1"
def test_base_url_prefers_explicit_config_over_env(mock_lm_studio_client, monkeypatch):
monkeypatch.setenv("LMSTUDIO_BASE_URL", "http://lmstudio.internal:9000/v1")
with patch("mem0.embeddings.lmstudio.OpenAI") as mock_openai:
LMStudioEmbedding(BaseEmbedderConfig(lmstudio_base_url="http://explicit:1234/v1"))
assert mock_openai.call_args.kwargs["base_url"] == "http://explicit:1234/v1"
def test_base_url_falls_back_to_localhost_default(mock_lm_studio_client, monkeypatch):
monkeypatch.delenv("LMSTUDIO_BASE_URL", raising=False)
with patch("mem0.embeddings.lmstudio.OpenAI") as mock_openai:
embedder = LMStudioEmbedding(BaseEmbedderConfig())
assert mock_openai.call_args.kwargs["base_url"] == "http://localhost:1234/v1"
assert embedder.config.lmstudio_base_url == "http://localhost:1234/v1"
-38
View File
@@ -2,7 +2,6 @@ from unittest.mock import Mock, patch
import pytest
from mem0.configs.llms.base import BaseLlmConfig
from mem0.configs.llms.lmstudio import LMStudioConfig
from mem0.llms.lmstudio import LMStudioLLM
@@ -71,40 +70,3 @@ def test_generate_response_specifying_response_format(mock_lm_studio_client):
)
assert response == "I'm doing well, thank you for asking!"
def test_base_url_honors_lmstudio_base_url_env(monkeypatch):
monkeypatch.setenv("LMSTUDIO_BASE_URL", "http://lmstudio.internal:9000/v1")
with patch("mem0.llms.lmstudio.OpenAI") as mock_openai:
LMStudioLLM(LMStudioConfig())
assert mock_openai.call_args.kwargs["base_url"] == "http://lmstudio.internal:9000/v1"
def test_base_url_prefers_explicit_config_over_env(monkeypatch):
monkeypatch.setenv("LMSTUDIO_BASE_URL", "http://lmstudio.internal:9000/v1")
with patch("mem0.llms.lmstudio.OpenAI") as mock_openai:
LMStudioLLM(LMStudioConfig(lmstudio_base_url="http://explicit:1234/v1"))
assert mock_openai.call_args.kwargs["base_url"] == "http://explicit:1234/v1"
def test_base_url_falls_back_to_localhost_default(monkeypatch):
monkeypatch.delenv("LMSTUDIO_BASE_URL", raising=False)
with patch("mem0.llms.lmstudio.OpenAI") as mock_openai:
llm = LMStudioLLM(LMStudioConfig())
assert mock_openai.call_args.kwargs["base_url"] == "http://localhost:1234/v1"
assert llm.config.lmstudio_base_url == "http://localhost:1234/v1"
def test_base_url_honors_env_via_base_llm_config(monkeypatch):
monkeypatch.setenv("LMSTUDIO_BASE_URL", "http://lmstudio.internal:9000/v1")
with patch("mem0.llms.lmstudio.OpenAI") as mock_openai:
LMStudioLLM(BaseLlmConfig(model="some-model"))
assert mock_openai.call_args.kwargs["base_url"] == "http://lmstudio.internal:9000/v1"