[PR #6] [CLOSED] Add Optional files/todos Propagation and Fix Sub-Agent State Issues #400

Closed
opened 2026-02-16 09:15:53 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/deepagents/pull/6
Author: @zamalali
Created: 7/30/2025
Status: Closed

Base: masterHead: master


📝 Commits (10+)

  • c0d9f06 Allow overriding the default system prompt via full_prompt parameter in create_deep_agent
  • f0817a3 Delete uv.lock
  • 3ec8be0 fix: prevent sub-agent state corruption, use DeepAgentState, and avoid recursion errors
  • cf79871 remove redundant code
  • 1d4da02 0.0.3 (#11)
  • 5daed48 fixing checkboxes on readme (#16)
  • f148c9d docs: Fix links in README.md (#18)
  • 0762b9e Add notes in README about MCP support
  • 97b3c52 fix: correct typo in research agent prompt (#26)
  • 615ec97 ollama example (#27)

📊 Changes

3 files changed (+32 additions, -16 deletions)

View changed files

📝 pyproject.toml (+1 -1)
📝 src/deepagents/graph.py (+1 -1)
📝 src/deepagents/sub_agent.py (+30 -14)

📄 Description

Problem

The original sub_agent.py had two critical issues that disrupted workflows:

  1. State Corruption: The task tool overwrote the parent agent's state with state["messages"] = [...], erasing prior user/assistant messages. This broke multi-step workflows requiring preserved context.
  2. Schema Inconsistency: The general-purpose sub-agent was created without state_schema, defaulting to LangGraph's AgentState. This caused files and todos fields to be dropped during delegation, breaking state persistence.

Additionally, the tool passed the full state (including files and todos) to all sub-agents, causing unnecessary memory overhead for lightweight agents like analyzer.

Fix

To address these issues, I made the following changes to sub_agent.py:

  1. Isolated sub_state:
    • Replaced state["messages"] mutation with a new sub_state dictionary containing only the task description and selected fields.
    • Prevents corruption of the parent agent's state, preserving message history.
  2. Schema Consistency:
    • Added state_schema=DeepAgentState to the general-purpose sub-agent creation.
    • Ensures all sub-agents maintain files and todos fields consistently.
  3. Optional State Propagation:
    • Added include_files and include_todos flags to SubAgent (default: False).
    • Created a subagent_configs dictionary for efficient flag lookups at initialization.
    • Only propagate files/todos to sub_state and update in Command(update=...) if flagged.
    • Optimized Command return to reuse the existing config variable, removing a redundant lookup.
  4. Async Path:
    • Retained ainvoke for asynchronous execution, ensuring compatibility.

Behavior

  • Parent State Preservation: The parent agent's state is never modified, maintaining message history for multi-step workflows.
  • General-Purpose Agent: Always propagates and updates both files and todos for compatibility.
  • Custom Sub-Agents: Default to receiving/returning only messages unless include_files or include_todos is explicitly enabled, reducing memory overhead.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langchain-ai/deepagents/pull/6 **Author:** [@zamalali](https://github.com/zamalali) **Created:** 7/30/2025 **Status:** ❌ Closed **Base:** `master` ← **Head:** `master` --- ### 📝 Commits (10+) - [`c0d9f06`](https://github.com/langchain-ai/deepagents/commit/c0d9f06167e89f3c438354cf2d942a9ade845cd5) Allow overriding the default system prompt via full_prompt parameter in create_deep_agent - [`f0817a3`](https://github.com/langchain-ai/deepagents/commit/f0817a3fe4873947a841a3b804e0ca6d8ea536d6) Delete uv.lock - [`3ec8be0`](https://github.com/langchain-ai/deepagents/commit/3ec8be026d564a67c52abfb86c2775a5b4a0ad91) fix: prevent sub-agent state corruption, use DeepAgentState, and avoid recursion errors - [`cf79871`](https://github.com/langchain-ai/deepagents/commit/cf7987195b2f01e51b5123557cce6e6328f8272c) remove redundant code - [`1d4da02`](https://github.com/langchain-ai/deepagents/commit/1d4da02e06990788179bc6adf3188d6bcf1120c1) 0.0.3 (#11) - [`5daed48`](https://github.com/langchain-ai/deepagents/commit/5daed48978e4be066fb70e9979d4c6d0a81b73d5) fixing checkboxes on readme (#16) - [`f148c9d`](https://github.com/langchain-ai/deepagents/commit/f148c9dd3fb2244ff43d5ac470eafa44474423ad) docs: Fix links in README.md (#18) - [`0762b9e`](https://github.com/langchain-ai/deepagents/commit/0762b9e9c0db503739e33e38f248f8cb7b89e5bb) Add notes in README about MCP support - [`97b3c52`](https://github.com/langchain-ai/deepagents/commit/97b3c523cf57381115bce1efe5d0a8a2a72a0676) fix: correct typo in research agent prompt (#26) - [`615ec97`](https://github.com/langchain-ai/deepagents/commit/615ec9770fc482943c5cd23fe90e61b8ea1b5ef6) ollama example (#27) ### 📊 Changes **3 files changed** (+32 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `pyproject.toml` (+1 -1) 📝 `src/deepagents/graph.py` (+1 -1) 📝 `src/deepagents/sub_agent.py` (+30 -14) </details> ### 📄 Description ## Problem The original `sub_agent.py` had two critical issues that disrupted workflows: 1. **State Corruption**: The `task` tool overwrote the parent agent's state with `state["messages"] = [...]`, erasing prior user/assistant messages. This broke multi-step workflows requiring preserved context. 2. **Schema Inconsistency**: The `general-purpose` sub-agent was created without `state_schema`, defaulting to LangGraph's `AgentState`. This caused `files` and `todos` fields to be dropped during delegation, breaking state persistence. Additionally, the tool passed the full `state` (including `files` and `todos`) to all sub-agents, causing unnecessary memory overhead for lightweight agents like `analyzer`. ## Fix To address these issues, I made the following changes to `sub_agent.py`: 1. **Isolated `sub_state`**: - Replaced `state["messages"]` mutation with a new `sub_state` dictionary containing only the task description and selected fields. - Prevents corruption of the parent agent's state, preserving message history. 2. **Schema Consistency**: - Added `state_schema=DeepAgentState` to the `general-purpose` sub-agent creation. - Ensures all sub-agents maintain `files` and `todos` fields consistently. 3. **Optional State Propagation**: - Added `include_files` and `include_todos` flags to `SubAgent` (default: `False`). - Created a `subagent_configs` dictionary for efficient flag lookups at initialization. - Only propagate `files`/`todos` to `sub_state` and update in `Command(update=...)` if flagged. - Optimized `Command` return to reuse the existing `config` variable, removing a redundant lookup. 4. **Async Path**: - Retained `ainvoke` for asynchronous execution, ensuring compatibility. ## Behavior - **Parent State Preservation**: The parent agent's state is never modified, maintaining message history for multi-step workflows. - **General-Purpose Agent**: Always propagates and updates both `files` and `todos` for compatibility. - **Custom Sub-Agents**: Default to receiving/returning only `messages` unless `include_files` or `include_todos` is explicitly enabled, reducing memory overhead. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-16 09:15:53 -05:00
yindo closed this issue 2026-02-16 09:15:53 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/deepagents#400