Skip to content

cookbook(llamaindex): pass memory= to agent.run(), not ReActAgent constructor#39

Open
DK09876 wants to merge 1 commit into
mainfrom
cookbook/llamaindex-pattern1-workflow-fix
Open

cookbook(llamaindex): pass memory= to agent.run(), not ReActAgent constructor#39
DK09876 wants to merge 1 commit into
mainfrom
cookbook/llamaindex-pattern1-workflow-fix

Conversation

@DK09876

@DK09876 DK09876 commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Brings the memory= fix from cookbook/llamaindex-react-agent (commit 668fc27) into main as a fresh PR. The fix existed but was never merged back after PR #18 landed.

Why this is needed

Pattern 1 (HindsightMemory as a drop-in BaseMemory) silently broke in the cookbook 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 workflow-based class. Its constructor has no memory field; the kwarg is silently swallowed by trailing **kwargs: Any and ignored.

End-user symptom: cell 7 (the cross-session recall demo) returned "I don't have access to that information" (or 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. This PR splits create_memory_agent to return (agent, memory) and threads memory=memory through agent.run(...) in cells 5 and 7.

Changes

  • Cell 3: create_memory_agent returns (ReActAgent, HindsightMemory) instead of just ReActAgent. Drops memory= from the ReActAgent(...) ctor call.
  • Cell 5: agent, memory = create_memory_agent("alice") then agent.run("...", memory=memory, max_iterations=10).
  • Cell 7: same change for the cross-session recall.

Total: 1 file, 9 insertions, 7 deletions.

Relationship to other PRs

Test plan

  • Cookbook 14/14 cells execute against canonical PR #1867 build of hindsight_llamaindex, 0 errors.
  • Cell 7 (Pattern 1 cross-session) now returns the recalled facts (verbatim "VS Code as your IDE" surfaced) instead of "I don't have access".
  • Pattern 2 (HindsightToolSpec, cell 23) and Pattern 3 (factory tools, cell 26) still pass.
  • Reviewer to verify against published hindsight-llamaindex once #1867 is merged.

🤖 Generated with Claude Code

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) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant