npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@coralogix/guardrails-langchain

v0.5.0

Published

AI Guardrails Langchain TypeScript adapter

Downloads

27

Readme

LangChain Aporia Guardrails Integration

A TypeScript wrapper that adds Aporia Guardrails to any LangChain BaseChatModel, enabling real-time content filtering and safety controls for both prompts and responses.

Installation

npm install

Configuration

Environment Variables

Create a .env file in the project root with your API keys:

# Aporia Guardrails Configuration
APORIA_PROJECT_ID=your_project_id_here
APORIA_API_KEY=your_api_key_here

# OpenAI API Key
OPENAI_API_KEY=your_openai_api_key_here

# Alternatively, to use Azure OpenAI:
# AZURE_OPENAI_API_KEY=your_azure_openai_api_key_here
# AZURE_OPENAI_API_INSTANCE_NAME=your_azure_openapi_api_instance_name_here
# AZURE_OPENAI_API_DEPLOYMENT_NAME=your_azure_openai_api_deployment_name_here
# AZURE_OPENAI_API_VERSION=your_azure_openapi_api_version_here

You'll need:

  • Aporia Project ID: Your Aporia guardrails project identifier
  • Aporia API Key: Authentication key for the Aporia service
  • OpenAI / Azure OpenAI auth details: Your OpenAI / Azure OpenAI API key for the language model

Quick Start

After setting up your .env file, you can run the examples:

# Run the basic example
npm run example

For Azure OpenAI, please uncomment the relevant section in example.ts.

Usage

Basic Example

import { ChatOpenAI } from "@langchain/openai";
import { HumanMessage } from "@langchain/core/messages";
import { withGuardrails } from "@coralogix/guardrails-langchain";

// Create your base model
const model = new ChatOpenAI({
  model: "gpt-4o-2024-11-20",
  temperature: 0,
  configuration: {
    apiKey: "your-openai-api-key"
  }
});

// Wrap with guardrails
const guardedModel = withGuardrails(model, {
  projectId: "your-aporia-project-id",
  apiKey: "your-aporia-api-key"
});

// Use the model normally - guardrails are applied automatically
const response = await guardedModel.invoke([
  new HumanMessage("Hello, how can you help me today?")
]);

console.log(response.content);

Streaming Example

const guardedModel = withGuardrails(model, {
  projectId: "your-aporia-project-id",
  apiKey: "your-aporia-api-key",
  chunkBatchSize: 25  // Check guardrails every 25 chunks (default: 50)
});

// Optionally override the Aporia Guardrails base URL (default: https://gr-prd.aporia.com)
const guardedModelWithCustomBase = withGuardrails(model, {
  projectId: "your-aporia-project-id",
  apiKey: "your-aporia-api-key",
  baseUrl: "https://gr-prd-eu.example.com",
  chunkBatchSize: 25
});

const stream = await guardedModel.stream([
  new HumanMessage("Tell me a story about space exploration")
]);

for await (const chunk of stream) {
  process.stdout.write(chunk.content);
}

Advanced Configuration

import type { AporiaGuardrailsConfig } from "@coralogix/guardrails-langchain";

const config: AporiaGuardrailsConfig = {
  projectId: "your-project-id",
  apiKey: "your-api-key",
  chunkBatchSize: 100,  // Optional: check every 100 chunks instead of default 200
  // Optional: override the Aporia Guardrails API base URL (defaults to https://gr-prd.aporia.com)
  baseUrl: "https://gr-prd-eu.example.com"
};

const guardedModel = withGuardrails(model, config);

Configuration Options

| Property | Type | Required | Default | Description | |----------|------|----------|---------|-------------| | projectId | string | ✅ | - | Your Aporia guardrails project ID | | apiKey | string | ✅ | - | Your Aporia API key | | chunkBatchSize | number | ❌ | 200 | How often to check guardrails during streaming (every N chunks) | | baseUrl | string | ❌ | https://gr-prd.aporia.com | Optional base URL for the Aporia Guardrails API |

How It Works

  1. Prompt Validation: Before sending to the model, user prompts are validated against your guardrail policies
  2. Response Validation: Model responses are checked before being returned to the user
  3. Streaming Validation: During streaming, content is validated in configurable batches
  4. Action Handling: Based on guardrail responses, content can be:
    • passthrough - Allow content unchanged
    • block - Block content with override message
    • modify - Replace with suggested alternative
    • rephrase - Replace with rephrased version

Error Handling

The wrapper handles guardrail API failures gracefully:

  • Network errors are thrown as exceptions
  • Invalid responses are validated with Zod schemas
  • Failed guardrail checks return override messages

Types

interface AporiaGuardrailsConfig {
  projectId: string;
  apiKey: string;
  chunkBatchSize?: number;
}

type GuardrailsResponse = {
  action: "modify" | "passthrough" | "block" | "rephrase";
  revised_response: string | null;
  // ... other fields allowed
}