@@ -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
453526class 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