Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions python/packages/core/agent_framework/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,13 @@ def prepend_instructions_to_messages(
if isinstance(instructions, str):
instructions = [instructions]

# An empty instruction list (or all-empty strings) adds nothing; without
# this a caller that passes an unset options default of "" gets a
# contentless system message injected ahead of the real conversation.
instructions = [part for part in instructions if part.strip()]
if not instructions:
return messages

# Skip instructions that are already present as the leading messages with the
# same role and text. This prevents duplicate system messages when
# instructions are injected by multiple layers (e.g. Agent + chat client).
Expand Down
21 changes: 21 additions & 0 deletions python/packages/core/tests/test_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@


class TestPrependInstructionsEmpty:
def test_empty_string_instructions_add_no_message(self) -> None:
"""An unset "" instruction must not inject a contentless system message."""
from agent_framework import Message, prepend_instructions_to_messages

messages = [Message(role="user", contents=["hi"])]
out = prepend_instructions_to_messages(messages, "")
assert [(m.role, [getattr(c, "text", c) for c in m.contents]) for m in out] == [("user", ["hi"])]

def test_whitespace_only_instructions_add_no_message(self) -> None:
from agent_framework import prepend_instructions_to_messages

assert prepend_instructions_to_messages([], ["", " "]) == []

def test_real_instructions_still_prepend_verbatim(self) -> None:
from agent_framework import Message, prepend_instructions_to_messages

out = prepend_instructions_to_messages([], [" real "])
assert [(m.role, [getattr(c, "text", c) for c in m.contents]) for m in out] == [("system", [" real "])]
Loading