Skip to content

Microsoft.Extensions.AI Integration

Cross-SDK comparison

See the centralized MEAI documentation for feature matrices and comparisons across all tryAGI SDKs.

The Guardrails SDK provides AIFunction tool wrappers compatible with Microsoft.Extensions.AI. These tools can be used with any IChatClient to give AI models LLM output validation, hallucination detection, and PII protection capabilities.

Available Tools

Tool Method Description
ValidateWithGuard AsValidateTool() Validates LLM output against a named Guard
ListGuards AsListGuardsTool() Lists all available Guards
GetGuard AsGetGuardTool() Gets a specific Guard's configuration

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
using Guardrails;
using Microsoft.Extensions.AI;

// Create the Guardrails client (self-hosted server)
using var guardrailsClient = new GuardrailsClient(apiKey);

// Create tools for use with any IChatClient
var tools = new[]
{
    guardrailsClient.AsValidateTool(),
    guardrailsClient.AsListGuardsTool(),
    guardrailsClient.AsGetGuardTool(),
};

// Use with any IChatClient (OpenAI, Anthropic, Ollama, etc.)
IChatClient chatClient = /* your chat client */;

var response = await chatClient.GetResponseAsync(
    "Validate this text against the 'content-safety' guard: 'Hello world'",
    new ChatOptions { Tools = [..tools] });

Tool Details

ValidateWithGuard

The primary tool for content validation. It takes a guard name and LLM output text, then returns:

  • Whether validation passed or failed
  • Validation summaries per validator
  • Error spans highlighting problematic content
  • Any suggested fixes
1
2
// With custom re-ask count
var validateTool = guardrailsClient.AsValidateTool(numReasks: 3);

ListGuards

Lists all Guards the user has access to, including their validators and configurations.

GetGuard

Fetches detailed configuration for a specific Guard by name.