Skip to content

Commit 6969706

Browse files
cechetaTaoChenOSUmoonbox3
authored
Python: Add context_providers and description to workflow.as_agent() (#4651)
* Add context_providers and description to `workflow.as_agent()` * Add default workflow name and description * Positional * Move import --------- Co-authored-by: Tao Chen <taochen@microsoft.com> Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
1 parent fe4cd3c commit 6969706

2 files changed

Lines changed: 55 additions & 5 deletions

File tree

python/packages/core/agent_framework/_workflows/_workflow.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
import types
1212
import uuid
1313
from collections.abc import AsyncIterable, Awaitable, Callable, Mapping, Sequence
14-
from typing import Any, Literal, overload
14+
from typing import TYPE_CHECKING, Any, Literal, overload
1515

16+
from .._sessions import ContextProvider
1617
from .._types import ResponseStream
1718
from ..observability import OtelAttr, capture_exception, create_workflow_span
18-
from ._agent import WorkflowAgent
1919
from ._checkpoint import CheckpointStorage
2020
from ._const import DEFAULT_MAX_ITERATIONS, GLOBAL_KWARGS_KEY, WORKFLOW_RUN_KWARGS_KEY
2121
from ._edge import (
@@ -35,6 +35,9 @@
3535
from ._state import State
3636
from ._typing_utils import is_instance_of, try_coerce_to_type
3737

38+
if TYPE_CHECKING:
39+
from ._agent import WorkflowAgent
40+
3841
logger = logging.getLogger(__name__)
3942

4043

@@ -910,7 +913,14 @@ def output_types(self) -> list[type[Any] | types.UnionType]:
910913

911914
return list(output_types)
912915

913-
def as_agent(self, name: str | None = None) -> WorkflowAgent:
916+
def as_agent(
917+
self,
918+
name: str | None = None,
919+
*,
920+
description: str | None = None,
921+
context_providers: Sequence[ContextProvider] | None = None,
922+
**kwargs: Any,
923+
) -> WorkflowAgent:
914924
"""Create a WorkflowAgent that wraps this workflow.
915925
916926
The returned agent converts standard agent inputs (strings, Message, or lists of these)
@@ -924,7 +934,10 @@ def as_agent(self, name: str | None = None) -> WorkflowAgent:
924934
initialization will fail with a ValueError.
925935
926936
Args:
927-
name: Optional name for the agent. If None, a default name will be generated.
937+
name: Optional name for the agent. Defaults to workflow name.
938+
description: Optional description of the agent. Defaults to workflow description.
939+
context_providers: Optional sequence of context providers for the agent.
940+
**kwargs: Additional keyword arguments passed to BaseAgent.
928941
929942
Returns:
930943
A WorkflowAgent instance that wraps this workflow.
@@ -935,4 +948,10 @@ def as_agent(self, name: str | None = None) -> WorkflowAgent:
935948
# Import here to avoid circular imports
936949
from ._agent import WorkflowAgent
937950

938-
return WorkflowAgent(workflow=self, name=name)
951+
return WorkflowAgent(
952+
workflow=self,
953+
name=name if name is not None else self.name,
954+
description=description if description is not None else self.description,
955+
context_providers=context_providers,
956+
**kwargs,
957+
)

python/packages/core/tests/workflow/test_workflow_agent.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,37 @@ def test_workflow_as_agent_method(self) -> None:
313313
assert isinstance(agent_no_name, WorkflowAgent)
314314
assert agent_no_name.workflow is workflow
315315

316+
def test_workflow_as_agent_with_description_and_context_providers(self) -> None:
317+
"""Test that Workflow.as_agent() forwards description and context_providers."""
318+
executor = SimpleExecutor(id="executor1", response_text="Response")
319+
workflow = WorkflowBuilder(start_executor=executor).build()
320+
321+
history_provider = InMemoryHistoryProvider()
322+
agent = workflow.as_agent(
323+
name="MyAgent",
324+
description="A test agent",
325+
context_providers=[history_provider],
326+
)
327+
328+
assert isinstance(agent, WorkflowAgent)
329+
assert agent.name == "MyAgent"
330+
assert agent.description == "A test agent"
331+
assert history_provider in agent.context_providers
332+
333+
def test_workflow_as_agent_defaults_name_and_description_from_workflow(self) -> None:
334+
"""Test that as_agent() defaults name and description to the workflow's own values."""
335+
executor = SimpleExecutor(id="executor1", response_text="Response")
336+
workflow = WorkflowBuilder(
337+
start_executor=executor,
338+
name="my-workflow",
339+
description="Workflow description",
340+
).build()
341+
342+
agent = workflow.as_agent()
343+
344+
assert agent.name == "my-workflow"
345+
assert agent.description == "Workflow description"
346+
316347
def test_workflow_as_agent_cannot_handle_agent_inputs(self) -> None:
317348
"""Test that Workflow.as_agent() raises an error if the start executor cannot handle agent inputs."""
318349

0 commit comments

Comments
 (0)