This commit is contained in:
Timothy Jaeryang Baek
2026-03-24 17:46:33 -05:00
parent c0fcbc5b4c
commit 631bd20c35
2 changed files with 32 additions and 0 deletions
+27
View File
@@ -330,6 +330,33 @@ def pop_system_message(messages: list[dict]) -> tuple[Optional[dict], list[dict]
return get_system_message(messages), remove_system_message(messages)
def merge_system_messages(messages: list[dict]) -> list[dict]:
"""
Merge all system messages into one at position 0.
Some chat templates (e.g. Qwen) require exactly one system
message at the start. Multiple pipeline stages may each
insert their own system message; this function consolidates
them.
"""
system_contents: list[str] = []
other_messages: list[dict] = []
for message in messages:
if message.get('role') == 'system':
content = get_content_from_message(message)
if content:
system_contents.append(content)
else:
other_messages.append(message)
if not system_contents:
return other_messages
merged = {'role': 'system', 'content': '\n'.join(system_contents)}
return [merged, *other_messages]
def update_message_content(message: dict, content: str, append: bool = True) -> dict:
if isinstance(message['content'], list):
for item in message['content']: