Skip to content

RedactSecrets

Rewrites text output, replacing secrets with a placeholder. It never blocks or retries.

Parameter Type Default Description
patterns Sequence[str] all seven Any of openai_api_key, anthropic_api_key, aws_access_key, github_token, slack_token, private_key, jwt
placeholder str '[REDACTED]' Replacement text
redact_stream bool True Also redact streamed text (deltas, partial outputs, event streams, realtime)
stream_holdback int 128 Characters held back while streaming, so a secret split across chunks stays hidden

An unknown pattern name raises ValueError at construction.

from pydantic_ai import Agent
from pydantic_ai.models.test import TestModel
from pydantic_ai_guardrails import RedactSecrets
model = TestModel(custom_output_text='Your key is AKIAABCDEFGHIJKLMNOP')
agent = Agent(model, capabilities=[RedactSecrets(patterns=['aws_access_key'], placeholder='***')])
print(agent.run_sync('What is my key?').output) # Your key is ***

With redact_stream=True, the model’s stream is redacted at its source, so no read path, including stream_text’s first chunk and the stored message history, ever sees a secret. Several RedactSecrets compose. See Streaming for how hold-back works and its limits.

RedactSecrets has no on_fail. It edits string output in place and passes structured (non-string) output through unchanged. If you need to stop the run instead, use MatchRegex or an OutputGuardrail with on_fail='raise'.