Research guide · English preview

Sanitize the prompt. Keep the creative intent.

PromptSan is not another image model. It is a classifier-guided safety layer that tries to move an unsafe prompt into a safer embedding region before Stable Diffusion generates anything.

arXiv:2506.18325 Stable Diffusion v1.4 Published June 23, 2025
PRE-GENERATION SAFETY LAYER
ACTIVE
01 · User prompt String

"A cinematic portrait containing a high-risk concept…"

Text NSFW classifier

CLIP embedding → category risk

0.91

Modify

Optimize high-impact token embeddings at inference.

Suffix

Append a pre-trained safety embedding for the risk category.

Sanitized embedding

Re-check before generation

PASS

2

Sanitization variants

1.1M

Classifier parameters

20

Learned suffix tokens

10

Modify update steps

01 · The core idea

A third option between “allow” and “block”

Most prompt filters make a binary decision. PromptSan asks a different question: can the system remove the unsafe direction while retaining the subject, composition, style, lighting, and other benign instructions?

Conventional gate

unsafe → reject

Simple to reason about, but it discards the entire request—even when much of the creative direction is harmless.

PromptSan

unsafe → sanitize → generate

Changes selected text embeddings or appends a learned safety suffix, then lets the same frozen image model continue.

“Preserve intent” is an optimization goal, not a guarantee. The paper measures safety reduction and benign-content preservation, but a production system still needs policy checks before and after generation.

02 · How it works

One classifier, two intervention paths

Both variants start with a CLIP-derived text representation and a small category-specific NSFW classifier. Safe inputs pass through unchanged. Unsafe inputs are routed to either inference-time token repair or a pre-trained suffix.

Official PromptSan framework showing the Modify and Suffix paths around Stable Diffusion
Official PromptSan framework, Figure 1. Source: Xie et al., arXiv:2506.18325v1. The image is reproduced here for research commentary and attribution.

03 · PromptSan-Modify

Repair only the tokens driving the risk

Inference-time

Modify computes how sensitive the classifier loss is to every token embedding. The highest-impact tokens receive gradient updates toward the classifier’s safe region; the rest of the prompt stays fixed.

01 Encode Convert the prompt into token-level CLIP embeddings.
02 Score Run the category classifier and stop immediately if risk is below the safety threshold.
03 Locate Rank tokens by the infinity norm of their safety-loss gradient and select the top-p set.
04 Optimize Mask every other gradient, update only selected embeddings, and re-check the classifier.
Conceptual update rule
risk = classifier(prompt_embeddings)
token_scores = norm(∂ safety_loss / ∂ token_embedding)
selected = top_p(token_scores, p=0.1)

for step in range(10):
    prompt_embeddings[selected] -= 0.03 * gradient[selected]
    if classifier(prompt_embeddings) < threshold:
        break

0.03

AdamW learning rate

10

Inference updates

0.1

Top-p threshold

04 · PromptSan-Suffix

Train once, append at runtime

20 soft tokens

Suffix learns a short sequence of continuous embeddings for a harmful category. Training uses both image and text classifiers; inference first classifies the prompt, then attaches the corresponding suffix without running ten prompt-optimization steps.

Human-readable concept

original prompt
+
<SAFE_01> … <SAFE_20>

The token names are illustrative. The useful object is the learned embedding sequence, not readable safety wording.

Why it is product-friendly

  • No per-request gradient loop after the suffix has been trained.
  • Category-specific suffixes can be selected by the same lightweight classifier.
  • The base diffusion model remains frozen.
Official t-SNE plots comparing original prompt embeddings with PromptSan suffix embeddings across eight safety categories
Paper Figure 3. The orange suffix-conditioned embeddings move away from the blue originals in the 2D projection. This visualizes a representation shift; it does not by itself prove safety.

05 · The classifier

Small enough to sit in front of generation

The appendix describes a roughly 1.1-million-parameter binary network. It consumes a 768-dimensional CLIP feature and outputs a risk probability. Rather than one universal classifier, the authors train separate detectors for categories such as nudity, violence, and disturbing content using the VISU dataset.

CLIP feature

768 dimensions

Safety network

≈ 1.1M parameters

Category score

Probability 0–1

06 · Results and limits

The reported reduction is large. The scope is narrow.

On the I2P benchmark with Stable Diffusion v1.4, the paper reports the following NudeNet detection totals. These are detected exposed-region counts—not the number of prompts, images, or policy violations.

SD v1.4 baseline

659

PromptSan-Modify

43

93.5% fewer detections

PromptSan-Suffix

38

94.2% fewer detections

Official paper comparison of benign animal, food, furniture, and transport preservation across safety methods
Paper Figure 6 compares benign-content preservation across methods. The authors report similar FID and CLIP behavior on COCO30K, rather than a clear quality improvement.

What the paper does not establish

The main benchmark uses Stable Diffusion v1.4, not modern hosted image APIs or every current model family.
The authors explicitly report weaker mitigation for violence than nudity and possible bypasses for composite harmful concepts.
A NudeNet count is one detector's measurement; it is not a complete policy or human-safety evaluation.
The arXiv record does not link an author-maintained PromptSan repository, so reproduction requires engineering work.

07 · Runnable alternatives

Use PromptSan as a design pattern, not a package name

If the goal is a working prototype rather than a paper reproduction, these official repositories cover adjacent parts of the same safety stack.

ProjectBest fitSafety layerPublished support
PromptGuard Closest to a reusable soft-suffix approachPrompt embeddingSD v1.4 example
SAFREE Training-free removal of unsafe concept directionsEmbedding + generationSD 1.4, SDXL, video code
Safe Latent Diffusion Mature denoising-time safety guidanceDiffusion processSafe SD pipeline
LatentGuard Fast prompt detection, not prompt repairInput classifierSD/SDXL text encoders

08 · Production blueprint

A gateway should fail in layers

Prompt sanitization is best treated as one recoverable step inside a larger control plane. Detection decides what to repair; output moderation catches what the prompt layer misses.

01

Text policy

Hard rules and obvious abuse

02

Risk model

Category + calibrated score

03

Sanitizer

Embedding repair or rewrite

04

Generator

Local model or image API

05

Image check

Return, retry, or reject

When you control the model

Diffusers, Stable Diffusion, SDXL, or a local workflow can expose prompt embeddings. That makes PromptSan-style gradient repair, soft suffixes, and SAFREE-like projection technically possible.

Build a local ComfyUI workflow

When the API accepts only text

You cannot pass a modified CLIP embedding into most hosted APIs. Use classifier → constrained rewrite → second classifier → generation → image classifier, and measure semantic similarity as a guardrail.

Explore prompt structure examples
A useful gateway response
{
  "safe": false,
  "risk": 0.91,
  "categories": { "sexual": 0.91, "violence": 0.04 },
  "action": "sanitized",
  "sanitizedPrompt": "…",
  "semanticSimilarity": 0.93,
  "outputCheck": "pending"
}

Recommended MVP

Classify → rewrite → verify → generate → inspect

Start with measurable layers that work across model providers. Add learned suffixes or embedding-space repair only after you have category data, failure examples, thresholds, and a review loop.

Quick answers

PromptSan FAQ

Is PromptSan a new image generation model?

No. It is a prompt-level safety intervention tested on Stable Diffusion v1.4. The base image model stays frozen while the prompt embedding or a learned suffix is changed.

Can PromptSan be added to a closed image API?

Not in its original embedding-based form if the API accepts only a prompt string. A closed API usually needs a classifier, a text rewrite step, a second classifier pass, and an output image checker instead.

Does PromptSan replace output moderation?

No. The paper reports weaker behavior for some concepts and possible bypasses for composite harmful concepts. A production system should still inspect generated images and keep policy enforcement outside the model.

Is there an official PromptSan package?

The paper's arXiv record does not link an author-maintained code repository as of August 27, 2026. PromptGuard, SAFREE, Safe Latent Diffusion, and LatentGuard are useful open-source references for adjacent layers.

References

Primary paper and official implementations

  1. Xie, Zeng, Zhang & Fu — NSFW-Classifier Guided Prompt Sanitization for Safe Text-to-Image Generation · arXiv:2506.18325v1, June 23, 2025.
  2. PromptGuard official repository · Closest to a reusable soft-suffix approach.
  3. SAFREE official repository · Training-free removal of unsafe concept directions.
  4. Safe Latent Diffusion official repository · Mature denoising-time safety guidance.
  5. LatentGuard official repository · Fast prompt detection, not prompt repair.

Last fact-checked August 27, 2026. This guide is an independent technical interpretation and is not affiliated with the paper authors or the referenced projects.