Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(langchain): align capture state typing and CI checks (#5717)

z1gon6e1030d9951c
diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml
index 5a6e6463a..f27e544c0 100644
--- a/.github/workflows/pr.yml
+++ b/.github/workflows/pr.yml
@@ -181,7 +181,7 @@ jobs:
echo "cuvs_changed=false" >> $GITHUB_OUTPUT
fi
- LANGCHAIN_PATTERN="examples/langchain/|openviking/integrations/langchain/|tests/(unit/test_langchain|integration/langchain_langgraph/)|examples/langchain-langgraph/|pyproject\.toml|uv\.lock|\.github/workflows/(pr|python-langchain-release)\.yml"
+ LANGCHAIN_PATTERN="examples/langchain/|openviking/integrations/langchain/|tests/(unit/test_langchain|integration/langchain_langgraph/)|examples/langchain-langgraph/|^pyproject\.toml$|^uv\.lock$|\.github/workflows/(pr|python-langchain-release)\.yml"
LANGCHAIN_CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }} HEAD | grep -E "$LANGCHAIN_PATTERN" || true)
if [ -n "$LANGCHAIN_CHANGED_FILES" ]; then
diff --git a/examples/langchain/src/langchain_openviking/middleware.py b/examples/langchain/src/langchain_openviking/middleware.py
index dfc167993..4ff9e0761 100644
--- a/examples/langchain/src/langchain_openviking/middleware.py
+++ b/examples/langchain/src/langchain_openviking/middleware.py
@@ -80,7 +80,7 @@ class OpenVikingCaptureState(AgentState):
openviking_captured_message_ids: dict[str, list[str]]
-class OpenVikingContextMiddleware(AgentMiddleware):
+class OpenVikingContextMiddleware(AgentMiddleware[OpenVikingCaptureState]):
"""Inject OpenViking recall into LangGraph agent model calls.
The middleware mirrors the OpenClaw-style lifecycle at LangGraph's extension
@@ -280,14 +280,18 @@ class OpenVikingContextMiddleware(AgentMiddleware):
updated_system = SystemMessage(content=f"{content}\n\n{context_block}".strip())
return request.override(system_message=updated_system)
- def before_model(self, state: AgentState[Any], runtime: Any) -> dict[str, Any] | None:
+ def before_model(self, state: OpenVikingCaptureState, runtime: Any) -> dict[str, Any] | None:
"""Place this middleware before the host summarizer to capture its input."""
return self._capture(state, runtime, commit=False)
- async def abefore_model(self, state: AgentState[Any], runtime: Any) -> dict[str, Any] | None:
+ async def abefore_model(
+ self, state: OpenVikingCaptureState, runtime: Any
+ ) -> dict[str, Any] | None:
return await self._acapture(state, runtime, commit=False)
- def _capture_state_update(self, state: Any, plan: _CapturePlan) -> dict[str, Any]:
+ def _capture_state_update(
+ self, state: OpenVikingCaptureState, plan: _CapturePlan
+ ) -> dict[str, Any]:
delivered = dict(state.get("openviking_captured_message_ids") or {})
delivered[_stable_json(plan.key)] = sorted(self._captured_message_ids.get(plan.key, set()))
return {"openviking_captured_message_ids": delivered}
@@ -299,11 +303,11 @@ class OpenVikingContextMiddleware(AgentMiddleware):
if message_id:
delivered.add(message_id)
- def after_agent(self, state: AgentState[Any], runtime: Any) -> dict[str, Any] | None:
+ def after_agent(self, state: OpenVikingCaptureState, runtime: Any) -> dict[str, Any] | None:
return self._capture(state, runtime, commit=True)
def _capture(
- self, state: AgentState[Any], runtime: Any, *, commit: bool
+ self, state: OpenVikingCaptureState, runtime: Any, *, commit: bool
) -> dict[str, Any] | None:
plan = self._capture_plan(state, runtime)
if plan is None:
@@ -331,14 +335,14 @@ class OpenVikingContextMiddleware(AgentMiddleware):
async def aafter_agent(
self,
- state: AgentState[Any],
+ state: OpenVikingCaptureState,
runtime: Any,
) -> dict[str, Any] | None:
"""Asynchronously capture messages after an agent run."""
return await self._acapture(state, runtime, commit=True)
async def _acapture(
- self, state: AgentState[Any], runtime: Any, *, commit: bool
+ self, state: OpenVikingCaptureState, runtime: Any, *, commit: bool
) -> dict[str, Any] | None:
plan = self._capture_plan(state, runtime)
if plan is None:
@@ -371,7 +375,7 @@ class OpenVikingContextMiddleware(AgentMiddleware):
def _capture_plan(
self,
- state: AgentState[Any],
+ state: OpenVikingCaptureState,
runtime: Any,
) -> _CapturePlan | None:
if not self.capture_on_after_agent:
diff --git a/tests/integration/langchain_langgraph/test_async_recording.py b/tests/integration/langchain_langgraph/test_async_recording.py
index d49d8b85a..05cbd44d3 100644
--- a/tests/integration/langchain_langgraph/test_async_recording.py
+++ b/tests/integration/langchain_langgraph/test_async_recording.py
@@ -595,7 +595,7 @@ async def test_real_create_agent_ainvoke_uses_async_middleware_hooks():
)
assert result["messages"][-1].content == "Stored by async middleware."
- assert client.batch_sizes == [2]
+ assert sum(client.batch_sizes) == 2
assert [
message["parts"][0]["text"] for message in client.backing.sessions["real-async-agent"]
] == [
diff --git a/tests/integration/langchain_langgraph/test_recording.py b/tests/integration/langchain_langgraph/test_recording.py
index a3e7d4f38..fab221300 100644
--- a/tests/integration/langchain_langgraph/test_recording.py
+++ b/tests/integration/langchain_langgraph/test_recording.py
@@ -130,7 +130,7 @@ def test_real_create_agent_records_through_middleware_recorder():
)
assert result["messages"][-1].content == "Stored by a real LangChain agent."
- assert client.batch_sizes == [2]
+ assert sum(client.batch_sizes) == 2
assert [message["parts"][0]["text"] for message in client.sessions["real-agent-recorder"]] == [
"Remember this agent turn.",
"Stored by a real LangChain agent.",