Skip to content

Python SDK

The xybern-redact package is a thin Python client for the Xybern Redact API. Install it with pip and call the same anonymization, vault, and proxy endpoints without writing any HTTP code yourself.

pip install xybern-redact

Requirements

  • Python 3.9+
  • A Xybern workspace API key (xr_live_...) from the API Keys tab in your dashboard

Quick start

from xybern_redact import RedactClient

client = RedactClient(api_key="xr_live_YOUR_KEY")

result = client.anonymize("Michael Chen signed the contract on 12 March 2024.")
print(result.text)
# "Morgan Ross signed the contract on 11 April 2024."

print(result.entity_count)   # 2
print(result.entities)       # {"PERSON": 1, "DATE": 1}

Anonymize then call any LLM yourself

Strip PII before sending to an LLM, then restore real values in the response:

from xybern_redact import RedactClient
from anthropic import Anthropic

redact = RedactClient(api_key="xr_live_YOUR_KEY")
llm    = Anthropic(api_key="sk-ant-...")

anon = redact.anonymize("Summarise the contract signed by Michael Chen at Goldman Sachs.")

response = llm.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=512,
    messages=[{"role": "user", "content": anon.text}],
)

original = redact.deanonymize(response.content[0].text, anon.entity_map)
print(original)
# Michael Chen and Goldman Sachs are restored.

Use the built-in proxy

The proxy handles anonymization and de-anonymization automatically - you send the original text and get the original names back in the response:

response = client.chat(
    model="claude-sonnet-4-6",
    messages=[{"role": "user", "content": "Summarise the contract signed by Michael Chen."}],
)
print(response.content)
# Response already has real names restored.

Anonymize a file

result = client.anonymize_file("contract.pdf")

with open("contract_clean.pdf", "wb") as f:
    f.write(result.content)

print(f"{result.entity_count} entities anonymized")

Multi-turn conversations

Pass the same thread_id across calls to keep pseudonyms consistent throughout a conversation:

thread = "session_abc123"

r1 = client.anonymize("Michael Chen signed.", thread_id=thread)
r2 = client.anonymize("Chen reviewed the annexes.", thread_id=thread)

# Both map "Chen" to the same pseudonym across the thread.

Error handling

from xybern_redact import RedactClient, AuthenticationError, APIError

try:
    client = RedactClient(api_key="xr_live_YOUR_KEY")
    result = client.anonymize(text)
except AuthenticationError:
    print("Invalid or missing API key.")
except APIError as e:
    print(f"Server error {e.status_code}: {e}")

Context manager

with RedactClient(api_key="xr_live_YOUR_KEY") as client:
    result = client.anonymize("...")

Return types

AnonymizeResult

Field Type Description
text str Anonymized text
entity_map dict[str, str] Pseudonym → real value mapping for de-anonymization
entity_count int Total entities replaced
entities dict[str, int] Count per entity type
leakage_detected bool Whether PII leakage was found

ChatResult

Field Type Description
content str Response text with real values restored
model str Model used
usage dict Token usage from the upstream provider

FileResult

Field Type Description
content bytes Anonymized file bytes
entity_count int Total entities replaced