cookbook(llamaindex): pass memory= to agent.run(), not ReActAgent constructor#39
Open
DK09876 wants to merge 1 commit into
Open
cookbook(llamaindex): pass memory= to agent.run(), not ReActAgent constructor#39DK09876 wants to merge 1 commit into
DK09876 wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Brings the
memory=fix fromcookbook/llamaindex-react-agent(commit668fc27) 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:
llama_index.core.agent.ReActAgentwas the legacy class whose constructor acceptedmemory=BaseMemory. The original cookbook usedReActAgent(tools=[], llm=..., memory=memory, ...).llama_index.core.agent.ReActAgentto the workflow-based class. Its constructor has nomemoryfield; the kwarg is silently swallowed by trailing**kwargs: Anyand 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/agetwere never called.Workflow agents accept
memory=as a kwarg torun()instead. This PR splitscreate_memory_agentto return(agent, memory)and threadsmemory=memorythroughagent.run(...)in cells 5 and 7.Changes
create_memory_agentreturns(ReActAgent, HindsightMemory)instead of justReActAgent. Dropsmemory=from theReActAgent(...)ctor call.agent, memory = create_memory_agent("alice")thenagent.run("...", memory=memory, max_iterations=10).Total: 1 file, 9 insertions, 7 deletions.
Relationship to other PRs
47bcac45(HindsightMemory.aget falls back to last user message when input=None). Both layers need to be in place for end-user UX to work end-to-end.cookbook/llamaindex-react-agent(7 commits ahead of main, including668fc27 fix: pass memory to agent.run() instead of constructor).Test plan
🤖 Generated with Claude Code