Orchestration API¶
orchestration ¶
Orchestration primitives for composing agents.
Provides Pipeline (sequential), Parallel (concurrent), Router (conditional), and RefinementLoop (iterative) — plus the Step protocol for defining executable units.
Evaluator
module-attribute
¶
An evaluator can be a sync/async callable or a BaseAgent.
StepLike
module-attribute
¶
Union type accepted by Pipeline — auto-coerced to Step.
EvalResult
dataclass
¶
Outcome of evaluating a single iteration's output.
IterationRecord
dataclass
¶
Record of a single refinement iteration.
LoopResult
dataclass
¶
LoopResult(
output: OutputT,
iterations: int,
exit_reason: str,
history: list[IterationRecord] = list(),
best_iteration: int | None = None,
best_score: float | None = None,
)
Final result of a RefinementLoop run.
output is the iteration that the loop is returning to the caller. With
keep_best=True (the default), this may be an earlier iteration's
output than the final one — specifically the iteration that achieved the
highest scored :class:EvalResult. best_iteration and best_score
record which iteration was kept and at what score; both are None when
no iteration produced a numeric score.
RefinementLoop ¶
RefinementLoop(
step: StepLike,
evaluator: Evaluator,
*,
state_updater: Callable[..., Any],
max_iterations: int = 5,
timeout: float | None = None,
convergence_window: int | None = None,
convergence_threshold: float = 0.01,
confidence_threshold: float | None = None,
agent_self_eval: BaseAgent | None = None,
keep_best: bool = True,
)
Iterate a step with evaluation feedback until convergence or limits.
The loop runs the step, evaluates the output, and uses the state_updater to produce the next input. It terminates when the evaluator passes, max iterations are reached, the timeout expires, or scores converge.
Configure the refinement loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
StepLike
|
The step to iterate. |
required |
evaluator
|
Evaluator
|
Callable or BaseAgent that produces EvalResult. |
required |
state_updater
|
Callable[..., Any]
|
(current_input, output, eval_result) -> next_input. |
required |
max_iterations
|
int
|
Must be >= 1. |
5
|
timeout
|
float | None
|
Optional wall-clock timeout in seconds. |
None
|
convergence_window
|
int | None
|
Number of recent scores to check for convergence. |
None
|
convergence_threshold
|
float
|
Max score variance within the window. |
0.01
|
confidence_threshold
|
float | None
|
When set AND |
None
|
agent_self_eval
|
BaseAgent | None
|
Optional :class: |
None
|
keep_best
|
bool
|
When |
True
|
Source code in orqest/orchestration/loop.py
run
async
¶
Execute the refinement loop starting from initial_input.
With keep_best=True (the default), tracks the highest-scoring
iteration and returns its output on any non-passed exit if the
final iteration regressed. The passed=True early exit always
returns the passing iteration's output regardless of score.
Source code in orqest/orchestration/loop.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
MergeStrategy ¶
Parallel ¶
Parallel(
steps: list[Step | Any],
*,
merge: Callable[
[list[Any]], Any
] = MergeStrategy.collect_all,
timeout: float | None = None,
name: str = "parallel",
)
Run multiple steps concurrently and merge their results.
Each step is launched as an independent asyncio task. Timed-out tasks are cancelled and recorded as TimeoutError. The merge strategy receives only the successful (non-None) outputs.
Initialize with steps, merge strategy, and optional timeout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
steps
|
list[Step | Any]
|
StepLike objects to execute concurrently. |
required |
merge
|
Callable[[list[Any]], Any]
|
Callable that combines successful outputs into a final value. |
collect_all
|
timeout
|
float | None
|
Maximum seconds to wait for all steps. None means no limit. |
None
|
name
|
str
|
Identifier for logging and events. |
'parallel'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If steps is empty. |
Source code in orqest/orchestration/parallel.py
run
async
¶
Execute all steps concurrently and return aggregated results.
Source code in orqest/orchestration/parallel.py
ParallelResult
dataclass
¶
Outcome of a parallel execution.
Attributes:
| Name | Type | Description |
|---|---|---|
outputs |
list[OutputT | None]
|
Per-step results; None for steps that failed or timed out. |
errors |
list[Exception | None]
|
Per-step exceptions; None for steps that succeeded. |
merged |
Any
|
Result of applying the merge strategy to successful outputs. |
Pipeline ¶
Run a sequence of steps, feeding each output into the next input.
Each entry in steps is either a StepLike value or a (StepLike, StepConfig) tuple. StepLike values are auto-coerced via _coerce_step.
Validate and coerce the step list.
Raises ValueError if steps is empty.
Source code in orqest/orchestration/pipeline.py
run
async
¶
Execute all steps sequentially, returning the final output.
Source code in orqest/orchestration/pipeline.py
run_stream
async
¶
Execute steps and yield PipelineEvent at each lifecycle point.
Source code in orqest/orchestration/pipeline.py
PipelineStepError ¶
Raised when a step fails with ErrorStrategy.STOP.
Store the failing step's name and index alongside the message.
Source code in orqest/orchestration/pipeline.py
Route ¶
Route(
name: str,
step: Step | BaseAgent | Callable[..., Any],
*,
condition: Callable[[Any], bool] | None = None,
)
A named step with an optional condition for rule-based routing.
Initialize a route.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Identifier used for classifier-based selection. |
required |
step
|
Step | BaseAgent | Callable[..., Any]
|
The StepLike to execute when this route is selected. |
required |
condition
|
Callable[[Any], bool] | None
|
Predicate evaluated in rule-based mode. None means this route is only reachable via classifier. |
None
|
Source code in orqest/orchestration/router.py
Router ¶
Router(
routes: list[Route],
*,
classifier: BaseAgent
| Callable[..., Any]
| None = None,
fallback: Step
| BaseAgent
| Callable[..., Any]
| None = None,
name: str = "router",
)
Select and execute a single route based on input.
In rule-based mode, routes are evaluated in order and the first whose condition returns True is selected. In classifier mode, an agent or callable picks the route by name.
Initialize the router.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
routes
|
list[Route]
|
Ordered list of Route objects. |
required |
classifier
|
BaseAgent | Callable[..., Any] | None
|
Agent or callable that returns a route name. |
None
|
fallback
|
Step | BaseAgent | Callable[..., Any] | None
|
Step executed when no route matches. |
None
|
name
|
str
|
Identifier for logging and events. |
'router'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If routes is empty, or if no classifier is provided and none of the routes have conditions. |
Source code in orqest/orchestration/router.py
run
async
¶
Route input_data to the selected step and return its output.
Raises:
| Type | Description |
|---|---|
RouterError
|
When no route matches and no fallback exists. |
Source code in orqest/orchestration/router.py
RouterError ¶
Raised when no route matches and no fallback is configured.
AgentStep ¶
Wraps a BaseAgent as a pipeline Step.
Creates a fresh GlobalState per invocation so each step execution is stateless. Uses a prompt_builder to convert arbitrary input into a string prompt for the agent.
Initialize with a BaseAgent and optional prompt builder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
BaseAgent
|
The BaseAgent to wrap. |
required |
prompt_builder
|
Callable[[Any], str] | None
|
Converts input_data to a prompt string. Defaults to JSON for BaseModel, str() otherwise. |
None
|
Source code in orqest/orchestration/step.py
execute
async
¶
Run the agent with a fresh state built from input_data.
Source code in orqest/orchestration/step.py
FunctionStep ¶
Wraps an async callable as a pipeline Step.
Initialize with an async callable and optional name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
The async callable to wrap. |
required |
name
|
str | None
|
Step name; defaults to the function's name. |
None
|
Source code in orqest/orchestration/step.py
Step ¶
Minimal interface for an executable pipeline step.
ErrorStrategy ¶
How to handle a step failure during pipeline execution.
PipelineEvent
dataclass
¶
PipelineEvent(
event_type: EventType,
pipeline_name: str,
step_name: str = "",
step_index: int = -1,
timestamp: datetime = (lambda: datetime.now(tz=UTC))(),
data: dict[str, Any] = dict(),
error: Exception | None = None,
)
Event emitted during pipeline execution for observability.
StepConfig
dataclass
¶
Per-step configuration for pipeline execution.
Controls the step's name, error-handling strategy, and retry limits.