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_keydefaults to theENKRYPTAI_API_KEYenvironment variable andbase_urltoENKRYPTAI_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.AsyncClientashttp_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_waitcaps the total seconds one guard call spends waiting, andmax_retries, if set, the number of retries; after that the guard’son_errorapplies.max_concurrencycaps 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, sorestoreputs back what any of them redacted.This is not a capability: it goes nowhere in
capabilities=, its guards do.- Parameters:
- injection_attack(*, on_violation='block', on_error='block')#
Flag prompt injection and jailbreak attempts.
- toxicity(*, on_violation='block', on_error='block')#
Flag hateful, harassing, violent or otherwise toxic text.
- code_of_conduct(rules=None, *, policy=None, on_violation='block', on_error='block')#
Flag text that breaks your rules.
Pass the
rulesinline, or the name of a Code of Conductpolicysaved in Enkrypt, not both. One guard works on the prompt and on the output.
- 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 localredact_secrets, to catch what the regexes miss.
- pii(*, entities, on_violation='redact', on_error='block')#
Find personal data, and by default replace it with placeholders.
entitiesare 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;restoreputs it back. A mappingon_violationsets an action per entity type, with"pii"covering those not listed; the rest redact.
- 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_violationsays what happens when one fires. A single action applies to all of them. A mapping is merged overDEFAULT_ACTIONS, so{"injection_attack": "raise"}changes only that detector, and{"pii": "redact"}redacts PII forrestoreto put back.
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.
inputandtool_resultrun the input detectors, since a tool result is untrusted input to the model;outputruns the output detectors.- Parameters:
- property input: Callable[[RunContext[Any], object], Awaitable[GuardrailResult]]#
For
InputGuardrail: 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.
- async save(conversation_id, placeholders)#
Replace the conversation’s placeholders and values.
- class pydantic_ai_enkrypt.InMemoryStore(max_conversations=1024)#
Keeps the latest
max_conversationsconversations 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.
responseis Enkrypt’s full response.- Parameters:
message (str)
response (Response)
- Return type:
None
- exception pydantic_ai_enkrypt.EnkryptOutputBlocked(message, response)#
Enkrypt AI blocked the output.
responseis 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.
responseis Enkrypt’s full response.
- exception pydantic_ai_enkrypt.EnkryptError#
Enkrypt AI could not be reached or returned an error.
Types#
GuardThe type of every guard:
Callable[[RunContext[Any], object], Awaitable[GuardrailResult]], a function the harness guardrails call with the run’s context and the value.