Events

Record GuardrailEvent rows: the audit trail of guardrails.

For the pii and secrets categories, the excerpt is masked, so that the event does not store the personal data or secret that the guardrail detected: personal data keeps its last four characters, and secrets only their length.

smarter.apps.guardrail.services.events.excerpt(finding, segment_text)[source]

Return the excerpt of a finding, for its event: its matches, or the segment, masked for the pii and secrets categories.

Parameters:
  • finding (GuardrailFinding) – The finding, whose first MAX_EXCERPT_MATCHES matches are excerpted.

  • segment_text (str | None) – The text of the segment the finding is about, used when the finding has no matches.

Return type:

str | None

Returns:

The excerpt, at most MAX_EXCERPT_LENGTH characters, or None when the finding has no matches and the segment is empty.

smarter.apps.guardrail.services.events.mask(text, category)[source]

Mask a match, for an event’s excerpt.

Parameters:
  • text (str) – The matched text.

  • category (str) – The guardrail’s category.

Return type:

str

Returns:

For secrets, only the length; for personal data, the last four characters; otherwise the text.

smarter.apps.guardrail.services.events.record_event(*, guardrail, stage, disposition, finding=None, segment_text=None, error=None, llmclient=None, session_key=None)[source]

Record a guardrail event.

A failure to record is logged, and does not fail the prompt.

Return type:

GuardrailEvent | None

Returns:

The event, or None if it could not be recorded.

smarter.apps.guardrail.services.events.stage_name(stage)[source]

Return the Guardrail stage of a pipeline stage: input or output.

Return type:

str