feat(prebuilt): add "system-prompt" mode to withAgentName#2009
Open
RajeshKumar11 wants to merge 3 commits intolangchain-ai:mainfrom
Open
feat(prebuilt): add "system-prompt" mode to withAgentName#2009RajeshKumar11 wants to merge 3 commits intolangchain-ai:mainfrom
RajeshKumar11 wants to merge 3 commits intolangchain-ai:mainfrom
Conversation
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
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1178
The existing
"inline"mode inwithAgentName/includeAgentNameworks by retroactively mutating pastAIMessagecontent to inject XML tags:This breaks with providers that disallow editing message history:
Fix
Add a new
"system-prompt"mode toAgentNameModethat:<name>agentName</name><content>response</content>format_removeInlineAgentNameoutput processor — same XML extraction logic on the new AI messageChanges
libs/langgraph-core/src/prebuilt/agentName.tsAgentNameMode = "inline" | "system-prompt"_addSystemPromptAgentName(messages, agentName)— appends instruction to existingSystemMessage, or prepends a new one if absentwithAgentName(model, mode, agentName?)to handle the new mode (optionalagentNameparam, required for"system-prompt")libs/langgraph-core/src/prebuilt/react_agent_executor.tsincludeAgentNametype from"inline" | undefined→AgentNameModenamethrough towithAgentNameat both call siteslibs/langgraph-supervisor/src/supervisor.tssupervisorNametowithAgentName(needed for"system-prompt"mode)Tests: 4 new unit tests for
_addSystemPromptAgentName+ 3 new parametrized supervisor test cases for"system-prompt"modeBackward Compatibility
"inline"mode is completely unchanged — existing behaviour preservedagentNameparam onwithAgentNameis optional — no breaking change to the signatureincludeAgentNametype widens from"inline" | undefined→AgentNameMode— fully compatibleTest Results