Nodes and tools¶
Turn functions into nodes and tools, and route from inside a node. See Nodes and context and Agents and tools.
| Name | What it is |
|---|---|
| Nodes | |
node | Turn a function into a graph node. |
Node | A graph step: a callable plus its name and options. |
NodeMetadata | Serializable description of a node's options. |
NodeContext | Run information and helpers for a running node. |
| Routing from a node | |
Command | Node return value that updates state and routes. |
Send | Route to a node with its own input payload. |
| Tools | |
tool | Turn a function into a tool. |
Tool | A callable exposed to models, with validated input and output. |
ToolMetadata | Name, description and schemas of a tool. |
ToolContext | Run information passed to tools that declare a ToolContext. |
ToolCallResult | Validated result of a tool call and the arguments it ran with. |
call_tool | Validate arguments, run tool middleware and call a tool. |
Nodes¶
node¶
node(function=None, /, *, name=None, ref=None, timeout=None, goto=None) -> Node | Callable[[Callable[..., Any]], Node]
Turn a function into a graph node.
The function takes the state, optionally followed by a parameter annotated
NodeContext. It returns a dict update, the state it received changed in
place, Command, Send, END or None; see Nodes and
context for what each one does.
function | Callable[..., Any] | None |
Function to wrap; omit it to pass options. |
name | str | None |
Node name; defaults to the function's name. Lambdas need one. |
ref | str | None |
Import path for |
timeout | float | None |
Seconds before the node fails with |
goto | Sequence[Node | EndSentinel | str] | None |
Every target reached through |
TypeError |
If |
Node¶
Node(name, handler, ref=None, accepts_context=False, context_is_keyword=False, context_parameter_name='ctx', timeout=None, goto=None)
A graph step: a callable plus its name and options.
name | str | |
handler | Callable[..., Any] |
The wrapped function. |
ref | str | None |
|
accepts_context | bool |
The function takes a |
context_is_keyword | bool |
That parameter is keyword-only. |
context_parameter_name | str |
Name of that parameter. |
timeout | float | None |
Seconds before the node fails with |
goto | tuple[Node | EndSentinel | str, ...] | None |
Targets the node may reach through |
metadata | NodeMetadata |
The node's options as a |
NodeMetadata¶
NodeMetadata(*, name, ref=None, accepts_context=False, context_is_keyword=False, context_parameter_name='ctx', timeout=None, goto=None)
Serializable description of a node's options.
name | str | |
ref | str | None |
|
accepts_context | bool |
The function takes a |
context_is_keyword | bool |
That parameter is keyword-only. |
context_parameter_name | str |
Name of that parameter. |
timeout | float | None |
Seconds before the node fails with |
goto | tuple[str, ...] | None |
Names of the declared |
NodeContext¶
NodeContext(graph_name, run_id, thread_id, step, node, branch_id='main', root_run_id='', task_id='', middleware=(), workspace=None, state_store=None, state_schema=None, stream_modes=(), cache=dict(), resume_values=dict())
Run information and helpers for a running node.
graph_name | str |
Name of the graph. |
run_id | str |
Id of this run. |
thread_id | str | None |
Thread of the run. |
step | int |
Superstep, counted from 0. |
node | str |
Name of the node. |
branch_id | str |
History branch of the run. |
root_run_id | str |
Id of the outermost run when the graph runs inside another one. |
task_id | str |
Id of this task within the superstep. |
middleware | tuple[Any, ...] |
Middleware of the run, in order. |
workspace | Any |
The graph's workspace. |
state_store | Any |
Store of the run's history. |
state_schema | Any |
Schema of the graph state. |
stream_modes | tuple[str, ...] |
Stream modes the caller asked for. |
cache | dict[str, Any] |
Saved with the task when it pauses and given back on resume. |
resume_values | Mapping[str, Any] |
Answers to this task's interrupts by interrupt key, read-only. |
context | Any |
The |
Methods
emit(data)¶
Emit a "custom" stream event with this node, step and task.
emit(data)¶emit(data) -> None
data | Any |
Event payload. |
async spawn(tasks, ...)¶
Start sub-agents concurrently; they belong to this run.
async spawn(tasks, ...)¶async spawn(tasks, *, thread_prefix=None) -> list[AgentHandle]
A run that finishes or pauses with sub-agents never gathered cancels
them and raises GraphExecutionError; a run that fails or is closed
early cancels them too. To wait in a later run, use submit.
tasks | list[AgentTask] |
One task per sub-agent: the graph, its input and options. |
thread_prefix | str | None |
Prefix of the threads |
TypeError |
If a task has no |
ValueError |
If |
async gather(handles, ...)¶
Wait for sub-agents started with spawn.
async gather(handles, ...)¶async gather(handles, *, timeout=None, return_exceptions=False) -> list[AgentResult]
handles | Sequence[AgentHandle | str] |
Handles, or ids of sub-agents spawned in this run, by any node. Ids do not resolve after a pause. |
timeout | float | None |
Seconds to wait before cancelling the unfinished sub-agents. |
return_exceptions | bool |
Return failed and cancelled results instead of raising. |
AgentGroupError |
If a sub-agent failed or was cancelled and |
AgentTimeoutError |
If the timeout expires first. |
AgentHandleLostError |
If a handle was not spawned in this run or was already gathered. |
async submit(tasks, ...)¶
Start sub-agents on a background executor.
async submit(tasks, ...)¶async submit(tasks, *, executor, thread_prefix=None) -> list[DurableAgentHandle]
tasks | list[AgentTask] |
One task per sub-agent: the graph, its input and options. |
executor | AgentExecutor |
Executor that runs the sub-agents, such as |
thread_prefix | str | None |
Prefix of the threads |
TypeError |
If a task has no |
async poll_agents(handles, ...)¶
Check background sub-agents without waiting.
async poll_agents(handles, ...)¶async poll_agents(handles, *, executor) -> list[AgentResult | None]
handles | list[DurableAgentHandle] |
Handles returned by |
executor | AgentExecutor |
The executor the sub-agents were submitted to. |
list[AgentResult or None] |
|
AgentHandleLostError |
If the executor does not know a handle. |
Routing from a node¶
Command¶
Command(update=None, goto=None)
Node return value that updates state and routes.
update | Mapping[str, Any] | None |
Update applied through the reducers; not the state object the node received. |
goto | Any |
Next targets, replacing the node's edges. |
Send¶
Send(node, payload=None)
Route to a node with its own input payload.
node | Any |
Target node. |
payload | Any |
State fields laid over the current state for that task only, without
reducers. Keys must be state fields, unless the state is a plain
|
Tools¶
tool¶
tool(function=None, /, *, name=None, description=None, input_model=None) -> Tool | Callable[[Callable[..., Any]], Tool]
Turn a function into a tool.
The arguments are validated against a model built from the type hints,
which rejects unknown arguments. A parameter annotated exactly as
ToolContext receives the tool context and is hidden from the model.
Sync functions run in a worker thread.
function | Callable[..., Any] | None |
Function to wrap; omit it to use the decorator with options. |
name | str | None |
Tool name, matching |
description | str | None |
Description sent to the model; defaults to the function's own docstring, all of it. |
input_model | type[BaseModel] | None |
Argument model to use instead of the generated one; its fields must be
exactly the function's parameters, without the |
TypeError |
If |
Tool¶
Tool(function, metadata)
A callable exposed to models, with validated input and output.
tool() builds one from a function and its type hints.
function | Callable[..., Any] |
The function the tool calls. |
metadata | ToolMetadata |
Name, description and schemas of the tool, kept as |
name | str |
Tool name shown to the model. |
description | str |
Tool description shown to the model. |
input_model | type[BaseModel] |
Pydantic model of the tool arguments. |
output_model | type[BaseModel] |
Pydantic model of the tool result. |
Methods
input_schema_json()¶
JSON schema of the arguments, as a string.
input_schema_json()¶input_schema_json() -> str
input_schema()¶
JSON schema of the arguments.
input_schema()¶input_schema() -> dict[str, Any]
output_schema_json()¶
JSON schema of the result, as a string.
output_schema_json()¶output_schema_json() -> str
output_schema()¶
JSON schema of the result.
output_schema()¶output_schema() -> dict[str, Any]
supports_context()¶
Whether the tool takes a ToolContext argument.
supports_context()¶supports_context() -> bool
ToolMetadata¶
ToolMetadata(*, name, description, input_model, output_model, context_parameter=None, returns_model=False)
Name, description and schemas of a tool.
name | str | |
description | str | |
input_model | type[BaseModel] |
Model the arguments are validated against. |
output_model | type[BaseModel] |
Model the result is validated against. |
context_parameter | str | None |
Name of the parameter that receives the |
returns_model | bool |
The function's return type is the output model itself; otherwise the
value is wrapped in the model's |
ToolContext¶
ToolContext(graph_name=None, node=None, state=None, thread_id=None, run_id=None, emit=None, middleware=(), workspace=None, tool_call_id=None, root_run_id=None, task_id='')
Run information passed to tools that declare a ToolContext.
graph_name | str | None |
Name of the graph. |
node | str | None |
Name of the node that calls the tool. |
state | Any |
State that node received. |
thread_id | str | None | |
run_id | str | None |
Id of the run. |
emit | Callable[[Any], None] | None |
Sends a |
middleware | tuple[Any, ...] |
Middleware of the run, in order. |
workspace | Any |
The graph's workspace. |
tool_call_id | str | None |
Id of the tool call being executed. |
root_run_id | str | None |
Id of the outermost run when the graph runs inside another one. |
task_id | str |
Task of the node that calls the tool. |
context | Any |
The |
Methods
from_node_context(ctx, state, ...)¶
class method
Build a tool context from the running node.
from_node_context(ctx, state, ...)¶from_node_context(ctx, state, *, tool_call_id=None) -> ToolContext
ctx | NodeContext |
Context of the node that calls the tool. |
state | Any |
State the node received. |
tool_call_id | str | None |
Id of the tool call being executed. |
ToolCallResult¶
ToolCallResult(*, value, arguments)
Validated result of a tool call and the arguments it ran with.
value | SerializeAsAny[BaseModel] |
The result as an instance of the tool's |
arguments | dict[str, Any] |
The arguments after |
call_tool¶
async call_tool(tool, arguments, ctx=None) -> ToolCallResult
Validate arguments, run tool middleware and call a tool.
tool | Tool |
A tool made with |
arguments | BaseModel | dict[str, Any] |
Tool arguments. |
ctx | ToolContext | None |
Run information and middleware; an empty context is used when omitted. |
ToolCallResult |
The validated tool result and the arguments the tool ran with, after
|
TypeError |
If |
ValidationError |
If the arguments or the result do not match the tool's models; the
|
Exception |
What the tool raised, after the |