PIIMiddleware(apply_to_output=True) 在工具调用时清空参数,导致 GraphRecursion 错误。
作者: jclauson32创建于 2026年9月22日更新于 2026年9月28日
标签bugcorelangchainexternal
import time from LangChain.agents import create_agent from LangChain.agents.middleware import PIIMiddleware from LangChain.tools import tool from langchain_core.language_models.chat_models import BaseChatModel from langchain_core.messages import AIMessageChunk from langchain_core.outputs import ChatGenerationChunk ARGS = ["", '{"city"', ': "Bos', 'ton"}'] # {"city": "Boston"} split across chunks
class FakeStreamingModel(BaseChatModel): """Streams a tool call in chunks, paced like a real provider."""
idx: int = 0
@property
def _llm_type(self) -> str:
return "fake-streaming-model"
def bind_tools(self, tools, **kwargs):
return self
def _stream(self, messages, stop=None, run_manager=None, **kwargs):
i = self.idx
object.__setattr__(self, "idx", i + 1)
if i: # second turn: plain text, so the run terminates
yield ChatGenerationChunk(message=AIMessageChunk(content="Done."))
return
for n, part in enumerate(ARGS):
# The pauses are required. The PII stream Transformer runs on the
# consumer side, so an instantaneous model assembles the whole
# message before the mux pumps an event and the bug stays hidden.
time.sleep(0.01)
…
内容来源: langchain-ai/langchain