Guardrails API Client

0.4.0 · active · verified Fri Apr 17

The `guardrails-api-client` is a Python library providing a simple interface to interact with the Guardrails API for AI application development. It is currently at version 0.4.0 and follows a rapid release cadence common for pre-1.0 libraries, indicating active development with potential for API changes.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the Guardrails API client, authenticates using an environment variable, and demonstrates a basic call to the `create_run` endpoint. This example checks an LLM output against a specified guard.

import guardrails_api_client
import os

# Ensure GUARDRAILS_API_KEY is set in your environment
# Example: export GUARDRAILS_API_KEY="your_api_key_here"
api_key = os.environ.get("GUARDRAILS_API_KEY", "")

if not api_key:
    print("Error: GUARDRAILS_API_KEY environment variable is not set.")
    print("Please set it before running the quickstart.")
else:
    client = guardrails_api_client.Client(
        base_url="https://api.guardrails.ai", # Or your custom base URL
        api_key=api_key,
    )

    try:
        # Replace 'your-guard-id' with an actual guard ID from your Guardrails account
        response = client.v1.create_run(
            llm_output="Hello, I am an AI model.",
            guard_id="your-guard-id",
            # ... other parameters like metadata, stream etc.
        ).result

        print(f"Is valid: {response.is_valid}")
        if not response.is_valid:
            print(f"Reasons: {response.reasons}")
            print(f"Fixed output: {response.fixed_output}")

    except guardrails_api_client.errors.V1CreateRunError as e:
        print(f"API Error: {e.response.json() if e.response else e}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

view raw JSON →