Skip to content

fix: ensure single system message and merge historical system prompts#181

Open
728100660 wants to merge 1 commit intodataelement:mainfrom
728100660:fix/system-message-order
Open

fix: ensure single system message and merge historical system prompts#181
728100660 wants to merge 1 commit intodataelement:mainfrom
728100660:fix/system-message-order

Conversation

@728100660
Copy link
Contributor

Problem

Some LLM APIs (e.g. OpenAI/Claude-compatible) require that:

  • Only one system message exists
  • The system message must be the first message

However, the current implementation directly prepends a new system
message while keeping existing ones from messages, which may lead to:

  • Multiple system messages
  • System message not at the first position

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 messages

  • Merge it with the constructed system_prompt

  • Use a prefix-aware merge strategy:

    • Detect the longest common prefix (from the start)
    • Keep shared content only once
    • Append the remaining parts
  • Ensure:

    • Only one system message exists
    • It is always placed at the first position

Benefits

  • Prevents API errors caused by invalid message ordering
  • Fixes real issues in Feishu human-in-the-loop trigger scenarios
  • Avoids duplicated prompt content
  • Preserves additional instructions from historical context
  • Keeps implementation simple and efficient

Notes

  • Does not introduce complex diff/LCS logic
  • Follows existing code structure
  • Safe fallback when no meaningful prefix match exists

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.

1 participant