API reference#

Everything below is importable from pydantic_ai_enkrypt.

Enkrypt#

class pydantic_ai_enkrypt.Enkrypt(api_key=None, *, base_url=None, timeout=30.0, http_client=None, max_wait=60.0, max_retries=None, max_concurrency=None, store=None, enforce=True)#

Your Enkrypt AI account, and the guards that check against it.

api_key defaults to the ENKRYPTAI_API_KEY environment variable and base_url to ENKRYPTAI_BASE_URL, then Enkrypt’s hosted API.

With enforce=False, guards only observe: what each would have done, for a violation or an Enkrypt outage, is logged, and nothing is blocked, raised, retried or redacted. Use it to tune guards on real traffic.

Each request opens and closes its own HTTP connection. To reuse connections, pass an httpx.AsyncClient as http_client; closing it is yours.

When Enkrypt’s rate limit is reached, a guard waits until the limit allows another request and retries, rather than failing. max_wait caps the total seconds one guard call spends waiting, and max_retries, if set, the number of retries; after that the guard’s on_error applies. max_concurrency caps how many requests are in flight at once.

Redacted PII is kept in store (in memory by default), per conversation, for every guard this object creates, so restore puts back what any of them redacted.

This is not a capability: it goes nowhere in capabilities=, its guards do.

Parameters:
  • api_key (str | None)

  • base_url (str | None)

  • timeout (float)

  • http_client (httpx.AsyncClient | None)

  • max_wait (float)

  • max_retries (int | None)

  • max_concurrency (int | None)

  • store (PlaceholderStore | None)

  • enforce (bool)

injection_attack(*, on_violation='block', on_error='block')#

Flag prompt injection and jailbreak attempts.

Parameters:
  • on_violation (Literal['raise', 'block', 'retry', 'audit'])

  • on_error (Literal['block', 'allow'])

Return type:

Callable[[RunContext[Any], object], Awaitable[GuardrailResult]]

toxicity(*, on_violation='block', on_error='block')#

Flag hateful, harassing, violent or otherwise toxic text.

Parameters:
  • on_violation (Literal['raise', 'block', 'retry', 'audit'])

  • on_error (Literal['block', 'allow'])

Return type:

Callable[[RunContext[Any], object], Awaitable[GuardrailResult]]

code_of_conduct(rules=None, *, policy=None, on_violation='block', on_error='block')#

Flag text that breaks your rules.

Pass the rules inline, or the name of a Code of Conduct policy saved in Enkrypt, not both. One guard works on the prompt and on the output.

Parameters:
Return type:

Callable[[RunContext[Any], object], Awaitable[GuardrailResult]]

secrets(*, on_violation='raise', on_error='block')#

Flag credentials: API keys, passwords, tokens.

Raises by default, since a leaked secret is for the application to handle. There is no retry, which would send the secret back to the model. Put it after the harness’s local redact_secrets, to catch what the regexes miss.

Parameters:
  • on_violation (Literal['raise', 'block', 'audit'])

  • on_error (Literal['block', 'allow'])

Return type:

Callable[[RunContext[Any], object], Awaitable[GuardrailResult]]

pii(*, entities, on_violation='redact', on_error='block')#

Find personal data, and by default replace it with placeholders.

entities are Enkrypt’s PII entity types, e.g. ["pii", "person", "ip_address"]. Each value becomes a placeholder such as <<PERSON:1>>, the same one throughout the conversation; restore puts it back. A mapping on_violation sets an action per entity type, with "pii" covering those not listed; the rest redact.

Parameters:
  • entities (Sequence[str])

  • on_violation (Literal['redact', 'raise', 'block', 'retry', 'audit'] | ~collections.abc.Mapping[str, ~typing.Literal['redact', 'raise', 'block', 'retry', 'audit']])

  • on_error (Literal['block', 'allow'])

Return type:

Callable[[RunContext[Any], object], Awaitable[GuardrailResult]]

guardrail(name, *, on_violation=DEFAULT_ACTIONS, on_error='block')#

A guardrail saved in Enkrypt, by name.

Which detectors run is the saved guardrail’s business; on_violation says what happens when one fires. A single action applies to all of them. A mapping is merged over DEFAULT_ACTIONS, so {"injection_attack": "raise"} changes only that detector, and {"pii": "redact"} redacts PII for restore to put back.

Parameters:
  • name (str)

  • on_violation (Literal['raise', 'block', 'retry', 'redact', 'audit'] | ~collections.abc.Mapping[str, ~typing.Literal['raise', 'block', 'retry', 'redact', 'audit']])

  • on_error (Literal['block', 'allow'])

Return type:

Guardrail

property restore: Callable[[RunContext[Any], object], Awaitable[GuardrailResult]]#

Puts back the PII this object’s guards redacted.

For ToolGuardrail(guard=...), so tools get real arguments, and last in OutputGuardrail, after every check, so the user sees real values.

async forget(conversation_id)#

Drop a conversation’s placeholders and the values they stand for.

Parameters:

conversation_id (str)

Return type:

None

Saved guardrails#

class pydantic_ai_enkrypt.Guardrail(name, on_violation, on_error, client, placeholders, enforce=True)#

A saved Enkrypt guardrail, with one guard per edge.

A saved guardrail has separate input and output detectors. input and tool_result run the input detectors, since a tool result is untrusted input to the model; output runs the output detectors.

Parameters:
  • name (str)

  • on_violation (GuardrailAction | Mapping[str, GuardrailAction])

  • on_error (OnError)

  • client (EnkryptClient)

  • placeholders (Placeholders)

  • enforce (bool)

property input: Callable[[RunContext[Any], object], Awaitable[GuardrailResult]]#

For InputGuardrail: the guardrail’s input detectors.

property output: Callable[[RunContext[Any], object], Awaitable[GuardrailResult]]#

For OutputGuardrail: the guardrail’s output detectors.

property tool_result: Callable[[RunContext[Any], object], Awaitable[GuardrailResult]]#

For ToolGuardrail(result_guard=...): the guardrail’s input detectors.

pydantic_ai_enkrypt.DEFAULT_ACTIONS#

Read-only proxy of a mapping.

DEFAULT_ACTIONS is {"secrets": "raise"}, read-only.

Placeholder stores#

class pydantic_ai_enkrypt.PlaceholderStore(*args, **kwargs)#

Where a conversation’s placeholders and their real values are kept.

Each conversation is a mapping from placeholder (<<PERSON:1>>) to value (John Smith). Keep it wherever the conversation’s history is kept, so the two are available together; the values are personal data.

async load(conversation_id)#

The conversation’s placeholders and values; empty if it has none.

Parameters:

conversation_id (str)

Return type:

Mapping[str, str]

async save(conversation_id, placeholders)#

Replace the conversation’s placeholders and values.

Parameters:
Return type:

None

async forget(conversation_id)#

Drop the conversation’s placeholders and values.

Parameters:

conversation_id (str)

Return type:

None

class pydantic_ai_enkrypt.InMemoryStore(max_conversations=1024)#

Keeps the latest max_conversations conversations in this process.

Enough when every run of a conversation happens in one process. When the history is saved and loaded elsewhere, use a store that persists alongside it, or restoring silently stops working for earlier placeholders.

Parameters:

max_conversations (int)

Exceptions#

exception pydantic_ai_enkrypt.EnkryptInputBlocked(message, response)#

Enkrypt AI blocked the prompt. response is Enkrypt’s full response.

Parameters:
  • message (str)

  • response (Response)

Return type:

None

exception pydantic_ai_enkrypt.EnkryptOutputBlocked(message, response)#

Enkrypt AI blocked the output. response is Enkrypt’s full response.

Parameters:
  • message (str)

  • response (Response)

Return type:

None

exception pydantic_ai_enkrypt.EnkryptToolBlocked(tool_name, message, response)#

Enkrypt AI blocked a tool call or result.

response is Enkrypt’s full response.

Parameters:
  • tool_name (str)

  • message (str)

  • response (Response)

Return type:

None

exception pydantic_ai_enkrypt.EnkryptError#

Enkrypt AI could not be reached or returned an error.

Types#

Guard

The type of every guard: Callable[[RunContext[Any], object], Awaitable[GuardrailResult]], a function the harness guardrails call with the run’s context and the value.