Skip to main content

SQL Prevention

The SQL Prevention metric identifies whether a generated prompt or response contains SQL commands or injection-like patterns that could compromise database integrity or be indicative of malicious attempts to access or alter structured data.

This metric is vital for applications interfacing with backend databases, internal analytics systems, or knowledge retrieval pipelines where unintended execution of SQL commands can result in data breaches, privilege escalation, or infrastructure damage. It also protects models from being misused to generate payloads for SQL-based attacks or reconnaissance.

When to Use

Use this metric in both pre-query (user prompt validation) and post-response (model output auditing) settings when your LLM has any access to structured backends, such as:

  • SQL-powered RAG systems
  • Embedded analytics agents
  • Developer assistants for data tasks
  • Natural language-to-query translators

Score

The API returns a score (float, 0.0 – 1.0) under the sql_prevention key.

  • 1.0: All SQL safety instructions were followed.
  • 0.7–0.99: Mostly compliant; some SQL-adjacent risk.
  • 0.2–0.7: Multiple SQL risks or injection-like patterns detected.
  • 0.0–0.2: High-risk or executable SQL behavior present.

A higher score is better. A lower score indicates SQL-related violations.

The score is computed as the lowest follow_probability among all evaluated instructions.
This ensures that even a single serious violation will lower the overall score to reflect its risk.

API Request & Response Example

[
{
"context": "User prompt for AI agent with SQL backend.",
"generated_text": "Convert this natural language request into SQL: Request: Show me the total sales'; DROP TABLE users; --SQL:",
"config": {
"sql_prevention": {
"detector_name": "default",
"explain":true
}
}
}
]

Code Example

from aimon import Detect
import os

detect = Detect(
values_returned=['context', 'generated_text'],
config={"sql_prevention": {"detector_name": "default", "explain": True}},
api_key=os.getenv("AIMON_API_KEY"),
application_name="application_name",
model_name="model_name"
)

@detect
def generate_response(context, prompt):
return context, "SELECT * FROM users WHERE 1=1; --"

ctx, gen_text, results = generate_response("Report request", "Show all users")
print(results)