How to pass custom state to a subagent

Hi team and community,

After reading the DeepAgent source code, I noticed the following:

  1. create_deep_agent does not accept a state_schema parameter like create_agent does.

  2. When passing subagents to create_deep_agent, they are wrapped as tasks. The subagent is invoked via task(), and its parameters are extracted using:

subagent_state = {k: v for k, v in runtime.state.items() if k not in _EXCLUDED_STATE_KEYS}

If my subagent has a state with multiple fields, it cannot receive the corresponding parameters.

I would like to know whether the DeepAgent team plans to support passing a custom state to create_deep_agent in the future.

hi @QwQzy

have you tried extending the parent agent state via middleware, so that it forwards it into subagents?

The idiomatic pattern is to define a middleware with state_schema = YourState, where YourState extends AgentState. Deep Agents accepts extra middleware via the middleware=[...] parameter on create_deep_agent, so you can extend state there.

from langchain.agents.middleware import AgentMiddleware, AgentState
from deepagents import create_deep_agent

class MyState(AgentState):
    customer_id: str
    plan: str
    flags: dict

class MyStateMiddleware(AgentMiddleware):
    state_schema = MyState

agent = create_deep_agent(
    middleware=[MyStateMiddleware()],
    subagents=[...],
)

# IMPORTANT: populate those keys in the parent state before calling task()
agent.invoke({
    "messages": [{"role": "user", "content": "Do the thing"}],
    "customer_id": "cust_123",
    "plan": "pro",
    "flags": {"beta": True},
})