Skip to content

Tools

Helpers for integrating Temporal Activities with LangGraph tools.

activity_as_tool

activity_as_tool

activity_as_tool(
    activity_fn: Callable[..., Any],
    *,
    name: str | None = None,
    description: str | None = None,
    task_queue: str | None = None,
) -> Callable[..., Any]

Convert a Temporal Activity function into a LangGraph-compatible tool.

This wraps an @activity.defn decorated function so it can be used as a tool within LangGraph nodes. When the tool is called inside a node Activity, it executes the target Activity via the Temporal worker, providing durable execution guarantees for tool invocations.

Note: The returned tool is a regular callable that invokes the Activity function directly (in-process). For cross-worker Activity invocation, use Temporal's workflow.execute_activity() from within a Workflow.

Parameters:

Name Type Description Default
activity_fn Callable[..., Any]

A function decorated with @activity.defn.

required
name str | None

Override the tool name (defaults to function name).

None
description str | None

Override the tool description (defaults to docstring).

None
task_queue str | None

Task queue for Activity execution (for future use).

None

Returns:

Type Description
Callable[..., Any]

A callable that can be used as a LangGraph tool.

Example
@activity.defn
async def search_web(query: str) -> str:
    """Search the web for information."""
    ...

tools = [activity_as_tool(search_web)]
graph.add_node("tools", ToolNode(tools))
Source code in langgraph/temporal/tools.py
def activity_as_tool(
    activity_fn: Callable[..., Any],
    *,
    name: str | None = None,
    description: str | None = None,
    task_queue: str | None = None,
) -> Callable[..., Any]:
    """Convert a Temporal Activity function into a LangGraph-compatible tool.

    This wraps an `@activity.defn` decorated function so it can be used as
    a tool within LangGraph nodes. When the tool is called inside a node
    Activity, it executes the target Activity via the Temporal worker,
    providing durable execution guarantees for tool invocations.

    Note: The returned tool is a regular callable that invokes the Activity
    function directly (in-process). For cross-worker Activity invocation,
    use Temporal's `workflow.execute_activity()` from within a Workflow.

    Args:
        activity_fn: A function decorated with `@activity.defn`.
        name: Override the tool name (defaults to function name).
        description: Override the tool description (defaults to docstring).
        task_queue: Task queue for Activity execution (for future use).

    Returns:
        A callable that can be used as a LangGraph tool.

    Example:
        ```python
        @activity.defn
        async def search_web(query: str) -> str:
            \"\"\"Search the web for information.\"\"\"
            ...

        tools = [activity_as_tool(search_web)]
        graph.add_node("tools", ToolNode(tools))
        ```
    """
    tool_name: str = name or getattr(activity_fn, "__name__", None) or "activity_tool"
    tool_description: str = description or getattr(activity_fn, "__doc__", None) or ""

    async def tool_wrapper(*args: Any, **kwargs: Any) -> Any:
        return await activity_fn(*args, **kwargs)

    tool_wrapper.__name__ = tool_name
    tool_wrapper.__doc__ = tool_description
    tool_wrapper.__qualname__ = tool_name

    # Preserve original function metadata for introspection
    tool_wrapper._activity_fn = activity_fn  # type: ignore[attr-defined]
    tool_wrapper._task_queue = task_queue  # type: ignore[attr-defined]

    return tool_wrapper