langchain-aws-utils
v1.2.13
Published
Utilities and helpers for LangChain and LangGraph with AWS services
Maintainers
Readme
langchain-aws-utils
Utilities and helpers for LangChain and LangGraph with AWS services
Install
npm install langchain-aws-utilsor
yarn add langchain-aws-utilsUsage
Guardrails
import { isGuardrailStopped } from 'langchain-aws-utils/guardrails';
// Check if an LLM result was stopped by guardrails
const llOutput = await model.invoke(messages);
const isStopped = isGuardrailStopped(llOutput);
if (isStopped) {
console.log('LLM result was stopped by guardrails');
}Memory Management
import { cleanMemoryFromBlockedMessages } from 'langchain-aws-utils/memory';
// Clean memory checkpoint from blocked messages
const { cleanedData, shouldOverride } = cleanMemoryFromBlockedMessages(memoryCheckpoint);
if (shouldOverride) {
// Update your memory with the cleaned data
await updateMemory(cleanedData);
}API
Guardrails
isGuardrailStopped(llOutput)
Check if an LLM result was stopped by guardrails. This function examines the LLM result object to determine if guardrails intervened during execution.
Parameters:
llOutput(LLMResult): The LLM result object to check, usually from a LangGraph node callback
Returns: boolean - True if the LLM result was stopped by guardrails
Example:
// In a LangGraph node callback
async handleLLMEnd(llOutput: LLMResult): Promise<any> {
const isStopped = isGuardrailStopped(llOutput);
if (isStopped) {
console.log('LLM result was stopped by guardrails');
}
}Memory Management
cleanMemoryFromBlockedMessages(memoryCheckpoint)
Cleans memory checkpoint traces by removing messages that were blocked by guardrails. This function filters out blocked assistant messages and their causative human messages to maintain clean conversation memory.
Parameters:
memoryCheckpoint(unknown): The memory checkpoint trace object to clean
Returns: { cleanedData: CheckpointTuple; shouldOverride: boolean }
cleanedData: The cleaned checkpoint data with blocked messages removedshouldOverride: Whether the memory should be updated with the cleaned data
Example:
// Clean memory from blocked messages
const { cleanedData, shouldOverride } = cleanMemoryFromBlockedMessages(memoryCheckpoint);
if (shouldOverride) {
// Update your LangGraph memory with the cleaned data
await graph.updateState(cleanedData);
}What it removes:
- Assistant messages that were blocked by guardrails (detected by stop reasons like "blocked" or "guardrail_intervened")
- The causative human messages that led to the blocked assistant responses
- Maintains conversation flow by removing the problematic interaction pairs
Use cases:
- Clean conversation memory after guardrail interventions
- Remove blocked content from chat history
- Maintain clean conversation state in LangGraph applications
