From 2b8e4ea059d8d202c6fafac3bc73eff46b170f6f Mon Sep 17 00:00:00 2001 From: kartik-mem0 Date: Thu, 13 Aug 2026 19:45:57 +0530 Subject: [PATCH] fix(llms/lmstudio): honor LMSTUDIO_BASE_URL in the LLM too The embedder fix in this branch left the sibling LLM still hardcoded, so a self-hosted LM Studio behind a non-default host worked for embeddings and silently fell back to localhost for generation. LMStudioConfig baked "http://localhost:1234/v1" into __init__, which makes any env-var lookup downstream dead code. The field now defaults to None and LMStudioLLM resolves config value, then LMSTUDIO_BASE_URL, then the localhost default, matching the embedder and DeepSeek. --- mem0/configs/llms/lmstudio.py | 3 ++- mem0/llms/lmstudio.py | 4 ++++ tests/llms/test_lm_studio.py | 38 +++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/mem0/configs/llms/lmstudio.py b/mem0/configs/llms/lmstudio.py index 64abdd502..4190aa1e5 100644 --- a/mem0/configs/llms/lmstudio.py +++ b/mem0/configs/llms/lmstudio.py @@ -39,6 +39,7 @@ 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 @@ -55,5 +56,5 @@ class LMStudioConfig(BaseLlmConfig): ) # LM Studio-specific parameters - self.lmstudio_base_url = lmstudio_base_url or "http://localhost:1234/v1" + self.lmstudio_base_url = lmstudio_base_url self.lmstudio_response_format = lmstudio_response_format diff --git a/mem0/llms/lmstudio.py b/mem0/llms/lmstudio.py index bc2d9cce4..7632bdceb 100644 --- a/mem0/llms/lmstudio.py +++ b/mem0/llms/lmstudio.py @@ -1,4 +1,5 @@ import json +import os from typing import Dict, List, Optional, Union from openai import OpenAI @@ -37,6 +38,9 @@ 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) diff --git a/tests/llms/test_lm_studio.py b/tests/llms/test_lm_studio.py index a6c956e64..3d59cd47e 100644 --- a/tests/llms/test_lm_studio.py +++ b/tests/llms/test_lm_studio.py @@ -2,6 +2,7 @@ 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 @@ -70,3 +71,40 @@ 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"