Runtime
Helpers for loading configuration bundles and running guardrails.
This module is the bridge between configuration and runtime execution for guardrail validation. It provides pure helpers for loading config bundles, instantiating guardrails from registry specs, and orchestrating parallel execution of guardrail modules. All logic is pure and side-effect-free except for I/O during config reading.
ConfiguredGuardrail
dataclass
Bases: Generic[TContext, TIn, TCfg]
A configured, executable guardrail.
This class binds a GuardrailSpec definition to a validated configuration
object. The resulting instance is used to run guardrail logic in production
policies. It supports both sync and async check functions.
Attributes:
| Name | Type | Description |
|---|---|---|
definition |
GuardrailSpec[TContext, TIn, TCfg]
|
The immutable guardrail specification. |
config |
TCfg
|
Validated user configuration for this instance. |
suppress_enforcement |
bool
|
When |
Source code in src/mendguardrails/runtime.py
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 | |
run
async
run(ctx: TContext, data: TIn) -> GuardrailResult
Run the guardrail's check function with the provided context and data.
Main entry point for executing guardrails. Supports both sync and async functions, ensuring results are always awaited.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
TContext
|
Runtime context for the guardrail. |
required |
data
|
TIn
|
Input value to be checked. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
GuardrailResult |
GuardrailResult
|
The outcome of the guardrail logic. |
Source code in src/mendguardrails/runtime.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
GuardrailConfig
Bases: BaseModel
Configuration for a single guardrail instance.
Used for serializing, deserializing, and validating guardrail configs as part of a bundle.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The registry name used to look up the guardrail spec. |
config |
dict[str, Any]
|
Raw user configuration for this guardrail. |
suppress_enforcement |
bool
|
When |
Source code in src/mendguardrails/runtime.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
ConfigBundle
Bases: BaseModel
Versioned collection of configured guardrails.
Represents a serializable "bundle" of guardrails to be run as a unit. Suitable for JSON storage and loading.
Attributes:
| Name | Type | Description |
|---|---|---|
guardrails |
list[GuardrailConfig]
|
The configured guardrails. |
version |
int
|
Format version for forward/backward compatibility. |
config |
dict[str, Any]
|
Execution configuration for this bundle. Optional fields include: - concurrency (int): Maximum number of guardrails to run in parallel (default: 10) - suppress_enforcement (bool): If True (default), don't raise exceptions on enforcement |
Source code in src/mendguardrails/runtime.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
Policy
Bases: BaseModel
Three-stage collection of validated ConfigBundles for an LLM policy.
This class groups together guardrail configurations for the three main policy stages:
- pre_flight: Checks that run before the LLM request is issued.
- input: Checks on the user's prompt (may run concurrently with LLM call).
- output: Checks on incremental LLM output.
At least one stage must be provided, but all stages are optional.
Attributes:
| Name | Type | Description |
|---|---|---|
pre_flight |
ConfigBundle | None
|
Guardrails to run before the LLM request. |
input |
ConfigBundle | None
|
Guardrails to run on user input. |
output |
ConfigBundle | None
|
Guardrails to run on generated output. |
version |
int
|
Schema version for the envelope itself. Defaults to 1. |
Example
# All stages
policy = Policy(
pre_flight=load_config_bundle(PRE_FLIGHT),
input=load_config_bundle(INPUT_BUNDLE),
output=load_config_bundle(OUTPUT_BUNDLE),
)
# Just output stage
policy = Policy(
output=load_config_bundle(OUTPUT_BUNDLE),
)
# Print active stages
for stage in policy.stages():
print(stage.version)
Source code in src/mendguardrails/runtime.py
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
is_empty
property
is_empty: bool
Return True when all stages are None (guardrails explicitly disabled).
stages
stages() -> tuple[ConfigBundle, ...]
Return non-None bundles in execution order (pre_flight → input → output).
Source code in src/mendguardrails/runtime.py
189 190 191 | |
JsonString
dataclass
Explicit wrapper to mark a string as a raw JSON config.
Used to distinguish JSON string inputs from other config sources (path/dict).
Attributes:
| Name | Type | Description |
|---|---|---|
content |
str
|
The raw JSON string. |
Source code in src/mendguardrails/runtime.py
217 218 219 220 221 222 223 224 225 226 227 | |
default_policy
default_policy() -> Policy
Return the default guardrail policy (basic PII and content moderation).
Used when no config is provided to clients. Includes: - pre_flight: PII (with block) and Moderation (hate, violence categories) - input: none (add checks via custom policy if needed) - output: Moderation (default category set from empty config)
Returns:
| Name | Type | Description |
|---|---|---|
Policy |
Policy
|
A validated policy with default guardrails. |
Source code in src/mendguardrails/runtime.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
load_config_bundle
load_config_bundle(source: ConfigSource) -> ConfigBundle
Load a ConfigBundle from a path, dict, JSON string, or already-parsed object.
Supported sources
- ConfigBundle: Already validated bundle.
- dict: Raw data, parsed as ConfigBundle.
- Path: JSON file on disk.
- JsonString: Raw JSON string.
Example usage
bundle = load_config_bundle(JsonString('{"guardrails": [...]}'))
bundle = load_config_bundle({"guardrails": [...]})
bundle = load_config_bundle(Path("./config.json"))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
ConfigSource
|
Config bundle input. |
required |
Raises:
| Type | Description |
|---|---|
ConfigError
|
If loading fails. |
ValidationError
|
If model validation fails. |
FileNotFoundError
|
If file doesn't exist. |
Returns:
| Name | Type | Description |
|---|---|---|
ConfigBundle |
ConfigBundle
|
The loaded and validated configuration bundle. |
Source code in src/mendguardrails/runtime.py
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 | |
load_policy
load_policy(source: PolicySource) -> Policy
Load a Policy from a path, dict, JSON string, or already-parsed object.
Supported sources
- Policy: Already validated policy.
- dict: Raw data, parsed as Policy.
- Path: JSON file on disk.
- JsonString: Raw JSON string.
Example usage
policy = load_policy(JsonString('{"pre_flight": {...}, ...}'))
policy = load_policy({"pre_flight": {...}, ...})
policy = load_policy(Path("./policy.json"))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
PolicySource
|
Policy input. |
required |
Raises:
| Type | Description |
|---|---|
ConfigError
|
If loading fails. |
ValidationError
|
If model validation fails. |
FileNotFoundError
|
If file doesn't exist. |
Returns:
| Name | Type | Description |
|---|---|---|
Policy |
Policy
|
The loaded and validated policy configuration. |
Source code in src/mendguardrails/runtime.py
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
instantiate_guardrails
instantiate_guardrails(
bundle: ConfigBundle,
registry: GuardrailRegistry | None = None,
) -> list[ConfiguredGuardrail[Any, Any, Any]]
Instantiate all configured guardrails in a bundle as executable objects.
This function validates each guardrail configuration, retrieves the spec from the registry, and returns a list of fully configured guardrails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bundle
|
ConfigBundle
|
The validated configuration bundle. |
required |
registry
|
GuardrailRegistry
|
Registry mapping names to specs.
If not provided, defaults to |
None
|
Raises:
| Type | Description |
|---|---|
ConfigError
|
If any individual guardrail config is invalid. |
Returns:
| Type | Description |
|---|---|
list[ConfiguredGuardrail[Any, Any, Any]]
|
list[ConfiguredGuardrail[Any, Any, Any]]: All configured/runnable guardrail objects. |
Source code in src/mendguardrails/runtime.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
run_guardrails
async
run_guardrails(
ctx: TContext,
data: TIn,
media_type: str,
guardrails: Iterable[
ConfiguredGuardrail[TContext, TIn, Any]
],
*,
concurrency: int = 10,
result_handler: Callable[
[GuardrailResult], Coroutine[None, None, None]
]
| None = None,
suppress_enforcement: bool | None = None,
stage_name: str | None = None,
raise_guardrail_errors: bool = False,
guardrail_id: str | None = None,
mend_key: str | None = None,
inference_model: str | None = None,
offline: bool = False,
audit_queue: Any | None = None,
) -> list[GuardrailResult]
Run a set of configured guardrails concurrently and collect their results.
Validates context requirements for each guardrail, filters guardrails by the specified media type,
and runs each check function concurrently, up to the specified concurrency limit. Results for all
executed guardrails are collected and returned in order. If any guardrail triggers enforcement,
the function will raise a GuardrailEnforcementTriggered exception unless enforcement suppression is enabled.
An optional asynchronous result handler can be provided to perform side effects (e.g., logging, custom result processing) for each guardrail result as it becomes available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
TContext
|
Context object passed to all guardrail modules. Must satisfy all required fields specified by each guardrail's context schema. |
required |
data
|
TIn
|
The input to be validated by the guardrails. |
required |
media_type
|
str
|
MIME type used to filter which guardrails to execute. (e.g. "text/plain") |
required |
guardrails
|
Iterable[ConfiguredGuardrail[TContext, TIn, Any]]
|
Iterable of configured guardrails to run. |
required |
concurrency
|
int
|
Maximum number of guardrails to run in parallel. Defaults to 10. |
10
|
result_handler
|
Callable[[GuardrailResult], Awaitable[None]]
|
Asynchronous callback function to be invoked for each guardrail result as it is produced. Defaults to None. |
None
|
suppress_enforcement
|
bool | None
|
Controls enforcement behaviour.
|
None
|
stage_name
|
str | None
|
Name of the policy stage (e.g., "pre_flight", "input", "output"). If provided, this will be included in the GuardrailResult info. Defaults to None. |
None
|
raise_guardrail_errors
|
bool
|
If True, raise exceptions when guardrails fail to execute. If False (default), treat guardrail execution errors as safe and continue execution. |
False
|
guardrail_id
|
str | None
|
|
None
|
mend_key
|
str | None
|
Resolved Mend license key used to derive the audit
base URL when |
None
|
inference_model
|
str | None
|
Name of the AI inference model used in the
surrounding API call (e.g. |
None
|
offline
|
bool
|
When |
False
|
audit_queue
|
EventQueue | None
|
Pre-started
:class: |
None
|
Returns:
| Type | Description |
|---|---|
list[GuardrailResult]
|
list[GuardrailResult]: List of results for all executed guardrails. If enforcement suppression is disabled (default) and enforcement is triggered, only results up to the first enforcement may be included. |
Raises:
| Type | Description |
|---|---|
GuardrailEnforcementTriggered
|
Raised if a guardrail enforcement is triggered and
|
ContextValidationError
|
Raised if the provided context does not meet requirements for any guardrail being executed. |
Example
results = await run_guardrails(
ctx=my_ctx,
data="example input",
media_type="text/plain",
guardrails=my_guardrails,
concurrency=4,
suppress_enforcement=True,
stage_name="input",
)
Source code in src/mendguardrails/runtime.py
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 | |
check_plain_text
async
check_plain_text(
text: str,
bundle_path: ConfigSource = Path("mendguardrails.json"),
registry: GuardrailRegistry | None = None,
ctx: Any = None,
**kwargs: Any,
) -> list[GuardrailResult]
Validate plain text input against all configured guardrails for the 'text/plain' media type.
This function loads a guardrail configuration bundle, instantiates all guardrails for the specified registry, and runs each guardrail against the provided text input. It is the recommended entry point for validating plain text with one or more guardrails in an async context.
If no context object (ctx) is provided, a minimal default context will be constructed with
attributes required by the guardrails' context schema (for example, an OpenAI client and any
required fields with safe default values). For advanced use cases, you can supply your own
context object.
Keyword arguments are forwarded to run_guardrails, allowing you to control concurrency,
provide a result handler, or suppress enforcement exceptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The plain text input to validate. |
required |
bundle_path
|
ConfigSource
|
Guardrail configuration bundle. This can be a file path,
dict, JSON string, or |
Path('mendguardrails.json')
|
registry
|
GuardrailRegistry
|
Guardrail registry to use for instantiation. If not provided, the default registry is used. |
None
|
ctx
|
Any
|
Application context object passed to each guardrail. If None (default), a minimal default context will be used. |
None
|
**kwargs
|
Any
|
Additional keyword arguments forwarded to
|
{}
|
Returns:
| Type | Description |
|---|---|
list[GuardrailResult]
|
list[GuardrailResult]: Results from all executed guardrails. |
Raises:
| Type | Description |
|---|---|
ConfigError
|
If the configuration bundle cannot be loaded or is invalid. |
ContextValidationError
|
If the context does not meet required fields for the guardrails. |
GuardrailEnforcementTriggered
|
If a guardrail enforcement is triggered and |
Example
from mendguardrails import check_plain_text
results = await check_plain_text(
"some text",
bundle_path="my_guardrails.json",
concurrency=4,
suppress_enforcement=True,
)
Source code in src/mendguardrails/runtime.py
681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 | |