You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
oss: code snippet fixes + other minor changes (#852)
just scoped to oss folder for now
* removing old style comment + annotation syntax
* removing `highlight={X,Y,Z}` syntax in favor of `# [!code highlight]`
syntax inline
* some of @hwchase17's initial docs feedback 👍
Copy file name to clipboardExpand all lines: src/oss/concepts/context.mdx
+31-50Lines changed: 31 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,30 +39,26 @@ LangGraph provides three ways to manage context, which combines the mutability a
39
39
40
40
**Static runtime context** represents immutable data like user metadata, tools, and database connections that are passed to an application at the start of a run via the `context` argument to `invoke`/`stream`. This data does not change during execution.
1. This is the invocation of the agent or graph. The `invoke` method runs the underlying graph with the provided input.
54
-
2. This example uses messages as an input, which is common, but your application may use different input structures.
55
-
3. This is where you pass the runtime data. The `context` parameter allows you to provide additional dependencies that the agent can use during its execution.
56
-
57
53
<Tabs>
58
54
<Tabtitle="Agent prompt">
59
-
```python {highlight={6,20}}
55
+
```python
60
56
from langchain.messages import AnyMessage
61
57
from langgraph.runtime import get_runtime
62
58
from langgraph.prebuilt.chat_agent_executor import AgentState
State can also be accessed by the agent's **tools**, which can read or update the state as needed. See [tool calling guide](/oss/langchain/tools#short-term-memory) for details.
152
147
153
148
:::python
154
-
```python{highlight={6,10,19}}
149
+
```python
155
150
from langchain.messages import AnyMessage
156
151
from langchain_core.runnables import RunnableConfig
157
152
from langgraph.prebuilt import create_react_agent
158
153
from langgraph.prebuilt.chat_agent_executor import AgentState
159
154
160
-
class CustomState(AgentState): # (1)!
155
+
class CustomState(AgentState): # [!code highlight]
161
156
user_name: str
162
157
163
158
def prompt(
164
-
state: CustomState
159
+
state: CustomState # [!code highlight]
165
160
) -> list[AnyMessage]:
166
161
user_name = state["user_name"]
167
162
system_msg = f"You are a helpful assistant. User's name is {user_name}"
@@ -170,7 +165,7 @@ await graph.invoke(
170
165
agent = create_react_agent(
171
166
model="anthropic:claude-3-7-sonnet-latest",
172
167
tools=[...],
173
-
state_schema=CustomState, # (2)!
168
+
state_schema=CustomState, # [!code highlight]
174
169
prompt=prompt
175
170
)
176
171
@@ -179,20 +174,17 @@ await graph.invoke(
179
174
"user_name": "John Smith"
180
175
})
181
176
```
182
-
183
-
1. Define a custom state schema that extends `AgentState` or `MessagesState`.
184
-
2. Pass the custom state schema to the agent. This allows the agent to access and modify the state during execution.
185
177
:::
186
178
187
179
:::js
188
-
```typescript {highlight={6,12,22}}
180
+
```typescript
189
181
import type { BaseMessage } from "@langchain/core/messages";
190
182
import { createReactAgent } from "@langchain/langgraph/prebuilt";
191
183
import { MessagesZodMeta } from "@langchain/langgraph";
192
184
import { registry } from "@langchain/langgraph/zod";
// Specify how the trajectories will be compared. `superset` will accept output trajectory as valid if it's a superset of the reference one. Other options include: strict, unordered and subset
0 commit comments