LangChain RunnableSequence Bijective Sanitization Middleware
Wraps any LangChain Expression Language (LCEL) chain in a stateful volatile closure. Raw user variables are sanitized in local process RAM prior to ChatOpenAI or ChatAnthropic invocation and restored locally before returning to the caller.
import { PrivacyScrubberEngine } from "@privacyscrubber/sdk";
import { RunnableLambda } from "@langchain/core/runnables";
const engine = new PrivacyScrubberEngine({ profile: "finance" });
export async function invokeZeroTrustChain(chain, rawPrompt) {
const { sanitizedText, tokenMap } = engine.sanitize(rawPrompt);
try {
const llmResponse = await chain.invoke({ input: sanitizedText });
return engine.restore(llmResponse.content, tokenMap);
} finally {
engine.destroy(tokenMap); // Invariant 3 CSPRNG zeroization
}
}