fix: ensure single system message and merge historical system prompts#181
Open
728100660 wants to merge 1 commit intodataelement:mainfrom
Open
fix: ensure single system message and merge historical system prompts#181728100660 wants to merge 1 commit intodataelement:mainfrom
728100660 wants to merge 1 commit intodataelement:mainfrom
Conversation
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.
Problem
Some LLM APIs (e.g. OpenAI/Claude-compatible) require that:
However, the current implementation directly prepends a new system
message while keeping existing ones from
messages, which may lead to:This causes errors such as:
"System message must be at the beginning"
Real Scenario
This issue was observed when integrating with Feishu human-in-the-loop workflows.
During agent trigger execution (
_invoke_agent_for_triggers),messages may already contain system prompts constructed from previous
interaction steps (e.g. workflow instructions or injected context).
When calling the LLM again, a new system prompt is prepended without
handling the existing ones, leading to multiple system messages and API errors.
Solution
Extract the last system message from input
messagesMerge it with the constructed
system_promptUse a prefix-aware merge strategy:
Ensure:
Benefits
Notes