Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ async def aggregate(self, results: list[AgentExecutorResponse], ctx: WorkflowCon
else:
ret = await asyncio.to_thread(self._callback, results)

# Async callable objects and wrappers returning a coroutine are not coroutine functions
if inspect.isawaitable(ret):
ret = await ret

# If the callback returned a value, finalize the workflow with it
if ret is not None:
await ctx.yield_output(ret)
Expand Down
28 changes: 28 additions & 0 deletions python/packages/orchestrations/tests/test_concurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,34 @@ def summarize_sync(results: list[AgentExecutorResponse], _ctx: WorkflowContext[A
assert output == "One | Two"


class _AsyncCallableAggregator:
"""Aggregator object whose __call__ is async, for example one that holds a client."""

async def __call__(self, results: list[AgentExecutorResponse]) -> str:
return " | ".join(sorted(r.agent_response.text for r in results))


async def _join_texts(results: list[AgentExecutorResponse]) -> str:
return " | ".join(sorted(r.agent_response.text for r in results))


@pytest.mark.parametrize(
"aggregator",
[
pytest.param(_AsyncCallableAggregator(), id="async_callable_object"),
pytest.param(lambda results: _join_texts(results), id="callable_returning_coroutine"),
],
)
async def test_concurrent_custom_aggregator_awaits_awaitable_result(aggregator: Any) -> None:
e1 = _FakeAgentExec("agentA", "One")
e2 = _FakeAgentExec("agentB", "Two")

wf = ConcurrentBuilder(participants=[e1, e2]).with_aggregator(aggregator).build()
result = await wf.run("prompt: awaitable aggregator")

assert result.get_outputs() == ["One | Two"]


def test_concurrent_custom_aggregator_uses_callback_name_for_id() -> None:
e1 = _FakeAgentExec("agentA", "One")
e2 = _FakeAgentExec("agentB", "Two")
Expand Down
Loading