fix(embeddings/lmstudio): honor LMSTUDIO_BASE_URL env var

BaseEmbedderConfig baked "http://localhost:1234/v1" into the
lmstudio_base_url default, so the attribute was never falsy and the
embedder had no way to tell "unset" from "explicitly localhost". Setting
LMSTUDIO_BASE_URL had no effect.

The default moves to the provider, which now resolves explicit config,
then LMSTUDIO_BASE_URL, then the localhost default, matching how the
OpenAI embedder already reads OPENAI_BASE_URL. The resolved value is
written back to config.lmstudio_base_url, so the attribute still reads
the same after construction.

Closes #6692
This commit is contained in:
kartik-mem0
2026-08-13 17:36:59 +05:30
parent 96d45b78c7
commit 08e9cc476c
3 changed files with 35 additions and 3 deletions
+28 -1
View File
@@ -6,7 +6,6 @@ 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:
@@ -80,3 +79,31 @@ 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"