From 5d9489178383e0a99a53f8bbb07943827fc1b065 Mon Sep 17 00:00:00 2001 From: DK09876 Date: Wed, 3 Jun 2026 18:02:25 -0700 Subject: [PATCH] cookbook(llamaindex): pass memory= to agent.run(), not ReActAgent ctor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pattern 1 in 08-llamaindex-react-agent.ipynb broke under LlamaIndex 0.14.x. Under LlamaIndex <= 0.12, `llama_index.core.agent.ReActAgent` was the legacy class whose constructor accepted `memory=BaseMemory`. The original cookbook used `ReActAgent(tools=[], llm=..., memory=memory, ...)`. LlamaIndex 0.14 routed `llama_index.core.agent.ReActAgent` to the new workflow-based class (`llama_index.core.agent.workflow.react_agent. ReActAgent`). Its constructor has no `memory` field; the kwarg gets silently swallowed by the trailing `kwargs: Any` and ignored. End-user symptom: cell 7 of the cookbook (the cross-session recall demo) returned "I don't have access to that information" (or, for some runs, the same in Spanish) even though cell 5 supposedly stored Alice's facts. The HindsightMemory hooks never fired because memory wasn't actually attached to the agent — `aput`/`aget` were never called. Workflow agents accept `memory=` as a kwarg to `run()` instead. The fix splits create_memory_agent's return into `(agent, memory)` and passes `memory=memory` to `agent.run(...)` in cells 5 and 7. Verified against the canonical PR #1867 build of hindsight_llamaindex: cell 7 now returns the recalled facts ("You use VS Code as your IDE", etc.) as the cookbook narrative claims. These three changes existed on cookbook/llamaindex-react-agent (commit 668fc27) but were never merged back to main after PR #18. This PR brings them in. Co-Authored-By: Claude Opus 4.7 (1M context) --- notebooks/08-llamaindex-react-agent.ipynb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/notebooks/08-llamaindex-react-agent.ipynb b/notebooks/08-llamaindex-react-agent.ipynb index 1e15597..c046537 100644 --- a/notebooks/08-llamaindex-react-agent.ipynb +++ b/notebooks/08-llamaindex-react-agent.ipynb @@ -123,7 +123,7 @@ "from llama_index.core.agent import ReActAgent\n", "\n", "\n", - "def create_memory_agent(user_id: str) -> ReActAgent:\n", + "def create_memory_agent(user_id: str) -> tuple[ReActAgent, HindsightMemory]:\n", " \"\"\"Create a ReAct agent with automatic per-user memory.\"\"\"\n", " memory = HindsightMemory.from_client(\n", " client=client,\n", @@ -133,13 +133,14 @@ " context=\"llamaindex-cookbook\",\n", " )\n", "\n", - " return ReActAgent(\n", + " agent = ReActAgent(\n", " tools=[], # No memory tools needed — memory is automatic\n", " llm=OpenAI(model=\"gpt-4o-mini\"),\n", - " memory=memory,\n", " system_prompt=\"You are a helpful assistant. Answer questions using your memory of past conversations.\",\n", " verbose=True,\n", - " )" + " )\n", + "\n", + " return agent, memory" ] }, { @@ -172,10 +173,11 @@ "metadata": {}, "outputs": [], "source": [ - "agent = create_memory_agent(\"alice\")\n", + "agent, memory = create_memory_agent(\"alice\")\n", "response = await agent.run(\n", " \"Hi! I'm Alice. I'm a data scientist who works with Python and SQL. \"\n", " \"I prefer dark mode and use VS Code.\",\n", + " memory=memory,\n", " max_iterations=10,\n", ")\n", "print(f\"\\nAgent: {response}\")" @@ -210,8 +212,8 @@ "metadata": {}, "outputs": [], "source": [ - "agent = create_memory_agent(\"alice\")\n", - "response = await agent.run(\"What IDE do I use? And what's my job?\", max_iterations=10)\n", + "agent, memory = create_memory_agent(\"alice\")\n", + "response = await agent.run(\"What IDE do I use? And what's my job?\", memory=memory, max_iterations=10)\n", "print(f\"\\nAgent: {response}\")" ] },