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 Output Code

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

Overview

Code is an output scanner that detects specific programming languages in LLM responses by delegating to the input-side InputCode scanner.

Description

The Code output scanner is a thin wrapper around the corresponding input scanner InputCode. It classifies text to determine whether it contains code written in any of the specified programming languages. The scanner accepts a list of language names and uses a classification model to detect their presence. The is_blocked parameter controls the behavior: when True, the scanner flags outputs containing the specified languages as invalid; when False, it flags outputs that do not contain the specified languages (useful for enforcing that code must be present). The threshold parameter controls the minimum classification confidence required.

Usage

Use this scanner when you need fine-grained control over which programming languages appear in LLM outputs. Unlike BanCode which blocks all code, this scanner allows you to target specific languages. Use with is_blocked=True to block certain languages (e.g., preventing shell scripts in responses) or with is_blocked=False to ensure outputs contain code in a required language (e.g., ensuring Python-only responses in a Python tutorial).

Code Reference

Source Location

Signature

class Code(Scanner):
    def __init__(
        self,
        languages: list[str],
        *,
        model: Model | None = None,
        is_blocked: bool = True,
        threshold: float = 0.5,
        use_onnx: bool = False,
    ) -> None: ...

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

Import

from llm_guard.output_scanners import Code

I/O Contract

Inputs

Name Type Required Description
prompt str Yes The input prompt
output str Yes The LLM output to scan for code in specified languages

Constructor Parameters

Name Type Required Default Description
languages list[str] Yes N/A List of programming language names to detect
model None No None Custom classification model for code detection
is_blocked bool No True If True, block outputs with these languages; if False, require these languages
threshold float No 0.5 Minimum classification confidence for language detection
use_onnx bool No False Whether to use ONNX runtime for inference

Outputs

Name Type Description
sanitized_output str The output (potentially modified)
is_valid bool Whether the output passed the scan
risk_score float Risk score (-1.0 to 1.0)

Usage Examples

Basic Usage

from llm_guard.output_scanners import Code

# Block shell scripts and SQL in outputs
scanner = Code(
    languages=["bash", "sql"],
    is_blocked=True,
    threshold=0.5,
)

prompt = "How do I list files?"
output = "You can use: ls -la /home/user"

sanitized_output, is_valid, risk_score = scanner.scan(prompt, output)

if not is_valid:
    print(f"Blocked language detected (risk: {risk_score})")

Require Specific Language

from llm_guard.output_scanners import Code

# Ensure output contains Python code
scanner = Code(
    languages=["python"],
    is_blocked=False,
    threshold=0.5,
)

prompt = "Write a function to add two numbers"
output = "def add(a, b):\n    return a + b"

sanitized_output, is_valid, risk_score = scanner.scan(prompt, output)
# is_valid will be True because Python code is present

Related Pages

Page Connections

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