Skip to content

Commit bc4a173

Browse files
authored
fix: preserve graph-owned state keys during frontend state merge (CopilotKit#3852)
## Summary - During frontend state merge in LangGraph agent, preserve keys that are owned by the graph (not set by the frontend) - Prevents graph-computed state from being overwritten when frontend state is merged back Closes CopilotKit#2893 --- *Split from CopilotKit#3847*
2 parents de78dfd + 35ee689 commit bc4a173

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

sdk-python/copilotkit/langgraph_agent.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,12 @@ async def prepare_stream( # pylint: disable=too-many-arguments
267267
actions=actions,
268268
agent_name=self.name
269269
)
270-
current_graph_state.update(state)
270+
# Only update graph state with keys that merge_state explicitly produced,
271+
# not keys that were simply passed through from state_input unchanged.
272+
# This preserves graph-owned state keys that the frontend may have sent stale values for.
273+
for key, value in state.items():
274+
if key not in state_input or value is not state_input.get(key):
275+
current_graph_state[key] = value
271276
lg_interrupt_meta_event = next((ev for ev in (meta_events or []) if ev.get("name") == "LangGraphInterruptEvent"), None)
272277
has_active_interrupts = active_interrupts is not None and len(active_interrupts) > 0
273278

0 commit comments

Comments
 (0)