Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Guardrails ai Guardrails Guard Load

From Leeroopedia
Knowledge Sources
Domains Client_Server, API
Last Updated 2026-02-14 00:00 GMT

Overview

Concrete classmethod for fetching a Guard from a remote Guardrails API server provided by the guardrails package.

Description

The Guard.load classmethod connects to a Guardrails API server, fetches the Guard's configuration (name, validators, output schema), and returns a Guard instance configured to proxy all validation calls to the server. The Guard operates identically to a local Guard from the caller's perspective, but validation is executed remotely.

Usage

Call Guard.load(name="my_guard", base_url="http://server:8000") to fetch and use a remote Guard.

Code Reference

Source Location

  • Repository: guardrails
  • File: guardrails/guard.py
  • Lines: L1277-1312

Signature

@classmethod
def load(
    cls,
    name: str,
    *,
    api_key: Optional[str] = None,
    base_url: Optional[str] = None,
    history_max_length: Optional[int] = None,
) -> Optional["Guard"]:
    """Fetches and loads a Guard from your guardrails-api server.

    Args:
        name: Name of the Guard on the server
        api_key: API key (default from GUARDRAILS_API_KEY env var)
        base_url: Server URL (default from GUARDRAILS_BASE_URL or http://localhost:8000)
        history_max_length: Max history entries to retain
    """

Import

from guardrails import Guard

I/O Contract

Inputs

Name Type Required Description
name str Yes Name of the Guard on the server
api_key Optional[str] No API key (default from GUARDRAILS_API_KEY env var)
base_url Optional[str] No Server URL (default from GUARDRAILS_BASE_URL or http://localhost:8000)
history_max_length Optional[int] No Max history entries to keep locally

Outputs

Name Type Description
guard Optional[Guard] Guard instance with _use_server=True, proxying calls to server; None if not found

Usage Examples

Basic Remote Guard Usage

from guardrails import Guard

# Fetch Guard from server
guard = Guard.load(
    name="ContentGuard",
    base_url="http://localhost:8000",
)

# Use exactly like a local Guard
result = guard(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Generate content."}],
)
print(result.validated_output)

With Environment Variables

import os
os.environ["GUARDRAILS_BASE_URL"] = "http://guardrails-server:8000"
os.environ["GUARDRAILS_API_KEY"] = "my-api-key"

# base_url and api_key are read from env vars
guard = Guard.load(name="ContentGuard")

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment