Identify outputs that attempt to override system or task instructions.
Full problem description visible. Upgrade to unlock the editor, test cases, and solution.
Implement detect_injection(output) that checks model output for prompt injection attempts.
The function receives a string and returns a dict:
{"injected": bool, "reason": str}.
Look for patterns that indicate the model output is attempting to:
- Override previous instructions ("ignore previous instructions", "forget your instructions").
- Leak or reference system prompt content ("system prompt", "you are now...").
- Hijack the task ("act as if", "you are a free...").
Keep detection conservative — only flag clear injection attempts. Return
{"injected": False, "reason": "No injection detected"} for safe output.
₹999/monthLimited period launch pricing.
Get the full problem statement, test cases, interactive editor, solution explanation, and visual diagram.
_INJECTION_PATTERNS = [
"ignore previous instructions",
"ignore all previous",
"forget your instructions",
"you are now",
"act as if",
"you are a free",
"system prompt",
"you have been",
]
def detect_injection(output: str) -> dict:
lower = output.lower()Check output for injection patterns
Already have Pro access? Sign in