Repository navigation
Conversation
⏭️ Changeset Not RequiredLatest commit: e231658 No changes in this PR affected the Changeset is not required for this PR. |
WalkthroughUpdated sdk-python/copilotkit/langgraph_agent.py to persist thread-local state by synchronizing thread_state with current_graph_state at three update points. No changes to public APIs. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant C as Client
participant A as LangGraphAgent
participant S as current_graph_state
participant T as thread_state (per-thread)
C->>A: invoke()
A->>S: update state
A->>T: mirror S into T[thread_id]
Note right of A: Persist thread-local state for subsequent calls
C->>A: invoke() again
A->>T: read T[thread_id]
A->>S: initialize/merge from T
A-->>C: result
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested labels
Suggested reviewers
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests
Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Please see the documentation for more information. Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).Please share your feedback with us on this Discord post. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@eaocx is attempting to deploy a commit to the CopilotKit Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
sdk-python/copilotkit/langgraph_agent.py(3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
{sdk-python/**,sdk-python/copilotkit/crewai/**,sdk-python/copilotkit/openai/**}
📄 CodeRabbit inference engine (.cursor/rules/copilotkit-architecture.mdc)
Python SDK and its integrations should be implemented in sdk-python/, with CrewAI agents in sdk-python/copilotkit/crewai/ and OpenAI integrations in sdk-python/copilotkit/openai/
Files:
sdk-python/copilotkit/langgraph_agent.py
| current_graph_state.update(state) | ||
| self.thread_state[thread_id] = current_graph_state | ||
| lg_interrupt_meta_event = next((ev for ev in (meta_events or []) if ev.get("name") == "LangGraphInterruptEvent"), None) | ||
| has_active_interrupts = active_interrupts is not None and len(active_interrupts) > 0 | ||
|
|
There was a problem hiding this comment.
Fix thread_state assignment before generating a new thread_id
We’re storing current_graph_state under the pre-normalized thread_id, but a few lines later we replace a falsey thread_id with a freshly generated UUID. For first-time threads this writes the state under "", while the rest of the flow (and get_state) looks up the newly generated UUID, so the persisted state is never found. Move the assignment to occur after the UUID fallback to keep the keys aligned.
- current_graph_state.update(state)
- self.thread_state[thread_id] = current_graph_state
+ current_graph_state.update(state)
lg_interrupt_meta_event = next((ev for ev in (meta_events or []) if ev.get("name") == "LangGraphInterruptEvent"), None)
@@
- thread_id = thread_id or str(uuid.uuid4())
- config["configurable"]["thread_id"] = thread_id
+ thread_id = thread_id or str(uuid.uuid4())
+ config["configurable"]["thread_id"] = thread_id
+ self.thread_state[thread_id] = current_graph_state📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| current_graph_state.update(state) | |
| self.thread_state[thread_id] = current_graph_state | |
| lg_interrupt_meta_event = next((ev for ev in (meta_events or []) if ev.get("name") == "LangGraphInterruptEvent"), None) | |
| has_active_interrupts = active_interrupts is not None and len(active_interrupts) > 0 | |
| # update the graph state first | |
| current_graph_state.update(state) | |
| # only assign after we’ve ensured thread_id is finalized | |
| lg_interrupt_meta_event = next( | |
| (ev for ev in (meta_events or []) if ev.get("name") == "LangGraphInterruptEvent"), | |
| None | |
| ) | |
| has_active_interrupts = active_interrupts is not None and len(active_interrupts) > 0 | |
| # ensure we have a real thread_id before persisting state | |
| thread_id = thread_id or str(uuid.uuid4()) | |
| config["configurable"]["thread_id"] = thread_id | |
| self.thread_state[thread_id] = current_graph_state |
🤖 Prompt for AI Agents
In sdk-python/copilotkit/langgraph_agent.py around lines 232 to 236, the code
assigns current_graph_state to self.thread_state using the original (possibly
falsey) thread_id before the code later generates a new UUID fallback; move the
self.thread_state[thread_id] = current_graph_state assignment to after the logic
that sets thread_id = thread_id or new_uuid so that the state is stored under
the normalized (non-empty) thread_id used elsewhere (and ensure thread_id is
used consistently thereafter).
|
Nice! |
|
@ranst91 Any chance you or one of the other maintainers take a look at this? |
What does this PR do?
Updates
LangGraphAgent.thread_stateanytimecurrent_graph_stateis updated. This allows thethread_stateto be persisted for eachthread_idas a chat is occurring.Related PRs and Issues
Checklist
Summary by CodeRabbit