nodestep.core.builtin_nodes ¶
Built-in nodes for chat agents and sub-graphs.
model_node calls a chat model and tool_runner runs the tool calls it
returns; build_react_agent joins the two in a loop and subgraph runs a graph
as a node. See Agents and tools.
| Name | What it is |
|---|---|
| Agents | |
build_react_agent | Build a tool-calling agent graph. |
run_agent | Send one user message to an agent and return its final text. |
AgentState | State of the built-in ReAct agent. |
| Model and tool nodes | |
model_node | Create a node that calls a chat model. |
StructuredOutputEvent | Payload of the custom event sent by model_node(on_invalid="emit"). |
tool_runner | Create a node that runs the pending tool calls. |
ToolErrorPolicy | What a tool runner does when a tool call fails. |
| Sub-graphs | |
subgraph | Wrap a graph as a node of another graph. |
Agents¶
build_react_agent¶
build_react_agent(chat, *, tools, name='agent', system_prompt=None, middleware=(), tool_errors='raise', stream=False, output_schema=None, max_steps=25, max_tool_calls=50, workspace=None, state_store=None) -> Graph[AgentState]
Build a tool-calling agent graph.
The model node think and the tool node act alternate until the
model answers without tool calls. Both nodes get tools followed by the
tools() of each middleware in middleware.
chat | Chat |
Chat model to call. |
tools | Sequence[Tool] |
Tools the model may call; may be empty. |
name | str |
Graph name. |
system_prompt | str | None |
System message prepended to every model call. |
middleware | Sequence[Middleware] |
Extra middleware, after the built-in |
tool_errors | ToolErrorPolicy |
What a failed tool call does, as in |
stream | bool |
Stream model calls, as in |
output_schema | type[BaseModel] | None |
Model the final answer is parsed into; |
max_steps | int |
Superstep limit per call. |
max_tool_calls | int |
Tool-call budget for one run and its resumes. |
workspace | Workspace | None |
Workspace exposed to tools. |
state_store | StateStore | None |
Store that enables threads, interrupts and resume. |
GraphConfigError |
If two different tools share a name, or the same tool is listed twice, also through a middleware. |
run_agent¶
async run_agent(graph, message, **kwargs) -> str
Send one user message to an agent and return its final text.
graph | Graph |
Agent graph, usually from |
message | str |
User message. |
**kwargs | Any |
Passed to |
str |
The final text, or |
RunInterruptedError |
If the run paused for human input. |
AgentState¶
AgentState(*, messages=[], tool_calls=[], final_text=None, final_output=None)
State of the built-in ReAct agent.
Subclass of BaseState.
messages | Annotated[list[Message], add_messages] |
The conversation; updates are merged with |
tool_calls | list[ToolCall] |
Calls of the last model turn that have not run yet. |
final_text | str | None |
Answer text of the last model turn; |
final_output | Any |
The answer parsed into |
Model and tool nodes¶
model_node¶
model_node(name, *, chat, system_prompt=None, tools=(), tool_choice=None, stream=False, repair_history=False, output_schema=None, on_invalid='raise', messages_field='messages', tool_calls_field='tool_calls', final_text_field='final_text', final_output_field='final_output') -> Node
Create a node that calls a chat model.
The node sends the messages of messages_field and appends the model's
AIMessage to it. It writes the requested calls to tool_calls_field
and the text to final_text_field, or None when the model calls
tools or refuses. The before_model and after_model hooks run around
the call.
name | str |
Node name. |
chat | Chat |
Chat model to call. |
system_prompt | str | None |
System message prepended to the request. |
tools | Sequence[Tool] |
The node's complete tool list; middleware tools are not added. |
tool_choice | Literal['auto', 'required', 'none'] | str | None |
The request's tool choice; another string names the tool to call.
|
stream | bool |
Call |
repair_history | bool |
Send a repaired copy of an inconsistent history instead of raising.
Tool results that answer no call of the |
output_schema | type[BaseModel] | None |
Model the final answer is parsed into and stored in
|
on_invalid | Literal['raise', 'emit'] |
For an answer that does not validate: raise |
messages_field | str |
State field of the conversation; it must use |
tool_calls_field | str |
State field the requested tool calls are written to. |
final_text_field | str |
State field the answer text is written to. |
final_output_field | str |
State field the parsed answer is written to; used only with
|
GraphConfigError |
If two different tools share a name, or the same tool is listed twice. |
TypeError |
If |
ValueError |
If |
GraphConfigError |
When the node runs, before the model is called: |
ChatHistoryError |
When the node runs, before the model is called, with
|
TypeError |
When the node runs: a model hook replaced the request or the response with another type. |
StructuredOutputEvent¶
StructuredOutputEvent(*, kind='structured_output_error', schema_name, message, raw_content=None)
Payload of the custom event sent by model_node(on_invalid="emit").
kind | Literal['structured_output_error'] | |
schema_name | str |
Name of the |
message | str |
The validation error. |
raw_content | str | None |
The answer text that did not validate. |
tool_runner¶
tool_runner(name, *, tools, tool_errors='raise', messages_field='messages', tool_calls_field='tool_calls') -> Node
Create a node that runs the pending tool calls.
The calls in tool_calls_field run concurrently. Their ToolMessage
results are appended to messages_field, and tool_calls_field is
cleared. When before_tool middleware changed the arguments, the
ToolMessage records the ones the tool ran with in arguments. A call
denied with ToolDeniedError always becomes a ToolMessage with the
reason. Calls that finished before a pause do not run again on resume.
name | str |
Node name. |
tools | Sequence[Tool] |
The node's complete tool list; middleware tools are not added. |
tool_errors | ToolErrorPolicy |
What a failed call (an unknown tool, invalid arguments or an exception
from the tool) does. |
messages_field | str |
State field the |
tool_calls_field | str |
State field the calls are read from; the node clears it. |
GraphConfigError |
If two different tools share a name, or the same tool is listed twice. |
ValueError |
If |
GraphConfigError |
When the node runs, before any tool runs: |
ToolErrorPolicy¶
ToolErrorPolicy: one of "raise", "return" or Callable[[Exception], str]
What a tool runner does when a tool call fails.
"raise" raises the error, "return" sends its text to the model as the
tool result, and a callable turns the exception into that text.
Sub-graphs¶
subgraph¶
subgraph(name, graph, *, state_in=None, state_out=None, share=None, child_thread) -> Node
Wrap a graph as a node of another graph.
The child runs on the parent's state store, with the parent's context,
on the "main" branch of its thread: a new thread
"<parent thread>:<name>:<random hex>" per call with "fresh", or
"<parent thread>:<name>", which keeps the child's state, with
"stable". An unanswered interrupt in the child pauses the parent under
"<task_id>/<child key>". See
Sub-agents.
name | str |
Node name. |
graph | Graph |
Child graph, without a state store of its own. |
state_in | Callable[[Any], Any] | None |
Builds the child input from the parent state. |
state_out | Callable[[dict[str, Any]], dict[str, Any]] | None |
Builds the parent update, a dict, from the child's final state dict. |
share | Collection[str] | None |
Fields both graphs declare; instead of |
child_thread | Literal['fresh', 'stable'] |
Thread policy of the child. |
TypeError |
Unless exactly one of these is given: |
ValueError |
If |
GraphConfigError |
If the child graph has a state store or does not declare a |
GraphExecutionError |
When the node runs: a shared field with a custom reducer changed, or a
|