Skip to content

Commit 103229b

Browse files
committed
Python: fix(declarative): keep internal workflow-routing kwargs out of the Agent.run splat
The workflow state bag carries internal underscore-keyed copies for nested executor routing; the declarative agent step splatted the whole bag into Agent.run, which takes no **kwargs, so _raw_function_invocation_kwargs raised TypeError on the first agent step under AG-UI (#8413). The splat now forwards public parameters only, while additional_function_arguments still carries the full bag for nested routing.
1 parent c97528b commit 103229b

2 files changed

Lines changed: 81 additions & 7 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,10 @@ async def _invoke_agent_and_store_results(
806806
# Merge caller-provided options to avoid duplicate keyword argument
807807
options = dict(run_kwargs.get("options") or {})
808808
options["additional_function_arguments"] = run_kwargs
809-
# Exclude 'options' from splat to avoid TypeError on duplicate keyword
810-
run_kwargs = {k: v for k, v in run_kwargs.items() if k != "options"}
809+
# Exclude 'options' from splat to avoid TypeError on duplicate keyword,
810+
# and keep internal workflow-routing copies (stored under underscore
811+
# keys for nested executors) out of the public Agent.run signature
812+
run_kwargs = {k: v for k, v in run_kwargs.items() if k != "options" and not k.startswith("_")}
811813

812814
# Use run() method to get properly structured messages (including tool calls and results)
813815
# This is critical for multi-turn conversations where tool calls must be followed

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

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ def mock_set(key, value):
427427
async def test_invoke_agent_not_found(self, mock_context, mock_state):
428428
"""Test InvokeAzureAgentExecutor raises error when agent not found."""
429429
from agent_framework.exceptions import AgentInvalidRequestException
430-
431430
from agent_framework_declarative._workflows import (
432431
InvokeAzureAgentExecutor,
433432
)
@@ -449,6 +448,80 @@ async def test_invoke_agent_not_found(self, mock_context, mock_state):
449448
assert "non_existent_agent" in str(exc_info.value)
450449
assert "not found in registry" in str(exc_info.value)
451450

451+
@pytest.mark.asyncio
452+
async def test_invoke_agent_skips_internal_workflow_kwargs(self, mock_context, mock_state):
453+
"""Internal underscore keys in the run-kwargs bag must not reach Agent.run.
454+
455+
Regression for #8413: the workflow state bag carries internal routing
456+
copies (e.g. `_raw_function_invocation_kwargs`) next to the public
457+
kwargs, and the declarative agent step used to splat the whole bag into
458+
`Agent.run`, which takes no `**kwargs` and raised TypeError.
459+
"""
460+
from types import SimpleNamespace
461+
462+
from agent_framework_declarative._workflows import InvokeAzureAgentExecutor
463+
464+
state = DeclarativeWorkflowState(mock_state)
465+
state.initialize()
466+
467+
class _StrictAgent:
468+
"""Same public surface as Agent.run: no **kwargs catch-all."""
469+
470+
def __init__(self) -> None:
471+
self.received_options: dict[str, Any] | None = None
472+
self.received_public_kwargs: dict[str, Any] = {}
473+
474+
async def run(
475+
self,
476+
messages,
477+
options=None,
478+
function_invocation_kwargs=None,
479+
client_kwargs=None,
480+
session=None,
481+
tools=None,
482+
stream=False,
483+
):
484+
self.received_options = options
485+
self.received_public_kwargs = {
486+
"function_invocation_kwargs": function_invocation_kwargs,
487+
"client_kwargs": client_kwargs,
488+
}
489+
return SimpleNamespace(text="ok", messages=[], tool_calls=[])
490+
491+
agent = _StrictAgent()
492+
run_kwargs_bag = {
493+
"function_invocation_kwargs": {"forwarded_props": {"a": 1}},
494+
"_raw_function_invocation_kwargs": {"forwarded_props": {"a": 1}},
495+
}
496+
mock_context.get_state = MagicMock(
497+
side_effect=lambda key, default=None: run_kwargs_bag if key == "_workflow_run_kwargs" else default
498+
)
499+
500+
executor = InvokeAzureAgentExecutor(
501+
{"kind": "InvokeAzureAgent", "agent": "StubAgent", "input": "hello"},
502+
agents={"StubAgent": agent},
503+
)
504+
505+
# Before the fix this raises TypeError: Agent.run() got an unexpected
506+
# keyword argument '_raw_function_invocation_kwargs'.
507+
await executor._invoke_agent_and_store_results(
508+
agent,
509+
"StubAgent",
510+
"hello",
511+
state,
512+
mock_context,
513+
messages_var=None,
514+
response_obj_var=None,
515+
result_property=None,
516+
auto_send=False,
517+
)
518+
519+
assert agent.received_options is not None
520+
# The full bag still reaches tool forwarding, internal copies included.
521+
forwarded = agent.received_options["additional_function_arguments"]
522+
assert forwarded["_raw_function_invocation_kwargs"] == {"forwarded_props": {"a": 1}}
523+
assert forwarded["function_invocation_kwargs"] == {"forwarded_props": {"a": 1}}
524+
452525

453526
class TestHumanInputExecutors:
454527
"""Tests for human input executors."""
@@ -1824,7 +1897,6 @@ async def test_workflow_run_kwargs_survive_file_checkpoint_restore(
18241897
RESOLVED_WORKFLOW_RUN_KWARGS_KEY,
18251898
WORKFLOW_RUN_KWARGS_KEY,
18261899
)
1827-
18281900
from agent_framework_declarative._workflows._executors_external_input import ExternalInputResponse
18291901

18301902
storage = FileCheckpointStorage(
@@ -1911,7 +1983,9 @@ def build_workflow() -> tuple[Any, Any]:
19111983
)
19121984
expected_run_kwargs = {kwargs_channel: invocation_kwargs, raw_key: invocation_kwargs}
19131985
assert call_kwargs[kwargs_channel] == invocation_kwargs
1914-
assert call_kwargs[raw_key] == invocation_kwargs
1986+
# The internal raw copy stays routing state for nested executors;
1987+
# it is not a public Agent.run parameter (#8413).
1988+
assert raw_key not in call_kwargs
19151989
else:
19161990
expected_run_kwargs = {kwargs_channel: {"shared": "G", "specific": "S"}}
19171991
assert call_kwargs[kwargs_channel] == {"shared": "G", "specific": "S"}
@@ -1924,7 +1998,6 @@ async def test_invoke_agent_forwards_kwargs(self):
19241998
"""InvokeAzureAgentExecutor should forward run_kwargs to agent.run()."""
19251999
from agent_framework._workflows._const import WORKFLOW_RUN_KWARGS_KEY
19262000
from agent_framework._workflows._state import State
1927-
19282001
from agent_framework_declarative._workflows._executors_agents import (
19292002
InvokeAzureAgentExecutor,
19302003
)
@@ -1996,7 +2069,6 @@ async def test_invoke_agent_merges_caller_options(self):
19962069
"""Caller-provided options in run_kwargs should be merged, not cause TypeError."""
19972070
from agent_framework._workflows._const import WORKFLOW_RUN_KWARGS_KEY
19982071
from agent_framework._workflows._state import State
1999-
20002072
from agent_framework_declarative._workflows._executors_agents import (
20012073
InvokeAzureAgentExecutor,
20022074
)

0 commit comments

Comments
 (0)