Custom state schema (context_schema) not propagated to subagents #55

Closed
opened 2026-02-16 08:19:39 -05:00 by yindo · 3 comments
Owner

Originally created by @leecj on GitHub (Sep 27, 2025).

When using create_deep_agent with a custom state_schema, tools can access custom state fields when called directly by the main agent, but lose access to these fields when called by subagents. This creates inconsistent behavior where the same tool works differently depending on the calling context.

💥 Impact

Inconsistent tool behavior: Tools that depend on custom state fields work in main agent but fail in subagents
State isolation issues: Subagents cannot access or modify custom state defined in the main agent
Unexpected runtime errors: Tools may fail with KeyError when accessing custom state fields in subagent context

🔍 Root Cause

The context_schema parameter is correctly passed to the main agent via create_agent() in agent_builder(), but it's not propagated to subagents created in the _get_agents() function within SubAgentMiddleware.

Originally created by @leecj on GitHub (Sep 27, 2025). When using create_deep_agent with a custom state_schema, tools can access custom state fields when called directly by the main agent, but lose access to these fields when called by subagents. This creates inconsistent behavior where the same tool works differently depending on the calling context. 💥 Impact Inconsistent tool behavior: Tools that depend on custom state fields work in main agent but fail in subagents State isolation issues: Subagents cannot access or modify custom state defined in the main agent Unexpected runtime errors: Tools may fail with KeyError when accessing custom state fields in subagent context 🔍 Root Cause The context_schema parameter is correctly passed to the main agent via create_agent() in agent_builder(), but it's not propagated to subagents created in the _get_agents() function within SubAgentMiddleware.
yindo closed this issue 2026-02-16 08:19:39 -05:00
Author
Owner

@nhuang-lc commented on GitHub (Sep 29, 2025):

Hey! Which version of deepagents are you using?

On the latest version of deepagents, context_schema are created through custom middleware: https://docs.langchain.com/oss/python/langchain/middleware

I fixed the issue around state keys not being passed to subagents, so that is resolved on the latest version of deepagents.

Could you confirm what version you're using?

@nhuang-lc commented on GitHub (Sep 29, 2025): Hey! Which version of deepagents are you using? On the latest version of deepagents, context_schema are created through custom middleware: https://docs.langchain.com/oss/python/langchain/middleware I fixed the issue around state keys not being passed to subagents, so that is resolved on the latest version of deepagents. Could you confirm what version you're using?
Author
Owner

@nhuang-lc commented on GitHub (Oct 20, 2025):

Closing this issue as it is fixed in the latest package version 0.1.0 as well - feel free to re-open if you're still running into issues, happy to help 😄

@nhuang-lc commented on GitHub (Oct 20, 2025): Closing this issue as it is fixed in the latest package version 0.1.0 as well - feel free to re-open if you're still running into issues, happy to help 😄
Author
Owner

@wylswz commented on GitHub (Dec 31, 2025):

Hi, I'm getting a similar issue. I used context_schema to pass a sandbox id to the agent, like following

class Context(BaseModel):
    sandbox_id: str

agent = create_deep_agent(
        model=MODEL,
        system_prompt=SYSTEM_PROMPT,
        middleware=[SandboxMiddleware()],
        tools=[
            bash,
        ],
        context_schema=Context,
        subagents=[
            {
                "name": "worker",
                "model": MODEL,
                "description": "A worker agent that can help with tasks related to the worker.",
                "system_prompt": WORKER_PROMPT,
                "tools": [bash, tavily_search],
            }
        ],
    )


stream = agemt.astream({
            "messages": [HumanMessage(content="")],
        }, context=Context(sandbox_id=str(uuid.uuid4())), stream_mode="messages")

and I constantly get this warning

/Users/wy/Documents/GitHub/langchain-examples/.venv/lib/python3.13/site-packages/pydantic/main.py:464: UserWarning: Pydantic serializer warnings:
  PydanticSerializationUnexpectedValue(Expected `none` - serialized value may not be as expected [field_name='context', input_value=Context(sandbox_id='507f3...4a83-893d-0d26953fb1c6'), input_type=Context])
  return self.__pydantic_serializer__.to_python(

And in SubAgentMiddleware, I didn't see context_schema being passed to create_agent.

@wylswz commented on GitHub (Dec 31, 2025): Hi, I'm getting a similar issue. I used `context_schema` to pass a sandbox id to the agent, like following ```py class Context(BaseModel): sandbox_id: str agent = create_deep_agent( model=MODEL, system_prompt=SYSTEM_PROMPT, middleware=[SandboxMiddleware()], tools=[ bash, ], context_schema=Context, subagents=[ { "name": "worker", "model": MODEL, "description": "A worker agent that can help with tasks related to the worker.", "system_prompt": WORKER_PROMPT, "tools": [bash, tavily_search], } ], ) stream = agemt.astream({ "messages": [HumanMessage(content="")], }, context=Context(sandbox_id=str(uuid.uuid4())), stream_mode="messages") ``` and I constantly get this warning ``` /Users/wy/Documents/GitHub/langchain-examples/.venv/lib/python3.13/site-packages/pydantic/main.py:464: UserWarning: Pydantic serializer warnings: PydanticSerializationUnexpectedValue(Expected `none` - serialized value may not be as expected [field_name='context', input_value=Context(sandbox_id='507f3...4a83-893d-0d26953fb1c6'), input_type=Context]) return self.__pydantic_serializer__.to_python( ``` And in `SubAgentMiddleware`, I didn't see `context_schema` being passed to `create_agent`.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/deepagents#55