Skip to content

Commit b7ce0d6

Browse files
authored
fix(workflows): support keyword-only RunContext in functional workflows (#9397)
1 parent 1ba7425 commit b7ce0d6

2 files changed

Lines changed: 61 additions & 3 deletions

File tree

‎python/packages/core/agent_framework/_workflows/_functional.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,22 +2294,29 @@ async def _execute(self, ctx: RunContext, message: Any) -> Any:
22942294
# async def my_workflow(data: str, ctx: RunContext) -> str:
22952295
# async def my_workflow(data: str, ctx) -> str:
22962296
call_args: list[Any] = []
2297+
call_kwargs: dict[str, Any] = {}
22972298
message_injected = False
22982299

22992300
for param in params:
23002301
resolved = hints.get(param.name, param.annotation)
23012302
if resolved is RunContext or param.name == "ctx":
2302-
call_args.append(ctx)
2303+
if param.kind == inspect.Parameter.KEYWORD_ONLY:
2304+
call_kwargs[param.name] = ctx
2305+
else:
2306+
call_args.append(ctx)
23032307
elif not message_injected:
23042308
# First non-ctx param gets the message
2305-
call_args.append(message)
2309+
if param.kind == inspect.Parameter.KEYWORD_ONLY:
2310+
call_kwargs[param.name] = message
2311+
else:
2312+
call_args.append(message)
23062313
message_injected = True
23072314

23082315
root_task_ready = asyncio.Event()
23092316

23102317
async def _invoke_workflow() -> Any:
23112318
await root_task_ready.wait()
2312-
return await self._func(*call_args)
2319+
return await self._func(*call_args, **call_kwargs)
23132320

23142321
workflow_task = asyncio.create_task(_invoke_workflow())
23152322
ctx._root_task = workflow_task

‎python/packages/core/tests/workflow/test_functional_workflow.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4393,6 +4393,57 @@ async def wf(ctx: RunContext) -> str:
43934393
assert wf is not None
43944394

43954395

4396+
class TestKeywordOnlyRunContext:
4397+
"""Regression tests for Issue #9324: keyword-only RunContext parameter injection."""
4398+
4399+
async def test_workflow_with_keyword_only_run_context(self):
4400+
"""A @workflow function with a keyword-only RunContext parameter injects properly."""
4401+
4402+
@built_workflow
4403+
async def wf(text: str, *, ctx: RunContext) -> str:
4404+
ctx.set_state("text", text)
4405+
return ctx.get_state("text")
4406+
4407+
result = await wf.run("hello")
4408+
assert result.get_outputs() == ["hello"]
4409+
4410+
async def test_workflow_with_both_keyword_only_parameters(self):
4411+
"""A @workflow function with both message and context keyword-only injects properly."""
4412+
4413+
@built_workflow
4414+
async def wf(*, text: str, ctx: RunContext) -> str:
4415+
ctx.set_state("text", text)
4416+
return ctx.get_state("text")
4417+
4418+
result = await wf.run("hello")
4419+
assert result.get_outputs() == ["hello"]
4420+
4421+
async def test_workflow_with_keyword_only_ctx_named_param(self):
4422+
"""An unannotated keyword-only ctx parameter is injected properly."""
4423+
4424+
@built_workflow
4425+
async def wf(text: str, *, ctx) -> str:
4426+
ctx.set_state("val", text)
4427+
return ctx.get_state("val")
4428+
4429+
result = await wf.run("world")
4430+
assert result.get_outputs() == ["world"]
4431+
4432+
async def test_workflow_streaming_with_keyword_only_run_context(self):
4433+
"""Streaming mode properly passes keyword-only RunContext parameter."""
4434+
4435+
@built_workflow
4436+
async def wf(text: str, *, ctx: RunContext) -> str:
4437+
ctx.set_state("text", text)
4438+
return ctx.get_state("text")
4439+
4440+
stream = wf.run("hello", stream=True)
4441+
async for _ in stream:
4442+
pass
4443+
result = await stream.get_final_response()
4444+
assert result.get_outputs() == ["hello"]
4445+
4446+
43964447
class TestStaleResponsesRejected:
43974448
"""Regression for bug_014: stale responses after clean completion must be rejected."""
43984449

0 commit comments

Comments
 (0)