Skip to content

Commit f2617b7

Browse files
docs(deepagents): document custom state schemas (#4194)
1 parent df367b7 commit f2617b7

4 files changed

Lines changed: 52 additions & 4 deletions

File tree

pipeline/preprocessors/link_map.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class LinkMap(TypedDict):
3434
"langchain_text_splitters": "langchain-text-splitters/",
3535
# Deep Agents
3636
"create_deep_agent": "deepagents/graph/create_deep_agent",
37+
"DeepAgentState": "deepagents/graph/DeepAgentState",
3738
"SubAgent": "deepagents/middleware/subagents/SubAgent",
3839
"CompiledSubAgent": "deepagents/middleware/subagents/CompiledSubAgent",
3940
"SubAgentMiddleware": "deepagents/middleware/subagents/SubAgentMiddleware",

src/oss/deepagents/context-engineering.mdx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,51 @@ const result = await agent.invoke(
306306

307307
Runtime context **propagates to all subagents**. When a subagent runs, it receives the same runtime context as the parent. See [Subagents](/oss/deepagents/subagents#context-management) for per-subagent context (namespaced keys).
308308

309+
:::python
310+
## Custom state schema
311+
312+
<Note>
313+
Custom state schemas require `deepagents>=0.6.6`.
314+
</Note>
315+
316+
Use `state_schema` when data must be part of the agent's mutable graph state, checkpointed with the thread, or available through `runtime.state`. For immutable per-run inputs such as user IDs, credentials, or feature flags, prefer [runtime context](#runtime-context).
317+
318+
Custom state schemas must subclass @[DeepAgentState]. This preserves the built-in `DeltaChannel` reducer on `messages`, which keeps checkpoint growth linear as conversations get longer.
319+
320+
```python
321+
from deepagents import DeepAgentState, create_deep_agent
322+
from langchain.tools import ToolRuntime, tool
323+
324+
325+
class ResearchState(DeepAgentState):
326+
page_url: str
327+
file_urls: list[str]
328+
329+
330+
@tool
331+
def cite_page(runtime: ToolRuntime) -> str:
332+
"""Return the current page URL."""
333+
return runtime.state["page_url"]
334+
335+
336+
agent = create_deep_agent(
337+
model="anthropic:claude-sonnet-4-6",
338+
tools=[cite_page],
339+
state_schema=ResearchState,
340+
)
341+
342+
result = agent.invoke(
343+
{
344+
"messages": [{"role": "user", "content": "Cite the current page"}],
345+
"page_url": "https://example.com/report",
346+
"file_urls": [],
347+
}
348+
)
349+
```
350+
351+
The schema is merged with state schemas contributed by middleware. Declarative @[SubAgent] specs passed to `subagents=` inherit the parent `state_schema` when Deep Agents compiles them for the `task` tool. @[CompiledSubAgent] runnables and remote @[AsyncSubAgent] specs do not inherit it because their graphs are already compiled or hosted separately. Compile those graphs with a compatible schema if they need the same state fields.
352+
:::
353+
309354
## Context compression
310355

311356
Long-running tasks produce large tool outputs and long conversation history.

src/oss/deepagents/customization.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ agent = create_deep_agent(
7878
| [`middleware=`](#middleware) | Extra middleware to add to the stack |
7979
| [`interrupt_on=`](#human-in-the-loop) | Pause before tool calls for human approval |
8080
| [`response_format=`](#structured-output) | Structured output schema |
81+
| [`state_schema=`](/oss/deepagents/context-engineering#custom-state-schema) | Custom graph state schema |
8182
| [profiles](#profiles) | Per-model defaults as a reusable bundle |
8283

8384
<Accordion title="Full function signature">
@@ -491,12 +492,13 @@ Do **not** do this:
491492

492493
:::python
493494
Mutation in place, such as modifying `self.x` in `before_agent` or changing other shared values in hooks, can lead to subtle bugs and race conditions because many operations run concurrently (subagents, parallel tools, and parallel invocations on different threads).
495+
496+
For full details on extending state with custom properties, see [Custom middleware - Custom state schema](/oss/langchain/middleware/custom#custom-state-schema).
494497
:::
495498
:::js
496499
Mutation in place, such as modifying `state.x` in `beforeAgent`, mutating a shared variable in `beforeAgent`, or changing other shared values in hooks, can lead to subtle bugs and race conditions because many operations run concurrently (subagents, parallel tools, and parallel invocations on different threads).
497500
:::
498501

499-
For full details on extending state with custom properties, see [Custom middleware - Custom state schema](/oss/langchain/middleware/custom#custom-state-schema).
500502
If you must use mutation in custom middleware, consider what happens when subagents, parallel tools, or concurrent agent invocations run at the same time.
501503
</Warning>
502504

src/oss/javascript/integrations/providers/google.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ npm install @langchain/google-genai @langchain/core
9696

9797
### `@langchain/google-vertexai`
9898

99-
The `@langchain/google-vertexai` package provides [`ChatVertexAI`](/oss/integrations/chat/google_vertex_ai), [`VertexAIEmbeddings`](/oss/integrations/embeddings/google_vertex_ai), and [`VertexAI`](/oss/integrations/llms/google_vertex_ai) for Vertex AI on Node.js. It depends on [`@langchain/google-gauth`](#langchaingoogle-gauth) for authentication. This package is superseded by the Vertex AI support built into `@langchain/google` for chat.
99+
The `@langchain/google-vertexai` package provides [`ChatVertexAI`](/oss/integrations/chat/google_vertex_ai), [`VertexAIEmbeddings`](/oss/integrations/embeddings/google_vertex_ai), and [`VertexAI`](/oss/integrations/llms/google_vertex_ai) for Vertex AI on Node.js. It depends on [`@langchain/google-gauth`](#%40langchain%2Fgoogle-gauth) for authentication. This package is superseded by the Vertex AI support built into `@langchain/google` for chat.
100100

101101
```bash
102102
npm install @langchain/google-vertexai @langchain/core
103103
```
104104

105105
### `@langchain/google-vertexai-web`
106106

107-
The `@langchain/google-vertexai-web` package provides the same Vertex AI chat, embedding, and LLM classes for browser and Edge runtimes. Install this package (not `@langchain/google-vertexai`) when running in web environments. It depends on [`@langchain/google-webauth`](#langchaingoogle-webauth).
107+
The `@langchain/google-vertexai-web` package provides the same Vertex AI chat, embedding, and LLM classes for browser and Edge runtimes. Install this package (not `@langchain/google-vertexai`) when running in web environments. It depends on [`@langchain/google-webauth`](#%40langchain%2Fgoogle-webauth).
108108

109109
```bash
110110
npm install @langchain/google-vertexai-web @langchain/core
@@ -120,7 +120,7 @@ Set service account JSON in `GOOGLE_WEB_CREDENTIALS` (or the deprecated `GOOGLE_
120120

121121
### `@langchain/google-gauth`
122122

123-
The [`@langchain/google-gauth`](https://github.com/langchain-ai/langchainjs/tree/main/libs/providers/langchain-google-gauth) package provides Node.js authentication for legacy Google integrations built on [`@langchain/google-common`](#langchaingoogle-common). It is installed automatically when you add `@langchain/google-vertexai`—you typically do **not** install or import `@langchain/google-gauth` directly.
123+
The [`@langchain/google-gauth`](https://github.com/langchain-ai/langchainjs/tree/main/libs/providers/langchain-google-gauth) package provides Node.js authentication for legacy Google integrations built on [`@langchain/google-common`](#%40langchain%2Fgoogle-common). It is installed automatically when you add `@langchain/google-vertexai`—you typically do **not** install or import `@langchain/google-gauth` directly.
124124

125125
On Node.js, credentials are resolved in this order:
126126

0 commit comments

Comments
 (0)