Skip to content

Commit 53e9813

Browse files
authored
Python: Include InvokeAzureAgent input.arguments in agent text (microsoft#7902) (microsoft#8547)
* Python: Include InvokeAzureAgent input.arguments in agent text Fold evaluated input.arguments into the text passed to agent.run() so argument-only InvokeAzureAgent actions no longer silently invoke with an empty string. * Python: skip implicit fallback when InvokeAzureAgent arguments are present Addresses review on microsoft#7907: arguments-only actions no longer append System.LastMessage / Workflow.Inputs fallback text to their structured fields in chained workflows. Adds a regression test with poisoned fallback state.
1 parent 7232569 commit 53e9813

2 files changed

Lines changed: 145 additions & 37 deletions

File tree

‎python/packages/declarative/agent_framework_declarative/_workflows/_executors_agents.py‎

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,12 @@ async def _get_conversation_messages_path(
682682
async def _build_input_text(self, state: Any, arguments: dict[str, Any], messages_expr: Any) -> str:
683683
"""Build input text from arguments and messages.
684684
685+
``input.arguments`` are formatted as ``key: value`` lines (same shape as the
686+
multi-value ``Workflow.Inputs`` fallback) and included in the text sent to
687+
``agent.run()``. Python's agent ``run()`` has no separate structured-inputs
688+
channel, so arguments must be folded into this text rather than discarded
689+
(#7902).
690+
685691
Args:
686692
state: Workflow state for expression evaluation
687693
arguments: Input arguments to evaluate
@@ -690,55 +696,61 @@ async def _build_input_text(self, state: Any, arguments: dict[str, Any], message
690696
Returns:
691697
Input text for the agent
692698
"""
693-
# Evaluate arguments
694699
evaluated_args: dict[str, Any] = {}
695700
for key, value in arguments.items():
696701
evaluated_args[key] = state.eval_if_expression(value)
702+
args_text = "\n".join(f"{k}: {v}" for k, v in evaluated_args.items()) if evaluated_args else ""
697703

698-
# Evaluate messages/input
704+
messages_text = ""
699705
if messages_expr:
700706
evaluated_input: Any = state.eval_if_expression(messages_expr)
701707
if isinstance(evaluated_input, str):
702-
return evaluated_input
703-
if isinstance(evaluated_input, list) and evaluated_input:
708+
messages_text = evaluated_input
709+
elif isinstance(evaluated_input, list) and evaluated_input:
704710
# Extract text from last message
705711
last: Any = evaluated_input[-1] # type: ignore
706712
if isinstance(last, str):
707-
return last
708-
if isinstance(last, dict):
713+
messages_text = last
714+
elif isinstance(last, dict):
709715
last_dict = cast(dict[str, Any], last)
710716
content_val: Any = last_dict.get("content", last_dict.get("text", ""))
711-
return str(content_val) if content_val else ""
712-
if last is not None and hasattr(last, "text"): # type: ignore
713-
return str(getattr(last, "text", "")) # type: ignore
714-
if evaluated_input:
715-
return str(cast(Any, evaluated_input))
716-
return ""
717-
718-
# Fallback chain for implicit input (like .NET conversationId pattern):
719-
# 1. Local.input / Local.userInput (explicit turn state)
720-
# 2. System.LastMessage.Text (previous agent's response)
721-
# 3. Workflow.Inputs (first agent gets workflow inputs)
722-
input_text: str = str(state.get("Local.input") or state.get("Local.userInput") or "")
723-
if not input_text:
724-
# Try System.LastMessage.Text (used by external loop and agent chaining)
725-
last_message: Any = state.get("System.LastMessage")
726-
if isinstance(last_message, dict):
727-
last_msg_dict = cast(dict[str, Any], last_message)
728-
text_val: Any = last_msg_dict.get("Text", "")
729-
input_text = str(text_val) if text_val else ""
730-
if not input_text:
731-
# Fall back to workflow inputs (for first agent in chain)
732-
inputs: Any = state.get("Workflow.Inputs")
733-
if isinstance(inputs, dict):
734-
inputs_dict = cast(dict[str, Any], inputs)
735-
# If single input, use its value directly
736-
if len(inputs_dict) == 1:
737-
input_text = str(next(iter(inputs_dict.values())))
738-
else:
739-
# Multiple inputs - format as key: value pairs
740-
input_text = "\n".join(f"{k}: {v}" for k, v in inputs_dict.items())
741-
return input_text if input_text else ""
717+
messages_text = str(content_val) if content_val else ""
718+
elif last is not None and hasattr(last, "text"): # type: ignore
719+
messages_text = str(getattr(last, "text", "")) # type: ignore
720+
elif evaluated_input:
721+
messages_text = str(cast(Any, evaluated_input))
722+
elif not evaluated_args:
723+
# Fallback chain for implicit input (like .NET conversationId pattern):
724+
# Only when neither explicit messages nor explicit arguments are present.
725+
# Otherwise arguments-only actions (e.g. customer-support TicketingAgent)
726+
# would append System.LastMessage (prior agent's response) or
727+
# Workflow.Inputs to their structured fields in chained workflows.
728+
# 1. Local.input / Local.userInput (explicit turn state)
729+
# 2. System.LastMessage.Text (previous agent's response)
730+
# 3. Workflow.Inputs (first agent gets workflow inputs)
731+
messages_text = str(state.get("Local.input") or state.get("Local.userInput") or "")
732+
if not messages_text:
733+
# Try System.LastMessage.Text (used by external loop and agent chaining)
734+
last_message: Any = state.get("System.LastMessage")
735+
if isinstance(last_message, dict):
736+
last_msg_dict = cast(dict[str, Any], last_message)
737+
text_val: Any = last_msg_dict.get("Text", "")
738+
messages_text = str(text_val) if text_val else ""
739+
if not messages_text:
740+
# Fall back to workflow inputs (for first agent in chain)
741+
inputs: Any = state.get("Workflow.Inputs")
742+
if isinstance(inputs, dict):
743+
inputs_dict = cast(dict[str, Any], inputs)
744+
# If single input, use its value directly
745+
if len(inputs_dict) == 1:
746+
messages_text = str(next(iter(inputs_dict.values())))
747+
else:
748+
# Multiple inputs - format as key: value pairs
749+
messages_text = "\n".join(f"{k}: {v}" for k, v in inputs_dict.items())
750+
751+
if args_text and messages_text:
752+
return f"{args_text}\n{messages_text}"
753+
return args_text or messages_text or ""
742754

743755
def _get_agent(self, agent_name: str, ctx: WorkflowContext[Any, Any]) -> Any:
744756
"""Get agent from registry (sync helper for response handler)."""

‎python/packages/declarative/tests/test_graph_coverage.py‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,102 @@ async def test_agent_executor_build_input_text_fallback_chain(self, mock_context
977977
input_text = await executor._build_input_text(state, {}, None)
978978
assert input_text == "workflow input"
979979

980+
async def test_agent_executor_build_input_text_arguments_only_skips_implicit_fallback(
981+
self, mock_context, mock_state
982+
):
983+
"""Arguments-only actions must not append implicit fallback input (#7902 review)."""
984+
from agent_framework_declarative._workflows._executors_agents import (
985+
InvokeAzureAgentExecutor,
986+
)
987+
988+
state = DeclarativeWorkflowState(mock_state)
989+
state.initialize({"query": "workflow input"})
990+
state.set("System.LastMessage", {"Text": "prior agent response", "Id": "1"})
991+
state.set("Local.input", "turn input")
992+
993+
action_def = {"kind": "InvokeAzureAgent", "agent": "Test"}
994+
executor = InvokeAzureAgentExecutor(action_def)
995+
996+
input_text = await executor._build_input_text(
997+
state,
998+
{"IssueDescription": "Printer jammed"},
999+
None,
1000+
)
1001+
1002+
assert input_text == "IssueDescription: Printer jammed"
1003+
1004+
async def test_agent_executor_build_input_text_includes_arguments_only(self, mock_context, mock_state):
1005+
"""Regression for #7902: input.arguments must reach the agent when messages are omitted."""
1006+
from agent_framework_declarative._workflows._executors_agents import (
1007+
InvokeAzureAgentExecutor,
1008+
)
1009+
1010+
state = DeclarativeWorkflowState(mock_state)
1011+
state.initialize()
1012+
1013+
action_def = {"kind": "InvokeAzureAgent", "agent": "Test"}
1014+
executor = InvokeAzureAgentExecutor(action_def)
1015+
1016+
input_text = await executor._build_input_text(
1017+
state,
1018+
{
1019+
"IssueDescription": "The printer on the 3rd floor is jammed.",
1020+
"AttemptedResolutionSteps": "Restarted the printer twice.",
1021+
},
1022+
None,
1023+
)
1024+
1025+
assert "IssueDescription: The printer on the 3rd floor is jammed." in input_text
1026+
assert "AttemptedResolutionSteps: Restarted the printer twice." in input_text
1027+
1028+
@_requires_powerfx
1029+
async def test_agent_executor_build_input_text_combines_arguments_and_messages(
1030+
self, mock_context, mock_state
1031+
):
1032+
"""input.arguments are kept alongside explicit messages (#7902)."""
1033+
from agent_framework_declarative._workflows._executors_agents import (
1034+
InvokeAzureAgentExecutor,
1035+
)
1036+
1037+
state = DeclarativeWorkflowState(mock_state)
1038+
state.initialize()
1039+
state.set("Local.userInput", "Please help with this ticket.")
1040+
1041+
action_def = {"kind": "InvokeAzureAgent", "agent": "Test"}
1042+
executor = InvokeAzureAgentExecutor(action_def)
1043+
1044+
input_text = await executor._build_input_text(
1045+
state,
1046+
{"IssueDescription": "Printer jammed"},
1047+
"=Local.userInput",
1048+
)
1049+
1050+
assert input_text == "IssueDescription: Printer jammed\nPlease help with this ticket."
1051+
1052+
@_requires_powerfx
1053+
async def test_agent_executor_build_input_text_evaluates_argument_expressions(
1054+
self, mock_context, mock_state
1055+
):
1056+
"""Argument values that are expressions are evaluated before formatting (#7902)."""
1057+
from agent_framework_declarative._workflows._executors_agents import (
1058+
InvokeAzureAgentExecutor,
1059+
)
1060+
1061+
state = DeclarativeWorkflowState(mock_state)
1062+
state.initialize()
1063+
state.set("Local.issue", "Network outage")
1064+
1065+
action_def = {"kind": "InvokeAzureAgent", "agent": "Test"}
1066+
executor = InvokeAzureAgentExecutor(action_def)
1067+
1068+
input_text = await executor._build_input_text(
1069+
state,
1070+
{"IssueDescription": "=Local.issue"},
1071+
None,
1072+
)
1073+
1074+
assert input_text == "IssueDescription: Network outage"
1075+
9801076
async def test_agent_executor_build_input_text_from_system_last_message(self, mock_context, mock_state):
9811077
"""Test _build_input_text falls back to system.LastMessage.Text."""
9821078
from agent_framework_declarative._workflows._executors_agents import (

0 commit comments

Comments
 (0)