fix(middleware): fallback to configurable thread_id in thread data middleware (#1237)

Co-authored-by: Exploreunive <Exploreunive@users.noreply.github.com>
Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
This commit is contained in:
haoliangxu
2026-03-22 20:14:51 +08:00
committed by GitHub
parent 894875ab1b
commit e6c6770b70
2 changed files with 62 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ from typing import NotRequired, override
from langchain.agents import AgentState
from langchain.agents.middleware import AgentMiddleware
from langgraph.config import get_config
from langgraph.runtime import Runtime
from deerflow.agents.thread_state import ThreadDataState
@@ -71,9 +72,14 @@ class ThreadDataMiddleware(AgentMiddleware[ThreadDataMiddlewareState]):
@override
def before_agent(self, state: ThreadDataMiddlewareState, runtime: Runtime) -> dict | None:
thread_id = runtime.context.get("thread_id")
context = runtime.context or {}
thread_id = context.get("thread_id")
if thread_id is None:
raise ValueError("Thread ID is required in the context")
config = get_config()
thread_id = config.get("configurable", {}).get("thread_id")
if thread_id is None:
raise ValueError("Thread ID is required in runtime context or config.configurable")
if self._lazy_init:
# Lazy initialization: only compute paths, don't create directories