ant_ai.hooks.integrations.guardrails_ai
GuardrailsAIHook
pydantic-model
Bases: AgentHook, BaseModel
Wraps a guardrails.Guard instance as an AgentHook.
Only overrides after_model — validates the LLM output text and
returns PostModelRetry if validation fails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
guard
|
A configured |
required | |
num_reasks
|
How many times guardrails may internally call an LLM to
fix invalid output before handing control back to ant-ai. The
default ( |
required | |
api_key
|
API key forwarded to the LLM used by guardrails during
internal reasks. Only relevant when |
required |
.. note::
Guard is not thread-safe. Validation calls on a shared hook
instance are serialized with an internal lock so concurrent agent
invocations (e.g. inside an A2A server) do not race on the guard's
internal history.
Example::
```python
from guardrails import Guard
from guardrails.hub import ValidJson
hook = GuardrailsAIHook(guard=Guard().use(ValidJson))
agent = Agent(..., hooks=[hook])
```
See examples/guardrails_agent.py for a full safety pipeline using
ToxicLanguage and DetectPII validators.
Show JSON schema:
{
"description": "Wraps a ``guardrails.Guard`` instance as an ``AgentHook``.\n\nOnly overrides ``after_model`` \u2014 validates the LLM output text and\nreturns ``PostModelRetry`` if validation fails.\n\nArgs:\n guard: A configured ``guardrails.Guard`` instance.\n num_reasks: How many times guardrails may internally call an LLM to\n fix invalid output before handing control back to ant-ai. The\n default (``0``) disables guardrails' own reask loop so ant-ai's\n retry mechanism stays in control. Set to a positive value only\n when the guard has an ``llm_api`` configured and you want\n guardrails to attempt self-correction before ant-ai retries.\n api_key: API key forwarded to the LLM used by guardrails during\n internal reasks. Only relevant when ``num_reasks > 0``.\n\n.. note::\n ``Guard`` is not thread-safe. Validation calls on a shared hook\n instance are serialized with an internal lock so concurrent agent\n invocations (e.g. inside an A2A server) do not race on the guard's\n internal history.\n\nExample::\n\n ```python\n from guardrails import Guard\n from guardrails.hub import ValidJson\n\n hook = GuardrailsAIHook(guard=Guard().use(ValidJson))\n agent = Agent(..., hooks=[hook])\n ```\n\nSee ``examples/guardrails_agent.py`` for a full safety pipeline using\n``ToxicLanguage`` and ``DetectPII`` validators.",
"properties": {
"guard": {
"title": "Guard"
},
"num_reasks": {
"default": 0,
"minimum": 0,
"title": "Num Reasks",
"type": "integer"
},
"api_key": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Api Key"
}
},
"required": [
"guard"
],
"title": "GuardrailsAIHook",
"type": "object"
}
Config:
arbitrary_types_allowed:True
Fields:
-
guard(SkipValidation[Any]) -
num_reasks(int) -
api_key(str | None)
Source code in src/ant_ai/hooks/integrations/guardrails_ai.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |