Skip to content
Back to skills

2901 Parallel Execution 5d3062a6

ASecurity

`ParallelMiddleware` executes multiple middleware instances in parallel, reducing total latency when you have multiple independent validators.

  • 9 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added October 11, 2026
ai-agentspythongorailsapidatabaseperformance

Works with

  • api

Security analysis

A100/100

Scanned October 11, 2026

npx -y skills add tools-only/X-Skills --skill 2901-parallel-execution_5d3062a6 --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 2901 Parallel Execution 5d3062a6?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 2901 Parallel Execution 5d3062a6
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/tools-only-2901-parallel-execution-5d3062a6/badge)](https://www.skillsdirectory.com/skills/tools-only-2901-parallel-execution-5d3062a6)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

SKILL.md
# Parallel Execution

`ParallelMiddleware` executes multiple middleware instances in parallel, reducing total latency when you have multiple independent validators.

## Basic Usage

```python
from pydantic_ai_middleware import (
    ParallelMiddleware,
    AggregationStrategy,
    MiddlewareAgent,
)

# Create validators
profanity_filter = ProfanityFilter()
pii_detector = PIIDetector()
injection_guard = PromptInjectionGuard()

# Run them in parallel
parallel_validators = ParallelMiddleware(
    middleware=[profanity_filter, pii_detector, injection_guard],
    strategy=AggregationStrategy.ALL_MUST_PASS,
    timeout=5.0,
)

# Use with agent
agent = MiddlewareAgent(
    agent=base_agent,
    middleware=[parallel_validators],
)
```

## Aggregation Strategies

The `AggregationStrategy` enum controls how parallel results are combined:

| Strategy | Description | Use Case |
|----------|-------------|----------|
| `ALL_MUST_PASS` | All middleware must succeed; cancels remaining on first failure | Multiple validators that all must agree |
| `FIRST_SUCCESS` | Return first successful result; fails only when all fail | Fallback patterns with alternatives |
| `RACE` | Return fastest successful result, even if others error | Redundant validators for speed |
| `COLLECT_ALL` | Collect all results (errors included) | Comprehensive reporting |

## Performance Example

```python
from pydantic_ai_middleware import ParallelMiddleware, AggregationStrategy

# Sequential: 0.5s + 0.5s + 0.5s + 0.5s = 2.0s total
# Parallel:  max(0.5s, 0.5s, 0.5s, 0.5s) = 0.5s total

parallel_validators = ParallelMiddleware(
    middleware=[
        ProfanityFilter(delay=0.5),
        PIIDetector(delay=0.5),
        PromptInjectionGuard(delay=0.5),
        ToxicityChecker(delay=0.5),
    ],
    strategy=AggregationStrategy.ALL_MUST_PASS,
)
```

## Timeout Handling

Set a timeout to prevent slow validators from blocking:

```python
parallel = ParallelMiddleware(
    middleware=[...],
    timeout=5.0,  # Max 5 seconds for all validators
)
```

## When to Use Parallel Execution

**Good candidates:**

- Multiple independent validators
- External API calls (rate limiting, content moderation)
- ML model inference calls
- Database lookups

**Not recommended for:**

- Validators with dependencies on each other
- Fast, CPU-bound checks
- Validators that share mutable state

## Next Steps

- [Async Guardrails](async-guardrails.md) - Run guardrails alongside LLM calls
- [Middleware Chains](middleware-chains.md) - Sequential composition
- [API Reference](../api/middleware.md) - ParallelMiddleware API

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…