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(ollama): json format mutates caller's messages list in-place (#5539)

Hrushikesh Yadav9f34e858c7d4
diff --git a/mem0/llms/ollama.py b/mem0/llms/ollama.py
index 74d59c0d1..af976dc24 100644
--- a/mem0/llms/ollama.py
+++ b/mem0/llms/ollama.py
@@ -119,11 +119,12 @@ class OllamaLLM(LLMBase):
# Handle JSON response format by using Ollama's native format parameter
if response_format and response_format.get("type") == "json_object":
params["format"] = "json"
- # Also add JSON format instruction to the last message as a fallback
+ messages = [dict(m) for m in messages]
if messages and messages[-1]["role"] == "user":
messages[-1]["content"] += "\n\nPlease respond with valid JSON only."
else:
messages.append({"role": "user", "content": "Please respond with valid JSON only."})
+ params["messages"] = messages
# Add options for Ollama (temperature, num_predict, top_p)
options = {