Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Protectai Llm guard Input BanCode

From Leeroopedia
Knowledge Sources
Domains Security, Code_Detection
Last Updated 2026-02-14 12:00 GMT

Overview

The BanCode scanner detects code snippets in prompts using the CodeNLBERT classification model, preventing users from submitting programming code to the LLM.

Description

BanCode is an input scanner that uses the vishnun/codenlbert-sm model (a CodeNLBERT variant) to classify whether a given prompt contains programming code. The model performs binary classification, distinguishing between natural language text and code. When code is detected with a confidence score exceeding the configurable threshold (default 0.97), the prompt is flagged as invalid. This scanner is useful for enforcing policies where code submission to the LLM should be restricted, such as preventing source code leakage or limiting the LLM to natural language interactions only. It supports ONNX runtime for optimized inference.

Usage

Use the BanCode scanner when you need to prevent users from including programming code in their prompts. This is relevant in scenarios where code submission poses a security risk (e.g., proprietary code leakage) or where the LLM should only process natural language queries.

Code Reference

Source Location

Signature

class BanCode(Scanner):
    def __init__(
        self,
        *,
        model: Model | None = None,       # default: vishnun/codenlbert-sm
        threshold: float = 0.97,
        use_onnx: bool = False,
    ) -> None: ...

    def scan(self, prompt: str) -> tuple[str, bool, float]: ...

Import

from llm_guard.input_scanners import BanCode

I/O Contract

Inputs

Name Type Required Description
model Model or None No The classification model to use. Defaults to vishnun/codenlbert-sm.
threshold float No Confidence threshold for code detection. Defaults to 0.97.
use_onnx bool No Whether to use ONNX runtime for inference. Defaults to False.

scan() Inputs

Name Type Required Description
prompt str Yes The input text to scan for code snippets.

Outputs

Name Type Description
prompt str The original prompt (unchanged).
is_valid bool True if no code was detected above the threshold; False otherwise.
risk_score float A confidence score between 0.0 and 1.0 indicating the likelihood of code presence.

Usage Examples

Basic Usage

from llm_guard.input_scanners import BanCode

scanner = BanCode()
prompt = "def hello_world():\n    print('Hello, World!')"
sanitized_prompt, is_valid, risk_score = scanner.scan(prompt)

print(is_valid)    # False (code detected)
print(risk_score)  # High confidence score

Custom Threshold

from llm_guard.input_scanners import BanCode

# Lower threshold for more aggressive code detection
scanner = BanCode(threshold=0.8)
prompt = "Please explain the sort algorithm"
sanitized_prompt, is_valid, risk_score = scanner.scan(prompt)

print(is_valid)    # True (natural language text)
print(risk_score)  # Low confidence score

ONNX Runtime

from llm_guard.input_scanners import BanCode

# Use ONNX for faster inference
scanner = BanCode(use_onnx=True)
prompt = "SELECT * FROM users WHERE id = 1;"
sanitized_prompt, is_valid, risk_score = scanner.scan(prompt)
print(is_valid)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment