feat(core): add parallel input guardrails

Merged
OM
5mo ago
+1377-81

PR Checklist

Please check if your PR fulfills the following requirements:

Bugs / Features

What is the current behavior?

Input guardrails are fully blocking before streamText starts. Slow async checks, including LLM-based policy checks, delay the first stream output. There is no stream-level support for starting those checks in parallel, stopping the stream if they block, overriding visible output, and keeping speculative assistant output out of conversation memory.

What is the new behavior?

Adds parallel input guardrails for streamText through execution: "parallel" and streamPolicy: "holdUntilPass".

Parallel input guardrails now start while the model stream starts, buffer full/text/UI stream output until the guardrails pass, and replace blocked streams with the guardrail message. Blocked runs restore the conversation buffer checkpoint, skip assistant memory persistence, and stop before final hooks/evals persist generated output.

Blocked parallel input guardrails emit an AI SDK-compatible structured frontend event before the replacement text:

  • UI streams emit data-input-guardrail-blocked with { code, reason, message, guardrailId?, guardrailName?, severity? }, so useChat consumers can show localized UI without matching the fallback message string.
  • The docs show the type-safe AI SDK pattern with UIMessage<unknown, { "input-guardrail-blocked": InputGuardrailBlockedEventData }>.
  • Blocked UI streams finish with finishReason: "error".
  • Custom fullStream consumers receive an input-guardrail-blocked part with the same payload.

Parallel input guardrails can allow or block only. If a parallel guardrail returns action: "modify", VoltAgent raises a configuration error; input modification remains supported by the default blocking mode.

The speculative input guardrail runtime now lives in agent/streaming/input-guardrail-stream.ts, keeping agent.ts focused on orchestration.

Tool execution waits for pending parallel input guardrails before running side-effectful server-side tool handlers.

fixes (issue)

N/A

Notes for reviewers

Validation run:

  • pnpm --filter @voltagent/core typecheck
  • pnpm --filter @voltagent/core test:single src/agent/agent.spec.ts src/agent/streaming/guardrail-stream.spec.ts src/agent/streaming/output-guardrail-stream-runner.spec.ts src/agent/memory-persist-queue.spec.ts src/agent/memory-persistence.integration.spec.ts src/agent/guardrail.integration.spec.ts src/agent/guardrail.spec.ts src/agent/conversation-buffer.spec.ts (176 tests)
  • pnpm --filter @voltagent/core test:single src/agent/guardrail.integration.spec.ts src/agent/guardrail.spec.ts src/agent/streaming/guardrail-stream.spec.ts (39 tests after the AI SDK compatibility polish)
  • pnpm --filter @voltagent/core lint

lint exits successfully and still reports the existing cognitive complexity warnings in agent.ts, workflow/steps/and-agent.ts, and workspace/filesystem/backends/filesystem.ts.

Summary by CodeRabbit

  • New Features
    • Added parallel input guardrails for streamText, including configurable execution/stream buffering behavior.
    • Buffered streamed output until parallel guardrails resolve; when blocked, replaces streamed content with the guardrail message and prevents assistant output persistence.
    • Added conversation buffer checkpoints (create/restore) to support guarded stream replacement.
  • Bug Fixes
    • Improved guardrail block error details with guardrail metadata; modify inputs now fail when modifications are disallowed.
    • Tool execution is gated to avoid running when blocked.
  • Documentation
    • Expanded guardrails overview and API guidance for execution modes and UI/streaming event behavior (data-input-guardrail-blocked).
  • Tests
    • Extended integration/unit tests for parallel blocking across text/full/UI streams and event forwarding.