orbiter.train.trajectory
Trajectory capture with strategy patterns and export to JSON/CSV.
Trajectory capture with strategy patterns and export to JSON/CSV.
from orbiter.train.trajectory import (
DefaultStrategy,
TrajectoryDataset,
TrajectoryError,
TrajectoryItem,
TrajectoryStrategy,
)TrajectoryError
class TrajectoryError(Exception)Error during trajectory operations.
TrajectoryItem
@dataclass(frozen=True, slots=True)
class TrajectoryItemA single step in an agent execution trajectory. Follows the State-Action-Reward (SAR) pattern.
Fields
| Field | Type | Default | Description |
|---|---|---|---|
id | str | (auto: 12-char hex) | Unique step identifier |
task_id | str | "" | Task this step belongs to |
agent_id | str | "" | Agent that produced this step |
step | int | 0 | Step number within the task |
timestamp | float | (auto: time.time()) | When the step occurred |
input | str | "" | Input state (user message) |
messages | tuple[dict[str, Any], ...] | () | Full message history |
context | dict[str, Any] | {} | Context state |
output | str | "" | Agent output text |
tool_calls | tuple[dict[str, Any], ...] | () | Tool calls made |
score | float | None | None | Reward score |
status | str | "success" | Step status |
metadata | dict[str, Any] | {} | Additional metadata |
Methods
to_dict
def to_dict(self) -> dict[str, Any]Serialise to a plain dict.
from_dict (classmethod)
@classmethod
def from_dict(cls, data: dict[str, Any]) -> TrajectoryItemCreate a TrajectoryItem from a plain dict.
TrajectoryStrategy
class TrajectoryStrategy(ABC)Pluggable strategy for generating trajectory items from messages.
Abstract methods
build_item
def build_item(
self,
messages: Sequence[dict[str, Any]],
*,
task_id: str = "",
agent_id: str = "",
step: int = 0,
**kwargs: Any,
) -> TrajectoryItemBuild a single trajectory item from a message list.
Methods
validate
def validate(self, items: Sequence[TrajectoryItem]) -> boolReturn True if the trajectory is valid. Override for custom checks. Default implementation returns True if len(items) > 0.
DefaultStrategy
class DefaultStrategy(TrajectoryStrategy)Default strategy: extracts input/output/tool_calls from message dicts. Scans for the first user message as input and the last assistant message as output.
TrajectoryDataset
class TrajectoryDataset(
*,
strategy: TrajectoryStrategy | None = None,
)Dataset of trajectory items with capture, strategy, and export.
Constructor parameters
| Name | Type | Default | Description |
|---|---|---|---|
strategy | TrajectoryStrategy | None | None | Strategy for building items. Defaults to DefaultStrategy |
Properties
| Property | Type | Description |
|---|---|---|
items | list[TrajectoryItem] | Copy of all trajectory items |
strategy | TrajectoryStrategy | The active strategy |
Methods
append_trajectory
def append_trajectory(self, item: TrajectoryItem) -> NoneAppend a pre-built trajectory item.
from_messages
def from_messages(
self,
messages: Sequence[dict[str, Any]],
*,
task_id: str = "",
agent_id: str = "",
**kwargs: Any,
) -> TrajectoryItemBuild a trajectory item from messages via strategy and append it. Automatically increments the step counter per task/agent pair.
Returns: The built and appended TrajectoryItem.
save_task_trajectory
def save_task_trajectory(
self,
task_id: str,
items: Sequence[TrajectoryItem],
) -> intBulk-append items for a given task. Returns count added.
get_task_trajectory
def get_task_trajectory(self, task_id: str) -> list[TrajectoryItem]Return all items matching task_id.
validate
def validate(self) -> boolDelegate validation to the strategy.
to_json
def to_json(self) -> strExport all items as a JSON string.
to_csv
def to_csv(self) -> strExport all items as CSV text. Complex fields (messages, tool_calls, context, metadata) are serialized to JSON strings within the CSV.
clear
def clear(self) -> NoneRemove all items and reset step counters.
Special methods
__len__()— Returns the number of items.
Example
from orbiter.train import TrajectoryDataset
dataset = TrajectoryDataset()
messages = [
{"role": "user", "content": "What is Python?"},
{"role": "assistant", "content": "Python is a programming language."},
]
item = dataset.from_messages(messages, task_id="task-1", agent_id="tutor")
print(item.input) # "What is Python?"
print(item.output) # "Python is a programming language."
print(len(dataset)) # 1
# Export
json_str = dataset.to_json()
csv_str = dataset.to_csv()