Skip to content

Commit 0d09f7b

Browse files
authored
feat: add system_prompt to Agent run parameters (#9778)
* enhancement: add system_prompt to Agent run parameters * add reno * add test
1 parent 34f1a04 commit 0d09f7b

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

haystack/components/agents/agent.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ def _initialize_fresh_execution(
255255
messages: list[ChatMessage],
256256
streaming_callback: Optional[StreamingCallbackT],
257257
requires_async: bool,
258+
system_prompt: Optional[str] = None,
258259
**kwargs,
259260
) -> _ExecutionContext:
260261
"""
@@ -263,10 +264,12 @@ def _initialize_fresh_execution(
263264
:param messages: List of ChatMessage objects to start the agent with.
264265
:param streaming_callback: Optional callback for streaming responses.
265266
:param requires_async: Whether the agent run requires asynchronous execution.
267+
:param system_prompt: System prompt for the agent. If provided, it overrides the default system prompt.
266268
:param kwargs: Additional data to pass to the State used by the Agent.
267269
"""
268-
if self.system_prompt is not None:
269-
messages = [ChatMessage.from_system(self.system_prompt)] + messages
270+
system_prompt = system_prompt or self.system_prompt
271+
if system_prompt is not None:
272+
messages = [ChatMessage.from_system(system_prompt)] + messages
270273

271274
if all(m.is_from(ChatRole.SYSTEM) for m in messages):
272275
logger.warning("All messages provided to the Agent component are system messages. This is not recommended.")
@@ -443,6 +446,7 @@ def run(
443446
*,
444447
break_point: Optional[AgentBreakpoint] = None,
445448
snapshot: Optional[AgentSnapshot] = None,
449+
system_prompt: Optional[str] = None,
446450
**kwargs: Any,
447451
) -> dict[str, Any]:
448452
"""
@@ -455,6 +459,7 @@ def run(
455459
for "tool_invoker".
456460
:param snapshot: A dictionary containing a snapshot of a previously saved agent execution. The snapshot contains
457461
the relevant information to restart the Agent execution from where it left off.
462+
:param system_prompt: System prompt for the agent. If provided, it overrides the default system prompt.
458463
:param kwargs: Additional data to pass to the State schema used by the Agent.
459464
The keys must match the schema defined in the Agent's `state_schema`.
460465
:returns:
@@ -482,7 +487,11 @@ def run(
482487
)
483488
else:
484489
exe_context = self._initialize_fresh_execution(
485-
messages=messages, streaming_callback=streaming_callback, requires_async=False, **kwargs
490+
messages=messages,
491+
streaming_callback=streaming_callback,
492+
requires_async=False,
493+
system_prompt=system_prompt,
494+
**kwargs,
486495
)
487496

488497
with self._create_agent_span() as span:
@@ -558,6 +567,7 @@ async def run_async(
558567
*,
559568
break_point: Optional[AgentBreakpoint] = None,
560569
snapshot: Optional[AgentSnapshot] = None,
570+
system_prompt: Optional[str] = None,
561571
**kwargs: Any,
562572
) -> dict[str, Any]:
563573
"""
@@ -574,6 +584,7 @@ async def run_async(
574584
for "tool_invoker".
575585
:param snapshot: A dictionary containing a snapshot of a previously saved agent execution. The snapshot contains
576586
the relevant information to restart the Agent execution from where it left off.
587+
:param system_prompt: System prompt for the agent. If provided, it overrides the default system prompt.
577588
:param kwargs: Additional data to pass to the State schema used by the Agent.
578589
The keys must match the schema defined in the Agent's `state_schema`.
579590
:returns:
@@ -601,7 +612,11 @@ async def run_async(
601612
)
602613
else:
603614
exe_context = self._initialize_fresh_execution(
604-
messages=messages, streaming_callback=streaming_callback, requires_async=False, **kwargs
615+
messages=messages,
616+
streaming_callback=streaming_callback,
617+
requires_async=False,
618+
system_prompt=system_prompt,
619+
**kwargs,
605620
)
606621

607622
with self._create_agent_span() as span:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
enhancements:
3+
- |
4+
Added `system_prompt` to agent run parameters to enhance customization and control over agent behavior.

test/components/agents/test_agent.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,17 @@ def test_run_with_system_prompt(self, weather_tool):
732732
response = agent.run([ChatMessage.from_user("What is the weather in Berlin?")])
733733
assert response["messages"][0].text == "This is a system prompt."
734734

735+
def test_run_with_system_prompt_run_param(self, weather_tool):
736+
chat_generator = MockChatGeneratorWithoutRunAsync()
737+
agent = Agent(
738+
chat_generator=chat_generator, tools=[weather_tool], system_prompt="This is the init system prompt."
739+
)
740+
agent.warm_up()
741+
response = agent.run(
742+
[ChatMessage.from_user("What is the weather in Berlin?")], system_prompt="This is the run system prompt."
743+
)
744+
assert response["messages"][0].text == "This is the run system prompt."
745+
735746
def test_run_not_warmed_up(self, weather_tool):
736747
chat_generator = MockChatGeneratorWithoutRunAsync()
737748
chat_generator.warm_up = MagicMock()

0 commit comments

Comments
 (0)