nideshkaarthikrs/promptshield
PromptShield - Prompt Injection Detector
PromptShield is a real-world OpenEnv environment that trains and evaluates AI agents on detecting prompt injection attacks — the #1 LLM security threat per OWASP 2024.
This environment places an AI agent in the role of a security analyst reviewing prompts submitted to an LLM system. The agent must classify each prompt as injection or safe, explain its reasoning, and optionally suggest a sanitized version.
PromptShield fills a genuine gap in the OpenEnv ecosystem: no existing environment covers LLM security evaluation. As AI assistants get embedded into enterprise workflows, detecting prompt injection is critical for building safe production systems.
Observation Space
Each step, the agent receives a structured observation:
Action Space
Each action is a structured JSON response:
Tasks
Task 1 — Direct Injection Detection (Easy)
30 prompts: obvious injection patterns (ignore all previous instructions, disregard your system prompt, etc.) mixed with clearly legitimate user requests. Designed to test basic injection recognition.
Task 2 — Indirect & Role Hijacking (Medium)
40 prompts: indirect injections hidden inside documents/JSON/emails, DAN-style role hijacking, plus tricky safe prompts like legitimate roleplay requests and dark creative writing that is NOT injection.
Task 3 — Adversarial Edge Cases (Hard)
40 prompts: base64/Unicode/hex encoded injections, multi-language attacks (Hindi, Spanish, Japanese, Russian), few-shot attack patterns, and adversarially-designed safe prompts that use words like ignore and override in legitimate technical contexts.
Reward Function
Rewards are shaped at every step — the agent gets feedback continuously, not just at episode end.
Episode score = sum(step_rewards) / (max_steps × 1.0), clamped to [0.0, 1.0].
Baseline Scores
Measured using Qwen/Qwen2.5-72B-Instruct via HuggingFace Router:
All 3 tasks pass their success thresholds with the baseline model.
Setup & Usage
Local Python
git clone <repo-url>
cd promptshield
pip install -r server/requirements.txt
# Start server
python server/app.py
# In another terminal, test endpoints:
curl -X POST http://localhost:7860/reset -H 'Content-Type: application/json' -d '{"task_name": "task_easy"}'
curl http://localhost:7860/healthDocker
docker build -f server/Dockerfile -t promptshield .
docker run -p 7860:7860 promptshield
# Verify
curl -X POST http://localhost:7860/reset -H 'Content-Type: application/json' -d '{}'Running the Baseline Agent
export HF_TOKEN=your_huggingface_token
export MODEL_NAME=Qwen/Qwen2.5-72B-Instruct
export SERVER_URL=http://localhost:7860
python inference.pyAPI Endpoints
Project Structure
promptshield/
├── promptshield/
│ ├── env.py # Core OpenEnv environment class
│ ├── models.py # Pydantic models for Observation/Action/Reward/State
│ ├── grader.py # Deterministic grader
│ ├── dataset.py # Dataset loader
│ └── tasks/
│ ├── easy.json # 30 prompts (20 injection, 10 safe)
│ ├── medium.json # 40 prompts (25 injection, 15 safe)
│ └── hard.json # 40 prompts (30 injection, 10 adversarial safe)
├── server/
│ ├── app.py # FastAPI server
│ ├── Dockerfile # Production Docker container
│ └── requirements.txt
├── inference.py # Baseline LLM agent script
├── openenv.yaml # OpenEnv spec metadata
└── README.mdAuthor
Nidesh Kaarthik R S | Scaler School of Technology | Meta PyTorch OpenEnv Hackathon 2025
