Skip to content

fix(agent): restore temporary system_prompt override for invocations#1844

Closed
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone:fix/temporary-system-prompt-override-344
Closed

fix(agent): restore temporary system_prompt override for invocations#1844
giulio-leone wants to merge 1 commit intostrands-agents:mainfrom
giulio-leone:fix/temporary-system-prompt-override-344

Conversation

@giulio-leone
Copy link
Contributor

Bug

After #289 removed kwargs spreading, there is no way to temporarily override the system prompt for a single agent invocation. Setting agent.system_prompt permanently changes it, losing the original.

Reported in: #344

Root cause

The system_prompt parameter was removed from __call__, invoke_async, and stream_async as part of the kwargs deprecation. Previously it flowed through kwargs into invocation_state, but it was never properly consumed as a temporary override — it just happened to be accessible.

Fix

  • Add system_prompt as a proper keyword parameter to __call__, invoke_async, and stream_async
  • Use a _UNSET sentinel to distinguish "not provided" from explicit None
  • Save and restore the original system prompt in a try/finally block around the invocation
  • The override is active only during the invocation — the original is always restored, even on exception

Usage

agent = Agent(system_prompt="You are a helpful assistant")

# Temporary override — original prompt restored after call

# Permanent change
agent.system_prompt = "New permanent prompt"

Testing

  • Updated test_agent__call__passes_invocation_state to verify system_prompt is now a proper parameter
  • Added test_agent__call__temporary_system_prompt_override: verifies the override is active during the call and the original is restored after
  • All 114 agent tests pass

Fixes #344

Re-add system_prompt parameter to __call__, invoke_async, and
stream_async. The override is applied only for the duration of the
invocation and the original system prompt is restored in a finally
block, even if the invocation raises.

Uses a sentinel object to distinguish 'not provided' from explicit
None, allowing callers to explicitly clear the system prompt for a
single call.

Fixes strands-agents#344

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@giulio-leone
Copy link
Contributor Author

Closing to reduce PR volume. Happy to resubmit individually if the team finds this fix useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Loss of Temporary System Prompt Override Functionality

1 participant