BTEng — Behavior Tree Engine for Python
BTEng is a Behavior Tree engine for Python with no runtime dependencies.
pip install bteng installs one package and nothing else. Every module the engine loads
at runtime imports only the standard library, so BTEng drops into a ROS node, a PLC bridge
or an embedded controller without touching your dependency tree.
Exactly two modules import anything third-party, and neither is on that path: the ZMQ
event transport (pip install "bteng[zmq]") and the pytest plugin, loaded only by a test
run that asks for it.
Three things it does that are worth knowing about before you choose it:
async defleaves without an async tick loop. ACoroActionNodeawaits your host application's coroutines — an HTTP call, a ROS 2 action, an OPC UA read — while the tick loop stays ordinary synchronous code. No colouring your whole control loopasync.- Reactive nodes that watch the blackboard.
ReactiveSequenceandReactiveFallbacksubscribe to the blackboards their subtree can reach and re-check guards on every tick, interrupting a running action the moment a precondition stops holding — without re-entering the actions ahead of it. - Introspection built in, not bolted on. Every status change is recorded once, inside
execute_tick(), and fanned out to whichever of the Inspector, structured logger, execution tracer and optional ZMQ stream you attached. The tracer can save a run and replay it frame by frame afterwards.
Use it for robotics, automation, simulation — anything tick-driven that needs preconditions, fallbacks, retries and recovery paths rather than one large control loop.
Table of Contents
- Features
- Installation
- Quick Start
- Core Concepts
- Custom Nodes
- XML Trees
- Introspection & Logging
- Execution Tracing & Replay
- ZMQ Event Streaming
- Unit Testing
- CLI
- Documentation
- License
Features
| Category | Capabilities |
|---|---|
| Execution | Tick-based engine, event-loop executor, pause/resume, max-tick budget |
| Control nodes | Sequence, Fallback/Selector, Parallel, ReactiveSequence, ReactiveFallback |
| Decorators | Inverter, Retry, Timeout, RateController, ForceSuccess, ForceFailure |
| Leaf nodes | ActionNode, ConditionNode, StatefulActionNode, AsyncActionNode |
| Blackboard | Scoped, observable, provenance history, schema validation, change subscriptions |
| Builder API | Fluent TreeBuilder for programmatic tree construction |
| XML loader | Full XML parser with port-default resolution, subtree support and BehaviorTree.CPP-style preconditions (_skipIf, _while, …) |
| Factory | @register_node decorator, NodeManifest, dynamic plugin loading |
| Introspection | Inspector — per-node stats, active-path tracking, explain log |
| Logging | Logger — console and JSON-lines sinks, auto-wired to Inspector |
| Tracing | ExecutionTracer — per-tick frame recording, JSON export, replay |
| Concurrency | ThreadPool (auto-injected), CancellationToken for async actions |
| Testing | MockActionNode, MockConditionNode, BehaviorTreeTest |
| Streaming | ZmqPublisher — streams tick events via ZMQ PUB (optional dep) |
| CLI | bteng run for command-line tree execution |
Installation
From PyPI
pip install bteng
Optional extras
pip install "bteng[zmq]" # ZMQ publisher — stream tick events to dashboards
pip install "bteng[dev]" # Development tools: pytest, build, twine
Requirements: Python 3.12 or newer.
Quick Start
For new code, start with TreeBuilder + TreeExecutor.
from bteng import (
Blackboard,
NodeStatus,
TreeBuilder,
TreeExecutor,
)
bb = Blackboard.create("demo")
bb.set("battery_level", 87)
tree = (
TreeBuilder(blackboard=bb)
.tree_id("Mission")
.sequence("root")
.condition("BatteryOK", lambda: bb.get("battery_level", 0) > 20)
.action("Navigate", lambda: NodeStatus.SUCCESS)
.end()
.build()
)
executor = TreeExecutor()
executor.set_tree(tree)
status = executor.tick_until_result(max_ticks=100)
print(status) # NodeStatus.SUCCESS
BehaviorTreeEngine is still available for legacy code, but TreeExecutor is the
recommended runtime for new projects.
Core Concepts
Status model
Every node returns a NodeStatus each tick:
| Status | Meaning |
|---|---|
SUCCESS |
Node completed successfully |
FAILURE |
Node failed |
RUNNING |
Node is active and must be ticked again |
SKIPPED |
A precondition (_skipIf, _while) stopped the node; parents pass over it |
IDLE |
Node is inactive or has been halted |
SKIPPED only appears in trees that use preconditions — see
Preconditions.
Control nodes
| Node | Behavior |
|---|---|
Sequence |
Ticks children left-to-right; stops at first FAILURE or RUNNING |
Fallback / Selector |
Ticks children left-to-right; stops at first SUCCESS or RUNNING |
Parallel |
Ticks all children simultaneously with configurable success/failure thresholds |
Every control node passes over a SKIPPED child, and returns SKIPPED itself when
all of its children were skipped.
| ReactiveSequence | Restarts from child[0] every tick; earlier conditions interrupt later actions |
| ReactiveFallback | Restarts from child[0] every tick; higher-priority child can preempt |
Decorators
| Decorator | Purpose |
|---|---|
Inverter |
Swaps SUCCESS ↔ FAILURE; passes RUNNING and SKIPPED unchanged |
Retry(max_attempts) |
Retries child on FAILURE up to N times |
Timeout(duration) |
Returns FAILURE if child exceeds duration (seconds) |
RateController(hz) |
Rate-limits child ticking; returns cached status between ticks |
ForceSuccess |
Returns SUCCESS once the child completes; passes RUNNING and SKIPPED |
ForceFailure |
Returns FAILURE once the child completes; passes RUNNING and SKIPPED |
Blackboard
A thread-safe, scoped key-value store shared across the tree:
from bteng import Blackboard
bb = Blackboard.create("robot")
bb.set("pose", (1.0, 2.0, 0.0))
bb.set("stopped", None) # None is a valid stored value
bb.get("pose") # (1.0, 2.0, 0.0)
bb.get("stopped") # None (the stored None, not a missing-key default)
bb.has("stopped") # True
# Change subscriptions
sub = bb.subscribe(lambda key, val: print(f"{key} changed to {val}"))
bb.set("pose", (2.0, 3.0, 0.0)) # fires callback
bb.unsubscribe(sub)
# Scoped child blackboard for subtree port isolation
child = bb.create_child_scope("subtree", remapping={"local_goal": "goal"})
child.set("local_goal", "dock") # transparently writes bb["goal"]
child.get("local_goal") # transparently reads bb["goal"]
Custom Nodes
Define new node types by subclassing and declaring ports:
from bteng import ActionNode, InputPort, OutputPort, NodeStatus, register_node
@register_node()
class DetectObject(ActionNode):
@classmethod
def provided_ports(cls):
return [
InputPort("camera", default="rgb"),
OutputPort("object_pose"),
]
def tick(self) -> NodeStatus:
camera = self.get_input("camera") # resolves XML attribute or default
pose = run_detector(camera)
if pose is None:
self.set_failure_reason("detector returned no result")
return NodeStatus.FAILURE
self.set_output("object_pose", pose)
return NodeStatus.SUCCESS
Access the full node configuration via self.config (NodeConfig with blackboard, port mappings, and static params). self.blackboard is a convenience shortcut.
For long-running work use StatefulActionNode (three-phase lifecycle: on_start / on_running / on_halted) or AsyncActionNode (runs execute_async(token) in a background thread; the executor automatically injects a shared ThreadPool).
XML Trees
Load and execute trees defined in XML — useful for decoupling behavior from code:
<?xml version="1.0" encoding="UTF-8"?>
<BTEng format_version="1.0" main_tree_to_execute="main">
<Tree ID="main">
<ReactiveFallback name="root">
<ReactiveSequence name="navigate_if_safe">
<Condition ID="PathClear"/>
<Timeout duration="10.0">
<Action ID="NavigateTo" goal="{target_goal}"/>
</Timeout>
</ReactiveSequence>
<Action ID="StopRobot"/>
</ReactiveFallback>
</Tree>
<TreeNodesModel>
<Condition ID="PathClear"/>
<Action ID="NavigateTo">
<input_port name="goal"/>
</Action>
<Action ID="StopRobot"/>
</TreeNodesModel>
</BTEng>
Port syntax:
goal="{target_goal}"— blackboard reference; reads/writes keytarget_goalmode="inspection"— static literal parameterInputPort(default=...)is applied when the XML attribute is absent
from bteng import XMLTreeParser, Blackboard
bb = Blackboard.create("demo")
bb.set("target_goal", "dock_station")
parser = XMLTreeParser()
root = parser.parse_file("mission.xml", blackboard=bb)
See docs/reference/xml.md for the full specification.
Introspection & Logging
Attach an Inspector and Logger to any TreeExecutor — no changes to node code required:
from bteng import Inspector, Logger, LogLevel, TreeExecutor
inspector = Inspector.create()
logger = Logger.create()
logger.add_console_sink(colored=True)
logger.add_json_file_sink("/tmp/bt_run.jsonl")
executor = TreeExecutor()
executor.set_tree(tree)
executor.set_inspector(inspector)
executor.set_logger(logger) # auto-wired; no extra setup needed
executor.tick_until_result()
# Per-node statistics
for uid, stats in inspector.all_stats().items():
print(f"{uid:40s} ticks={stats.tick_count:4d} "
f"total={stats.total_duration*1000:.1f}ms")
Execution Tracing & Replay
Record every tick for offline analysis, regression testing, or replay:
from bteng import ExecutionTracer, TreeExecutor
tracer = ExecutionTracer()
executor = TreeExecutor()
executor.set_tree(tree)
executor.set_tracer(tracer)
executor.tick_until_result()
# Export full trace
with open("trace.json", "w") as f:
f.write(tracer.export_json())
# Compact replay format (smaller file)
with open("replay.json", "w") as f:
f.write(tracer.export_replay())
# Load and inspect a previously recorded run
tracer2 = ExecutionTracer()
tracer2.load_replay(open("replay.json").read())
frame = tracer2.replay_frame(0)
print(frame.tick_index, frame.blackboard_snapshot)
ZMQ Event Streaming
Stream tick events to an external dashboard or visualiser with zero coupling between the engine and the consumer:
pip install "bteng[zmq]"
from bteng.introspection import ZmqPublisher
pub = ZmqPublisher(port=1667) # default port — compatible with BehaviorTree.CPP convention
pub.attach(inspector)
pub.start()
# ... run tree as normal ...
pub.stop()
Each published message is a JSON object on ZMQ topic bteng:
{
"ts": 1234.567,
"uid": "a1b2c3d4",
"name": "NavigateTo",
"type": "action",
"status": "SUCCESS",
"dur_ms": 12.3,
"reason": ""
}
Unit Testing
BehaviorTreeTest and MockActionNode / MockConditionNode provide a purpose-built test harness:
from bteng import (
MockActionNode, MockConditionNode, BehaviorTreeTest,
NodeStatus, SequenceNode, Tree, TreeMetadata,
)
condition = MockConditionNode("IsReady")
condition.set_bool(True)
action = MockActionNode("Navigate")
action.set_ticks_to_complete(3) # returns RUNNING for 2 ticks, then SUCCESS
root = SequenceNode("root", children=[condition, action])
tree = Tree(TreeMetadata(id="test"), root)
result = (
BehaviorTreeTest(tree)
.expect_final_status(NodeStatus.SUCCESS)
.set_max_ticks(10)
.run()
)
assert result, result.error_message
CLI
Execute and visualize trees from the command line:
bteng run mission.xml --plugin my_robot_nodes.py --tree main --hz 10 --log run.json -v
Documentation
| Document | Contents |
|---|---|
| 5-minute Quick Start | Recommended first tree with TreeBuilder + TreeExecutor |
| Which API should I use? | Decision table for Python, XML, legacy, and advanced APIs |
| Beginner Guide | Control nodes, actions, blackboard, TreeBuilder, and first tests |
| Advanced Guide | Ports, reactive internals, plugins, introspection, ZMQ, runtime modification |
| Architecture | Component relationships and data-flow diagram |
| XML specification | Full XML format reference with port syntax |
| Node system | Node lifecycle, port model, custom node authoring |
| All documentation | Full index of every page |
| Changelog | Release history |
| Licensing | Third-party audit and open-source alternatives |
Acknowledgements
BTEng's design is inspired by BehaviorTree.CPP, a widely used C++ behavior tree library.
License
BTEng is released under the MIT License. See LICENSE for terms.
Metadata
Release files for bteng 0.3.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bteng-0.3.3.tar.gz | 167.2 kB | Without provenance | Details for bteng-0.3.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bteng-0.3.3-py3-none-any.whl | Python 3 | none | any | Details for bteng-0.3.3-py3-none-any.whl |
Total release size: 288.2 kB
Release files / bteng-0.3.3.tar.gz
| Download URL | bteng-0.3.3.tar.gz |
|---|---|
| Size | 167.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
837bad78eeaf7b41e0de4e36cd7087b1e11fa1f463060d4d5f4e19ee64d47eb4
|
|
BLAKE2b-256 checksum How to use checksums |
7eca9276507b0997d58a2702bb97e3a37f2ff00e70aa503a95c794bfde3cadfd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.4
|
Release files / bteng-0.3.3-py3-none-any.whl
| Download URL | bteng-0.3.3-py3-none-any.whl |
|---|---|
| Size | 121.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cb6c82cffe692ce235c9ef38cc58818054e7698d174bef126a0b8c361989196b
|
|
BLAKE2b-256 checksum How to use checksums |
09c2245f014ee9fc77b44d7aed01f8117f312e8870f6c607ad19d4469522475b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.4
|
Security for bteng 0.3.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.