Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Principle:Guardrails ai Guardrails AsyncExecution

From Leeroopedia

Template:Principle

Overview

Async Execution is the principle governing how Guardrails leverages Python's asyncio runtime to execute validators concurrently. When multiple validators are configured on a Guard and none depend on the output of another, running them in parallel can yield significant performance improvements. This principle captures the design decisions, concurrency model, and constraints that shape how Guardrails schedules and manages asynchronous validator execution.

The core mechanism is asyncio.gather, which schedules multiple validator coroutines to run concurrently within a single event loop. Each validator's async_validate method is invoked as a coroutine, and asyncio.gather awaits all of them simultaneously. This means that I/O-bound validators (such as those calling external APIs or performing network lookups) can overlap their waiting time, reducing total pipeline latency from the sum of individual validator durations to approximately the duration of the slowest validator.

Async execution is not appropriate in all scenarios. Validators that mutate shared state, depend on the output of a prior validator, or operate on streaming chunks require sequential execution. The framework provides both AsyncValidatorService and SequentialValidatorService to accommodate these different execution requirements. The choice between them is determined by the Guard's configuration and the nature of the validators attached to it.

Theoretical Basis

The async execution model is grounded in the cooperative multitasking paradigm implemented by Python's asyncio library. Unlike preemptive threading, cooperative multitasking requires each coroutine to explicitly yield control (via await) at I/O boundaries. This eliminates many concurrency hazards (race conditions, deadlocks) that arise with thread-based parallelism, while still achieving concurrency for I/O-bound workloads.

The use of asyncio.gather implements a fork-join concurrency pattern: the pipeline forks execution into multiple concurrent validator tasks and then joins (awaits) all results before proceeding. This pattern is well-suited to the validator pipeline because the join point provides a natural synchronization barrier where all validator outcomes are collected and aggregated. The gather semantics also support return_exceptions=True, allowing the pipeline to collect both successes and failures without short-circuiting on the first exception.

Related Pages

Implementations

Workflows

(To be connected)

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment