🐛 Bug: State does not persist between calls with pydanticAI
♻️ Reproduction Steps
- run the example from https://github.com/CopilotKit/with-pydantic-ai
- remove the "IMPORTANT" instructions from the @agent.instructions in agent.py
- try sending any message, to the chatbot on the client, the list of proverbs gets emptied automatically
✅ Expected Behavior
The state should be maintained between messages. (the list of proverbs should stay intact)
❌ Actual Behavior
After every message, if the llm does not use a tool that returns a StateSnapshotEvent, the state does not persist.
The with-pydantic example clearly tried to work around it by prompting the llm in the system message to "display_proverbs" after every message, because that tool returns a StateSnapshotEvent that maintains the state. There shouldn't be a need to return StateSnapshotEvent for the state to persist
𝌚 CopilotKit Version
├── @copilotkit/[email protected]
├── @copilotkit/[email protected]
├── @copilotkit/[email protected]
📄 Logs (Optional)
State persistence in pydanticAI is automatic and does not require returning a StateSnapshotEvent. The state is managed by the LangGraph workflow, where each node's return value becomes the new state, and only what is explicitly returned is persisted.
How State is Managed
- Pydantic-AI agents run inside a LangGraph workflow.
- Every node (tool, chat step, etc.) returns either:
- a plain Python value → the whole value becomes the new state, or
Command(goto=…, update={…})→ the key/value pairs in the update field are merged into the existing state.
- The graph’s check-pointer (e.g.,
MemorySaver) stores that state, so when the next request arrives, the agent starts with the previously saved data. - Therefore, state persistence is automatic; it does not depend on emitting any special event.
“When a function in your Pydantic AI agent finishes executing, its returned state becomes the single source of truth … any changes you want to persist must be explicitly included in the function’s final returned state.”
Source: CopilotKit Docs
What StateSnapshotEvent Is For
- It is a CopilotKit streaming event that lets you push a snapshot of the current (often still-in-progress) state to the UI while the function is running.
- Useful for “predictive state updates” (showing progress bars, partial results, etc.).
- It has no influence on what the backend finally stores; if the subsequent function return does not include the same data, the snapshot will be overwritten.
- Example emitting a StateSnapshotEvent
Why State Might Seem Lost
- If you return a value that does not include the field you expected to keep, that field is intentionally dropped when the node ends.
- If you only looked at the UI during the node’s execution, no snapshot event means the UI simply doesn’t receive intermediate updates, giving the impression that the state isn’t there yet.
How the “with-pydantic-ai” Example Works
- The agent defines a Pydantic model
AgentStatethat inheritsCopilotKitState. - Each node returns a
Command(…, update={...}); those updates are merged and persisted (noStateSnapshotEventused). - The frontend reads the persisted state with
useCoAgent/useCoAgentStateRender.
Conclusion
- You do not need
StateSnapshotEventfor persistence. - Use it only when you want to stream live (“predictive”) state to the UI while a node is still running; otherwise, just return the updated state from your tool or
Command.
Was this helpful?
If this solution worked for you, please click on the appropriate option below to help us improve:
Hi @malek-jerbi, thanks for flagging this. I'll pass this on to the team and get an answer for you.
@malek-jerbi, we are working on getting this fixed.
We can keep you updated.
👋 Thanks for your feedback!
I've escalated this issue to our development team. Someone from our team will review your request and provide additional assistance shortly.
If this is blocking production, please book a meeting with us at https://cal.com/nathan-tarbert-copilotkit/15min
Hello, I am facing this issue too. Is this a bug on copilotkit side or pydantic AI ag-ui implementation? Could you please provide more details of why is this happening? Are there any workarounds?
Hey @maxkorp, would you happen to have any insights here?
State persistence between calls with PydanticAI was a real bug that's been fixed in the copilotkit Python SDK. Upgrade to latest.