Skip to content

armgit / mem0

Public Imported from GitHub
0

The Memory Layer for AI Agents - Drop-in memory infrastructure for AI agents and apps. Context that persists. Built for production.

fix(deepseek): forward response_format to OpenAI-compatible API (#4635)

Rakhee Singh93bd4e248c8d
diff --git a/mem0/llms/deepseek.py b/mem0/llms/deepseek.py
index a9877065e..b6b5e4a86 100644
--- a/mem0/llms/deepseek.py
+++ b/mem0/llms/deepseek.py
@@ -99,6 +99,8 @@ class DeepSeekLLM(LLMBase):
}
)
+ if response_format:
+ params["response_format"] = response_format
if tools:
params["tools"] = tools
params["tool_choice"] = tool_choice
diff --git a/tests/llms/test_deepseek.py b/tests/llms/test_deepseek.py
index 4a84079a1..220cb481f 100644
--- a/tests/llms/test_deepseek.py
+++ b/tests/llms/test_deepseek.py
@@ -113,3 +113,47 @@ def test_generate_response_with_tools(mock_deepseek_client):
assert len(response["tool_calls"]) == 1
assert response["tool_calls"][0]["name"] == "add_memory"
assert response["tool_calls"][0]["arguments"] == {"data": "Today is a sunny day."}
+
+
+def test_generate_response_with_response_format(mock_deepseek_client):
+ config = BaseLlmConfig(model="deepseek-chat", temperature=0.7, max_tokens=100, top_p=1.0)
+ llm = DeepSeekLLM(config)
+ messages = [
+ {"role": "system", "content": "You are a memory extraction assistant."},
+ {"role": "user", "content": "I like hiking on weekends."},
+ ]
+
+ mock_response = Mock()
+ mock_response.choices = [Mock(message=Mock(content='{"facts": ["User likes hiking on weekends"]}'))]
+ mock_deepseek_client.chat.completions.create.return_value = mock_response
+
+ response = llm.generate_response(messages, response_format={"type": "json_object"})
+
+ mock_deepseek_client.chat.completions.create.assert_called_once_with(
+ model="deepseek-chat",
+ messages=messages,
+ temperature=0.7,
+ max_tokens=100,
+ top_p=1.0,
+ response_format={"type": "json_object"},
+ )
+ assert response == '{"facts": ["User likes hiking on weekends"]}'
+
+
+def test_generate_response_without_response_format(mock_deepseek_client):
+ config = BaseLlmConfig(model="deepseek-chat", temperature=0.7, max_tokens=100, top_p=1.0)
+ llm = DeepSeekLLM(config)
+ messages = [
+ {"role": "system", "content": "You are a helpful assistant."},
+ {"role": "user", "content": "Tell me a joke."},
+ ]
+
+ mock_response = Mock()
+ mock_response.choices = [Mock(message=Mock(content="Why did the chicken cross the road?"))]
+ mock_deepseek_client.chat.completions.create.return_value = mock_response
+
+ response = llm.generate_response(messages)
+
+ call_kwargs = mock_deepseek_client.chat.completions.create.call_args[1]
+ assert "response_format" not in call_kwargs
+ assert response == "Why did the chicken cross the road?"