Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/oss/langchain/runtime.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ Use the `runtime` parameter to access the @[`Runtime`] object inside a tool.
```ts
import * as z from "zod";
import { tool } from "langchain";
import { type Runtime } from "@langchain/langgraph"; // [!code highlight]
import { type ToolRuntime } from "@langchain/core/tools"; // [!code highlight]

const contextSchema = z.object({
userName: z.string(),
});

const fetchUserEmailPreferences = tool(
async (_, runtime: Runtime<z.infer<typeof contextSchema>>) => { // [!code highlight]
async (_, runtime: ToolRuntime<any, typeof contextSchema>) => { // [!code highlight]
const userName = runtime.context?.userName; // [!code highlight]
if (!userName) {
throw new Error("userName is required");
Expand Down Expand Up @@ -216,7 +216,7 @@ Use the `runtime` parameter to access the @[`Runtime`] object inside middleware.

```ts
import * as z from "zod";
import { createAgent, createMiddleware, type AgentState, SystemMessage } from "langchain";
import { createAgent, createMiddleware, SystemMessage } from "langchain";
import { type Runtime } from "@langchain/langgraph"; // [!code highlight]

const contextSchema = z.object({
Expand All @@ -226,7 +226,8 @@ const contextSchema = z.object({
// Dynamic prompt middleware
const dynamicPromptMiddleware = createMiddleware({
name: "DynamicPrompt",
beforeModel: (state: AgentState, runtime: Runtime<z.infer<typeof contextSchema>>) => { // [!code highlight]
contextSchema
beforeModel: (state, runtime: Runtime<z.infer<typeof contextSchema>>) => { // [!code highlight]
const userName = runtime.context?.userName; // [!code highlight]
if (!userName) {
throw new Error("userName is required");
Expand All @@ -242,11 +243,12 @@ const dynamicPromptMiddleware = createMiddleware({
// Logging middleware
const loggingMiddleware = createMiddleware({
name: "Logging",
beforeModel: (state: AgentState, runtime: Runtime<z.infer<typeof contextSchema>>) => { // [!code highlight]
contextSchema,
beforeModel: (state, runtime) => { // [!code highlight]
console.log(`Processing request for user: ${runtime.context?.userName}`); // [!code highlight]
return;
},
afterModel: (state: AgentState, runtime: Runtime<z.infer<typeof contextSchema>>) => { // [!code highlight]
afterModel: (state, runtime) => { // [!code highlight]
console.log(`Completed request for user: ${runtime.context?.userName}`); // [!code highlight]
return;
}
Expand Down