Skip to content

🚀 Feature Request: Parallel Execution of Validators in a guard  #1298

@discretedeamon

Description

@discretedeamon

Description
Currently, Guardrails executes all validators sequentially within the Guard.parse() method. This means that if you have multiple validators (e.g., PII detection, Jailbreak detection, Profanity filter), each validator runs one after the other, and the total validation time is the sum of all validator times.

For use cases where some validators are computationally expensive (such as PII or Jailbreak detection, which may use large ML models), this sequential execution leads to significant latency for every user query.

Why is this needed

  • Performance:
    Many validators are independent and can safely be run in parallel. For example, PII detection and Jailbreak detection do not depend on each other’s results.

  • Reduced Latency:
    With parallel execution, the total validation time becomes the time of the slowest validator, not the sum of all. This can dramatically improve response times for user queries.

  • Better Resource Utilization:
    On multi-core systems, parallel execution can utilize available CPU resources more efficiently.

Implementation details

  • API Change:
    Add an option to [Guard] or [Guard.parse()] (e.g., parallel=True) to enable parallel execution of validators.
  • Execution Model:
    Use Python’s [asyncio] or concurrent.futures.ThreadPoolExecutor to run each validator’s [validate] method concurrently.
  • Error Handling:
    Collect results and exceptions from all validators.
    If any validator fails, aggregate the errors and return as per current Guardrails behavior.
    Only proceed if all validators pass.
  • Backward Compatibility:
    Default to sequential execution unless parallel=True is specified, to avoid breaking existing workflows.

Example API Usage

guard = Guard().use_many(
    GuardrailsPII(...),
    ProfanityFree(...),
    DetectJailbreak(...),
    WebSanitization(...)
)
result = guard.parse(query, parallel=True)

Example Implementation Sketch

import asyncio
async def parallel_validate(validators, value, metadata):
    loop = asyncio.get_running_loop()
    tasks = [
        loop.run_in_executor(None, v.validate, value, metadata)
        for v in validators
    ]
    results = await asyncio.gather(*tasks, return_exceptions=True)
    return results

End result

  1. Significantly reduced validation latency for queries with multiple heavy validators.
  2. No change in validation logic or security: All validators must still pass before the input is accepted.
  3. Optional feature: Sequential validation remains the default for backward compatibility.

Reference

  1. https://docs.python.org/3/library/asyncio.html
  2. https://docs.python.org/3/library/concurrent.futures.html

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions