[GH-ISSUE #674] [langchain]: Using middleware with stateful tools in LangChain 1.x #104

Open
opened 2026-02-17 17:19:11 -05:00 by yindo · 0 comments
Owner

Originally created by @austinmw on GitHub (Sep 28, 2025).
Original GitHub issue: https://github.com/langchain-ai/docs/issues/674

Type of issue

request for content

Language

Python

Description

I want to use middleware to update system prompts with few-shot examples AND also have tools that maintain state between calls.

What I tried:
Using both middleware and state_schema to add custom state keys:

class CustomState(AgentState):
    dataframe_dicts: dict = {}

agent = create_agent(
    model=model,
    tools=[...],
    middleware=[...],
    state_schema=CustomState
)

Problem:
Got AssertionError: state_schema must be None when using middleware

What I did instead:
Used middleware instance storage:

class StorageMiddleware(AgentMiddleware):
    def __init__(self):
        self.dataframe_dicts = {}  # Instance storage
    
    def modify_model_request(self, request, state):
        request.system_prompt = self.generate_prompt_with_examples()
        return request

storage = StorageMiddleware()

@tool
def execute_sql(query: str) -> str:
    storage.dataframe_dicts[id] = results  # Tools access instance directly
    return "Stored"

Is this the correct pattern?

Documentation request:
Could the docs include an example of how to properly combine middleware with tools that need persistent state? This seems like a common use case but I can't find examples of it.

Originally created by @austinmw on GitHub (Sep 28, 2025). Original GitHub issue: https://github.com/langchain-ai/docs/issues/674 ### Type of issue request for content ### Language Python ### Description I want to use middleware to update system prompts with few-shot examples AND also have tools that maintain state between calls. **What I tried:** Using both middleware and `state_schema` to add custom state keys: ```python class CustomState(AgentState): dataframe_dicts: dict = {} agent = create_agent( model=model, tools=[...], middleware=[...], state_schema=CustomState ) ``` **Problem:** Got `AssertionError: state_schema must be None when using middleware` **What I did instead:** Used middleware instance storage: ```python class StorageMiddleware(AgentMiddleware): def __init__(self): self.dataframe_dicts = {} # Instance storage def modify_model_request(self, request, state): request.system_prompt = self.generate_prompt_with_examples() return request storage = StorageMiddleware() @tool def execute_sql(query: str) -> str: storage.dataframe_dicts[id] = results # Tools access instance directly return "Stored" ``` Is this the correct pattern? **Documentation request:** Could the docs include an example of how to properly combine middleware with tools that need persistent state? This seems like a common use case but I can't find examples of it.
yindo added the langchainexternal labels 2026-02-17 17:19:11 -05:00
yindo changed title from [langchain]: Using middleware with stateful tools in LangChain 1.x to [GH-ISSUE #674] [langchain]: Using middleware with stateful tools in LangChain 1.x 2026-06-05 17:25:06 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/docs#104