Skip to content

feat(prebuilt): add "system-prompt" mode to withAgentName#2009

Open
RajeshKumar11 wants to merge 3 commits intolangchain-ai:mainfrom
RajeshKumar11:rajesh/fix-1178-system-prompt-agent-name
Open

feat(prebuilt): add "system-prompt" mode to withAgentName#2009
RajeshKumar11 wants to merge 3 commits intolangchain-ai:mainfrom
RajeshKumar11:rajesh/fix-1178-system-prompt-agent-name

Conversation

@RajeshKumar11
Copy link

Summary

Fixes #1178

The existing "inline" mode in withAgentName / includeAgentName works by retroactively mutating past AIMessage content to inject XML tags:

<name>agentName</name><content>original response</content>

This breaks with providers that disallow editing message history:

  • OpenAI Responses API — past message IDs are immutable
  • Anthropic thinking blocks — thinking blocks in history cannot be modified/replayed

Fix

Add a new "system-prompt" mode to AgentNameMode that:

  1. Does NOT mutate past messages — leaves the entire conversation history untouched
  2. Injects an instruction into the system prompt telling the model to format its own output in <name>agentName</name><content>response</content> format
  3. Reuses the existing _removeInlineAgentName output processor — same XML extraction logic on the new AI message

Changes

  • libs/langgraph-core/src/prebuilt/agentName.ts

    • Extend AgentNameMode = "inline" | "system-prompt"
    • Add _addSystemPromptAgentName(messages, agentName) — appends instruction to existing SystemMessage, or prepends a new one if absent
    • Update withAgentName(model, mode, agentName?) to handle the new mode (optional agentName param, required for "system-prompt")
  • libs/langgraph-core/src/prebuilt/react_agent_executor.ts

    • Update includeAgentName type from "inline" | undefinedAgentNameMode
    • Pass name through to withAgentName at both call sites
  • libs/langgraph-supervisor/src/supervisor.ts

    • Pass supervisorName to withAgentName (needed for "system-prompt" mode)
  • Tests: 4 new unit tests for _addSystemPromptAgentName + 3 new parametrized supervisor test cases for "system-prompt" mode

Backward Compatibility

  • "inline" mode is completely unchanged — existing behaviour preserved
  • agentName param on withAgentName is optional — no breaking change to the signature
  • includeAgentName type widens from "inline" | undefinedAgentNameMode — fully compatible

Test Results

@langchain/langgraph:  1117 passed (0 failed)
@langchain/langgraph-supervisor: 10 passed (0 failed) — includes 3 new system-prompt cases
@langchain/langgraph-swarm: 1 passed

Fixes langchain-ai#1178

The existing "inline" mode works by retroactively mutating past
AIMessage content to inject XML name tags, which breaks with providers
that disallow editing message history (OpenAI Responses API, Anthropic
thinking blocks).

Add a new "system-prompt" mode to AgentNameMode that instead injects
an instruction into the system prompt telling the model to format its
own output as <name>agentName</name><content>response</content>.
This leaves past message history completely untouched.

Changes:
- Extend `AgentNameMode` type to `"inline" | "system-prompt"`
- Add `_addSystemPromptAgentName(messages, agentName)` helper that
  appends the formatting instruction to an existing SystemMessage or
  prepends a new one if none exists
- Update `withAgentName(model, mode, agentName?)` to accept an optional
  `agentName` param (required for "system-prompt" mode)
- Update `createReactAgent` to pass `name` through to `withAgentName`
- Update `createSupervisor` to pass `supervisorName` to `withAgentName`
- Add unit tests for `_addSystemPromptAgentName` covering: prepend when
  no system message, append to existing, non-string content, past
  AIMessages untouched
- Add "system-prompt" parametrized test cases to supervisor tests
@changeset-bot
Copy link

changeset-bot bot commented Feb 27, 2026

⚠️ No Changeset found

Latest commit: 6b6b398

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

prebuilt: includeAgentName and withAgentName won't work if provider disallows editing AIMessage history

1 participant