How qwenlm/qwen3guard works
1. This repository is primarily a model release and evaluation repo, not an application codebase. Its main purpose is to document Qwen3Guard, point users to model checkpoints, and provide scripts for benchmarking moderation behavior.
2. The core product boundary is the Qwen3Guard model family itself, released in two operational modes:
- Qwen3Guard-Gen: a generative moderation model that reads a prompt or prompt+response pair and emits a structured safety verdict.
- Qwen3Guard-Stream: a token-level streaming moderator that maintains state across incremental generation and classifies text in real time.
3. The repo’s documentation layer is centered in README.md and the attached technical report PDF. These define the safety policy, supported labels, example inputs/outputs, and the intended deployment patterns. The README is the main architecture reference for using the models correctly.
4. The generative moderation flow is:
user prompt or user prompt + assistant response → Qwen chat template → transformer inference → structured text output → regex-like parsing by the caller into Safety, Categories, and optionally Refusal.
This makes the output format part of the interface contract, not just free-form generation.
5. The streaming moderation flow is different:
tokenized conversation → initial moderation on the user turn → repeated token-by-token calls with maintained stream_state → per-token risk/category updates → stream closure.
This design is stateful and intended to sit alongside an LLM decoder, not replace it.
6. The repo clearly separates offline/local inference from serving deployment:
- Local inference uses transformers.
- Deployment for Qwen3Guard-Gen is shown through vLLM or SGLang as OpenAI-compatible endpoints.
- The README notes that streaming support in those serving stacks is still future work for Qwen3Guard-Stream.
7. The eval/ directory is the benchmarking subsystem. It contains:
- eval_gen.py for evaluating generative moderation,
- eval_stream.py for evaluating streaming moderation,
- eval/README.md for benchmark-specific instructions.
This separates evaluation logic from model usage examples.
8. Evaluation appears to be organized around the released Qwen3GuardTest benchmark, which focuses on moderation in streaming responses and reasoning-model outputs. The repo thus has a clear boundary between model inference and benchmark scoring.
9. The model’s safety taxonomy is part of the architecture because it drives both inference outputs and evaluation labels. It uses three severity levels:
Safe, Controversial, and Unsafe,
plus content categories such as Violent, PII, Suicide & Self-Harm, Copyright Violation, and Jailbreak for input-only detection.
10. The external distribution layer is important: checkpoints and datasets are hosted on Hugging Face and ModelScope. The repo itself does not store model weights; it acts as a pointer to those artifacts and as the documentation/evaluation entry point.
11. In architectural terms, the repo is a thin control plane around hosted model artifacts:
- documentation and policy definition,
- example inference flows,
- serving guidance,
- evaluation scripts.
The actual learned moderation capability lives in the remote checkpoints, while this repo defines how to invoke and assess them.
Main entry points: README.md README · eval_stream.py eval_stream.py · eval/README.md Eval guide · eval_gen.py eval_gen.py
Components 18
Documentation
Repo-facing policy, usage, and release guidance
- README · Main architecture and usage reference for Qwen3Guard
README.md - Tech report · Technical report covering the model design, policy, and release details
Qwen3Guard_Technical_Report.pdf - Safety policy · Severity levels and content categories used by the moderation system
README.md - Artifact links · Pointers to hosted checkpoints and datasets on external model hubs
README.md
Model Family
Released moderation checkpoints and runtime modes
- Qwen3Guard · Hosted moderation checkpoints and associated runtime interfaces
- Qwen3Guard-Gen · Generates structured safety verdicts from prompts or prompt-response pairs
- Qwen3Guard-Stream · Token-level moderator that maintains state across incremental generation
- Chat template · Formats inputs for generative inference before model execution
- Gen inference · Local inference path for Qwen3Guard-Gen using transformer execution
- Verdict parse · Interprets generated text into Safety, Categories, and optional Refusal fields
- Stream state · Persistent moderation state carried across token-by-token updates
- Token update · Per-token risk and category updates during active generation
- vLLM serving · Deployment path for Qwen3Guard-Gen via OpenAI-compatible serving
- SGLang serving · Alternative deployment path for Qwen3Guard-Gen via OpenAI-compatible serving
Evaluation
Benchmarking scripts and scoring entry points
- Eval guide · Instructions and conventions for running benchmarks in eval/
eval/README.md - eval_gen.py · Evaluation script for generative moderation behavior
eval/eval_gen.py - eval_stream.py · Evaluation script for streaming moderation behavior
eval/eval_stream.py - Qwen3GuardTest · Released benchmark focused on streaming responses and reasoning outputs
Connections 20
- README → Safety policy: defines · Documents the moderation labels, severity levels, and categories
- README → Artifact links: points to · Links users to external checkpoints and datasets
- README → Qwen3Guard-Gen: describes · Explains the generative moderation workflow and expected outputs
- README → Qwen3Guard-Stream: describes · Explains the stateful streaming moderation workflow
- Tech report → Qwen3Guard: covers · Technical background for the released model family
- Artifact links → Qwen3Guard: hosts · External artifact locations for the checkpoints
- Qwen3Guard → Qwen3Guard-Gen: includes · Generative moderation as one operational mode
- Qwen3Guard → Qwen3Guard-Stream: includes · Streaming moderation as one operational mode
- Qwen3Guard-Gen → Chat template: uses · Inputs are formatted with the Qwen chat template before inference
- Chat template → Gen inference: feeds · Prepared prompts are passed into local transformer inference
- Gen inference → Verdict parse: emits · Structured text is parsed into moderation fields
- Qwen3Guard-Stream → Stream state: maintains · Carries state across incremental calls
- Stream state → Token update: updates · State is refined by token-by-token moderation events
- Qwen3Guard-Gen → vLLM serving: deploys via · Shown as an OpenAI-compatible serving option
- Qwen3Guard-Gen → SGLang serving: deploys via · Shown as an OpenAI-compatible serving option
- Eval guide → eval_gen.py: guides · Benchmark instructions for generative evaluation
- Eval guide → eval_stream.py: guides · Benchmark instructions for streaming evaluation
- eval_gen.py → Qwen3Guard-Gen: scores · Measures generative moderation behavior
- eval_stream.py → Qwen3Guard-Stream: scores · Measures streaming moderation behavior
- eval_stream.py → Qwen3GuardTest: uses · Benchmark target for streaming and reasoning-response cases