Skip to main content
The Openlayer.Guardrails packages bring the Python guardrails library to C# / .NET. The PII detection is a port of the Python library and gives the same results: a test suite runs the Python library as the reference. Both packages support .NET 8+ and any netstandard2.0 runtime.

Install

PII

CheckInput takes a function’s arguments as a dictionary, and CheckOutput(output, inputs) takes its return value. Both walk nested dictionaries and lists. A Modify result rebuilds the same structure with the strings redacted.
The default entities, the confidence threshold (0.7) and the keys in result.Metadata (detected_entities, blocked_entities, redacted_entities, …) are the same as in the Python PIIGuardrail. A guardrail instance is thread-safe, so you can share one instance in your app.

CPNI

CpniGuardrail detects telecom customer proprietary network information (47 U.S.C. 222). It does not block by default. When you give it a key, it replaces each identifier with a keyed pseudonym, so you can still join records for the same subscriber. The pseudonyms are the same as the Python library’s for the same key.

Toxicity and prompt injection

The model-based guardrails run on ONNX Runtime. They do not download models: you export each model to ONNX once, and give the guardrail the folder.
The thresholds, categories and metadata keys are the same as in the Python guardrails. Instead of ModelDirectory, you can set the OPENLAYER_GUARDRAILS_MODELS_DIR environment variable to a folder that contains the folders above.

Export the models

Export the models with Optimum on a machine with Python. Each output folder holds model.onnx and the tokenizer files.
Use --task token-classification for dslim/bert-base-NER. meta-llama/Llama-Prompt-Guard-2-86M is a gated model: accept its license on Hugging Face, and set HF_TOKEN before you download it. To pin the model version, add --revision <commit-sha> to hf download. If the Hugging Face Hub is not reachable, set HF_ENDPOINT (and HF_TOKEN) to use a mirror, for example a Hugging Face remote repository in Artifactory or Nexus. For air-gapped deployments, export the models on a machine with access, then copy the models folder to the target.

Names, places and organizations

The Python PIIGuardrail finds PERSON, LOCATION and ORGANIZATION with a spaCy model, which does not run on .NET. To detect these entities in .NET, give the NLP engine a named-entity model. BertNerModel runs dslim/bert-base-NER:
The named-entity model is different from spaCy, so the detected PERSON, LOCATION, ORGANIZATION and DATE_TIME spans can be different from the Python library’s. All other entities are the same.

Tracing

The Python guardrails plug into the @trace decorator. The C# / .NET SDK does not have tracing, so call CheckInput before and CheckOutput after your LLM call, and act on result.Action.