Types: Python
Type definitions, Protocols, and result types for Guardrails.
This module provides core types for implementing Guardrails, including:
- The
TokenUsagedataclass, representing token consumption from LLM-based guardrails. - The
GuardrailResultdataclass, representing the outcome of a guardrail check. - The
CheckFnProtocol, a callable interface for all guardrail functions.
CheckFn
module-attribute
CheckFn = Callable[
[TContext, TIn, TCfg], MaybeAwaitableResult
]
Type alias for a guardrail function.
A guardrail function accepts a context object, input data, and a configuration object,
returning either a GuardrailResult or an awaitable resolving to GuardrailResult.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
TContext
|
TypeVar
|
The context type (often includes resources used by a guardrail). |
required |
TIn
|
TypeVar
|
The input data to validate or check. |
required |
TCfg
|
TypeVar
|
The configuration type, usually a Pydantic model. |
required |
Returns: GuardrailResult or Awaitable[GuardrailResult]: The outcome of the guardrail check.
TokenUsage
dataclass
Token usage statistics from an LLM-based guardrail.
This dataclass encapsulates token consumption data from OpenAI API responses. For providers that don't return usage data, the unavailable_reason field will contain an explanation.
Attributes:
| Name | Type | Description |
|---|---|---|
prompt_tokens |
int | None
|
Number of tokens in the prompt. None if unavailable. |
completion_tokens |
int | None
|
Number of tokens in the completion. None if unavailable. |
total_tokens |
int | None
|
Total tokens used. None if unavailable. |
unavailable_reason |
str | None
|
Explanation when token usage is not available (e.g., third-party models). None when usage data is present. |
Source code in src/guardrails/types.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
GuardrailLLMContextProto
Bases: Protocol
Protocol for context types providing an OpenAI client.
Classes implementing this protocol must expose an OpenAI client
via the guardrail_llm attribute. For conversation-aware guardrails
(like prompt injection detection), they can also access conversation_history
containing the full conversation history.
Attributes:
| Name | Type | Description |
|---|---|---|
guardrail_llm |
AsyncOpenAI | OpenAI
|
The OpenAI client used by the guardrail. |
conversation_history |
list
|
Full conversation history for conversation-aware guardrails. |
Source code in src/guardrails/types.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
get_conversation_history
get_conversation_history() -> list | None
Get conversation history if available, None otherwise.
Source code in src/guardrails/types.py
69 70 71 | |
GuardrailResult
dataclass
Result returned from a guardrail check.
This dataclass encapsulates the outcome of a guardrail function, including whether a tripwire was triggered, execution failure status, and any supplementary metadata.
Attributes:
| Name | Type | Description |
|---|---|---|
tripwire_triggered |
bool
|
True if the guardrail identified a critical failure. |
execution_failed |
bool
|
True if the guardrail failed to execute properly. |
original_exception |
Exception | None
|
The original exception if execution failed. |
info |
dict[str, Any]
|
Additional structured data about the check result, such as error details, matched patterns, or diagnostic messages. Implementations may include a 'checked_text' field containing the processed/validated text when applicable. Defaults to an empty dict. |
Source code in src/guardrails/types.py
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 | |
__post_init__
__post_init__() -> None
Validate required fields and consistency.
Source code in src/guardrails/types.py
97 98 99 100 101 | |
extract_token_usage
extract_token_usage(response: Any) -> TokenUsage
Extract token usage from an OpenAI API response.
Attempts to extract token usage data from the response's usage attribute.
Works with both Chat Completions API and Responses API responses.
For third-party models or responses without usage data, returns a TokenUsage
with None values and an explanation in unavailable_reason.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Any
|
An OpenAI API response object (ChatCompletion, Response, etc.) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
TokenUsage |
TokenUsage
|
Token usage statistics extracted from the response. |
Source code in src/guardrails/types.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
token_usage_to_dict
token_usage_to_dict(
token_usage: TokenUsage,
) -> dict[str, Any]
Convert a TokenUsage dataclass to a dictionary for inclusion in info dicts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_usage
|
TokenUsage
|
TokenUsage instance to convert. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary representation suitable for GuardrailResult.info. |
Source code in src/guardrails/types.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
aggregate_token_usage_from_infos
aggregate_token_usage_from_infos(
info_dicts: Iterable[dict[str, Any] | None],
) -> dict[str, Any]
Aggregate token usage from multiple guardrail info dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
info_dicts
|
Iterable[dict[str, Any] | None]
|
Iterable of guardrail info dicts (each may contain a
|
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary mirroring GuardrailResults.total_token_usage output. |
Source code in src/guardrails/types.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
total_guardrail_token_usage
total_guardrail_token_usage(result: Any) -> dict[str, Any]
Get aggregated token usage from any guardrails result object.
This is a unified interface that works across all guardrails surfaces: - GuardrailsResponse (from GuardrailsAsyncOpenAI, GuardrailsOpenAI, etc.) - GuardrailResults (direct access to organized results) - Agents SDK RunResult (from Runner.run with GuardrailAgent)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Any
|
A result object from any guardrails client. Can be: - GuardrailsResponse with guardrail_results attribute - GuardrailResults with total_token_usage property - Agents SDK RunResult with *_guardrail_results attributes |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with aggregated token usage: - prompt_tokens: Sum of all prompt tokens (or None if no data) - completion_tokens: Sum of all completion tokens (or None if no data) - total_tokens: Sum of all total tokens (or None if no data) |
Example
# Works with OpenAI client responses
response = await client.responses.create(...)
tokens = total_guardrail_token_usage(response)
# Works with Agents SDK results
result = await Runner.run(agent, input)
tokens = total_guardrail_token_usage(result)
print(f"Used {tokens['total_tokens']} guardrail tokens")
Source code in src/guardrails/types.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | |