Skip to main content

traceredact

PyPI Python CI License: Apache-2.0 Website

traceredact

Redact PII and secrets from AI prompts, agent traces and tool-call arguments before they reach your loggers / observability backend. — traceredact.com

LLM apps log everything — prompts, agent traces, tool-call arguments — into Langfuse / Helicone / Datadog / your own DB. Customer PII and API keys leak into those traces. traceredact is a small, dependency-light library that detects and redacts that data deterministically, in-process, before it leaves you.

It is content-based: it catches a sk-… key or a credit-card number even when it sits under an innocuous JSON key — not just well-known field names.

A missed secret is a real incident, so detection is treated as safety-critical: bounded (ReDoS-safe) patterns, entropy fallback, Luhn/IBAN validation, and adversarial evasion fixtures.

Install

pip install traceredact          # or: uv add traceredact

Usage (3 lines)

from traceredact import redact

result = redact({"args": {"email": "a@b.com", "key": "sk-1234567890abcdefABCDEFGH"}})
print(result.value)      # {'args': {'email': '[REDACTED:pii]', 'key': '[REDACTED:secret]'}}
print(result.findings)   # [Finding(detector_id='pii.email', json_path='args.email', ...), ...]

redact() accepts a string, dict, list, or any nested mix. The input is never mutated; result.value is a redacted copy and result.findings lists every hit with its detector_id, category, confidence, json_path and span.

CLI (CI-gateable)

traceredact scan ./logs/            # report findings as a table; exit 1 if any
traceredact scan trace.json -f json # machine-readable output for CI
traceredact redact trace.json -o redacted.json

scan exits non-zero when anything is found, so you can gate a CI job on it.

SDK integrations

from openai import OpenAI
from traceredact.integrations.openai import wrap_openai

client = wrap_openai(OpenAI())   # prompts + completions now redacted in-flight

Also: traceredact.integrations.anthropic.wrap_anthropic(client) and traceredact.integrations.langchain.RedactingCallbackHandler().

Async clients are supported via wrap_async_openai / wrap_async_anthropic.

Streaming

Redact a stream of text deltas without buffering the whole response — a secret spanning chunk boundaries is still caught (carry-over window):

from traceredact import redact_stream
for piece in redact_stream(token_deltas):   # also: redact_stream_async(...)
    log(piece)

# OpenAI async streams:
from traceredact.integrations.openai import redact_content_stream
async for safe_text in redact_content_stream(await client.chat.completions.create(..., stream=True)):
    ...

Structured objects

pydantic models, dataclasses and attrs instances are traversed automatically (redacted to dicts). Disable with Policy(traverse_objects=False).

Encoded payloads (opt-in)

Policy(decode_payloads=True) base64-decodes blobs one layer and, if the decoded text contains a high-confidence secret, redacts the whole blob.

Examples

Runnable, heavily-commented scenarios live in examples/: a drop-in logging.Filter, FastAPI/ASGI middleware, streaming, redacting traces before your DB/Langfuse, custom policies, and a CI gate.

Policy file (traceredact.yml)

Drop a traceredact.yml in your repo root (auto-discovered) or pass --policy:

entropy_threshold: 4.0
min_entropy_len: 20
disabled_detectors:
  - pii.phone
allowlist:
  - "noreply@example.com"
allow_patterns:
  - ".*@example\\.com"
placeholder: "[REDACTED:{category}]"
hash_correlation: false        # set true + hash_key to emit correlation tags
custom_patterns:
  - id: custom.internal_user_id
    category: pii
    regex: "ACME-USR-[0-9]{8}"
    confidence: 0.95

See traceredact.yml in this repo for a fully-commented example.

Detectors

Secrets: secrets.openai_key, secrets.anthropic_key, secrets.aws_access_key, secrets.github_token, secrets.slack_token, secrets.slack_webhook, secrets.discord_webhook, secrets.google_api_key, secrets.stripe_key, secrets.sendgrid_key, secrets.twilio_key, secrets.huggingface_token, secrets.npm_token, secrets.pypi_token, secrets.azure_storage_key, secrets.jwt, secrets.private_key, secrets.pgp_private_key, secrets.basic_auth_url, secrets.bearer_token, secrets.env_assignment, secrets.high_entropy.

PII: pii.email, pii.credit_card (Luhn), pii.iban (mod-97), pii.ipv4, pii.phone, pii.us_ssn.

Secret pattern hits are deterministic (confidence 1.0); fuzzy heuristics (entropy, phone, IP) carry lower confidence so policy thresholds can gate them.

Design & safety

  • Deterministic, no data retained. Pure functions; nothing is stored.
  • Copy, never mutate. Your objects are untouched.
  • ReDoS-safe. Cheap literal prefilters gate bounded regexes; no nested quantifiers; input length is capped.
  • Fail-closed. Hash correlation without a key, or exceeding max_depth, raises rather than silently leaking.

Detectors were hardened against adversarial evasion cases (see tests/test_evasion.py).

License

Apache-2.0.

Metadata

Release files for traceredact 0.2.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for traceredact 0.2.4
File Size Uploaded Provenance
traceredact-0.2.4.tar.gz 136.5 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for traceredact 0.2.4
File Interpreter ABI Platform
traceredact-0.2.4-py3-none-any.whl Python 3 none any Details

Total release size: 180.0 kB

Release files / traceredact-0.2.4.tar.gz

Download URL traceredact-0.2.4.tar.gz
Size 136.5 kB
Tags Source
SHA-256 checksum
How to use checksums
78273e1169315169ef6886d16539744e61e195c340b1799eaea7be1e80252881
BLAKE2b-256 checksum
How to use checksums
fc91fac7c22846c871845df02d21f24e4f45e1a6b928cbb83ba2b4c99f868247
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 7, 2026.

Transparency log

Release files / traceredact-0.2.4-py3-none-any.whl

Download URL traceredact-0.2.4-py3-none-any.whl
Size 43.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9b721a5c3a2167e22f7a1f2ff453472bff860fb6160f246a548c97baf57bc3ab
BLAKE2b-256 checksum
How to use checksums
a4aa7152f8aa4f95e73b7a8212694abae0669d69ad4fbe3f8f91ce6e6eb1b14e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 7, 2026.

Transparency log

Security for traceredact 0.2.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for traceredact-0.2.4.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 7, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page