[GH-ISSUE #1467] [langchain]: Migration Guide - Dynamic prompts code snippet errors #210

Closed
opened 2026-02-17 17:19:24 -05:00 by yindo · 0 comments
Owner

Originally created by @ddewaele on GitHub (Nov 17, 2025).
Original GitHub issue: https://github.com/langchain-ai/docs/issues/1467

Type of issue

issue / bug

Language

JavaScript

Description

Following code snippet doesn't work.

  • contextSchema default value is invalid
  • dynamicSystemPromptMiddleware accepts 2 args (state and runtime)
  • passing the userRole via context during the invoke is wrong
import { createAgent, dynamicSystemPromptMiddleware } from "langchain";
import * as z from "zod";

const contextSchema = z.object({
  userRole: z.enum(["expert", "beginner"]).default("user"),
});

const userRolePrompt = dynamicSystemPromptMiddleware((request) => { 
  const userRole = request.runtime.context.userRole;
  const basePrompt = "You are a helpful assistant.";

  if (userRole === "expert") {
    return `${basePrompt} Provide detailed technical responses.`;
  } else if (userRole === "beginner") {
    return `${basePrompt} Explain concepts simply and avoid jargon.`;
  }
  return basePrompt; 
});

const agent = createAgent({
  model,
  tools,
  middleware: [userRolePrompt],
  contextSchema,
});

await agent.invoke({
  messages: [new HumanMessage("Explain async programming")],
  context: {
    userRole: "expert",
  },
})

Should be

const contextSchema = z.object({
  userRole: z.enum(["expert", "beginner"]).default("beginner"),
});

const userRolePrompt = dynamicSystemPromptMiddleware<z.infer<typeof contextSchema>>( // [!code highlight]
    (_state, runtime) => {
        const userRole = runtime.context.userRole;
        const basePrompt = "You are a helpful assistant.";

        if (userRole === "expert") {
            return `${basePrompt} Provide detailed technical responses.`;
        } else if (userRole === "beginner") {
            return `${basePrompt} Explain concepts simply and avoid jargon.`;
        }
        return basePrompt; // [!code highlight]
    }
);

const agent = createAgent({
  model,
  tools,
  middleware: [userRolePrompt],
  contextSchema,
});

await agent.invoke(
    {
        messages: [new HumanMessage("Explain async programming")]
    },
    {
        configurable: {
            userRole: "expert",
        }
    }
);
Originally created by @ddewaele on GitHub (Nov 17, 2025). Original GitHub issue: https://github.com/langchain-ai/docs/issues/1467 ### Type of issue issue / bug ### Language JavaScript ### Description Following code snippet doesn't work. - `contextSchema` default value is invalid - `dynamicSystemPromptMiddleware` accepts 2 args (state and runtime) - passing the userRole via context during the `invoke` is wrong ``` import { createAgent, dynamicSystemPromptMiddleware } from "langchain"; import * as z from "zod"; const contextSchema = z.object({ userRole: z.enum(["expert", "beginner"]).default("user"), }); const userRolePrompt = dynamicSystemPromptMiddleware((request) => { const userRole = request.runtime.context.userRole; const basePrompt = "You are a helpful assistant."; if (userRole === "expert") { return `${basePrompt} Provide detailed technical responses.`; } else if (userRole === "beginner") { return `${basePrompt} Explain concepts simply and avoid jargon.`; } return basePrompt; }); const agent = createAgent({ model, tools, middleware: [userRolePrompt], contextSchema, }); await agent.invoke({ messages: [new HumanMessage("Explain async programming")], context: { userRole: "expert", }, }) ``` Should be ``` const contextSchema = z.object({ userRole: z.enum(["expert", "beginner"]).default("beginner"), }); const userRolePrompt = dynamicSystemPromptMiddleware<z.infer<typeof contextSchema>>( // [!code highlight] (_state, runtime) => { const userRole = runtime.context.userRole; const basePrompt = "You are a helpful assistant."; if (userRole === "expert") { return `${basePrompt} Provide detailed technical responses.`; } else if (userRole === "beginner") { return `${basePrompt} Explain concepts simply and avoid jargon.`; } return basePrompt; // [!code highlight] } ); const agent = createAgent({ model, tools, middleware: [userRolePrompt], contextSchema, }); await agent.invoke( { messages: [new HumanMessage("Explain async programming")] }, { configurable: { userRole: "expert", } } ); ```
yindo added the langchain label 2026-02-17 17:19:24 -05:00
yindo closed this issue 2026-02-17 17:19:24 -05:00
yindo changed title from [langchain]: Migration Guide - Dynamic prompts code snippet errors to [GH-ISSUE #1467] [langchain]: Migration Guide - Dynamic prompts code snippet errors 2026-06-05 17:25:39 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/docs#210