structured-generation
Design and troubleshoot Outlines constrained-generation output types, generators, backends, and logits processors.
- License 许可
- Apache-2.0
- 最近更新
- 2026-09-03
- Stars
- ★ 351
- Forks
- 27
01 前言 Front Matter
- name
- structured-generation
- license
- Apache 2.0
- metadata
- { "disco-role": "operating" }
- description
- Design and troubleshoot Outlines constrained-generation output types, generators, backends, and logits processors.
- disable-model-invocation
- true
02 资源文件 5
- 参考文档references/api-reference.md5.2 KB
- 参考文档references/backends.md2.7 KB
- 参考文档references/troubleshooting.md4.3 KB
- 参考文档references/workflows.md4.4 KB
- 脚本scripts/validate_structure.py2.7 KB
03 SKILL.md 正文
Structured Generation
Use this sub-skill when the task is to make an Outlines model return a specific structure: JSON, a Pydantic/dataclass/TypedDict schema, a Literal/Enum/Choice, a regular expression, or a context-free grammar. It also covers the Generator contract, backend selection, raw-output parsing, batch/stream behavior, and custom logits processors.
Route model setup elsewhere:
- Local/offline engines, tokenizers, GPU/MPS, and model downloads ->
../local-models/SKILL.md. - Hosted/server providers and credentials ->
../remote-providers/SKILL.md. - Prompt templates,
Application,Chat, images, and workflow orchestration ->../prompt-workflows/SKILL.md.
Fast Route
-
Choose the output language. Use Python types for simple data,
Literal/Enum/Choicefor finite choices, a Pydantic/dataclass/TypedDict/GenSON/JsonSchemafor JSON,Regexfor a regular language, orCFGfor Lark-style grammars. -
Pick a compatible model route. Local steerable models compile output types into logits processors. Server/black-box providers support only the structured-output formats their SDK/server exposes.
-
Call directly or reuse a generator. A model call and a generator call are equivalent in shape:
response = model(prompt, OutputType, max_new_tokens=200) generator = outlines.Generator(model, OutputType) response = generator(prompt, max_new_tokens=200) -
Parse the raw string yourself. Outlines v1 returns text. For Pydantic schemas, call
Schema.model_validate_json(response)after generation. -
Set the backend only for steerable models. Defaults are JSON schema=
outlines_core, regex=outlines_core, CFG=llguidance. Override withbackend="llguidance"orbackend="xgrammar"only after checking support. -
Use a custom processor only with steerable models.
Generator(model, processor=processor)is mutually exclusive withoutput_type; server models raiseNotImplementedErrorfor processors. -
Validate before generation. Check a JSON schema/regex/grammar against representative samples before spending tokens or running a model.
Load These References
references/api-reference.md: constructor signatures, output-type conversion rules, direct model calls,Generator, batch/stream, and parsing contracts.references/backends.md: default and selectable backends, model compatibility, unsupported cases, and logits-processor flow.references/workflows.md: schema-first and regex/CFG iteration patterns, Pydantic parsing, and structured workflow recipes.references/troubleshooting.md: symptoms, causes, and recovery for output-type, backend, parsing, and processor failures.
Bundled Script
Use scripts/validate_structure.py for local, no-model validation of JSON samples against JSON Schema or text samples against a regex before you wire the structure into an Outlines call.
Examples:
python scripts/validate_structure.py json --schema '{"type":"object","required":["name"],"properties":{"name":{"type":"string"}}}' --data '{"name":"Ada"}'
python scripts/validate_structure.py regex --pattern '[0-9]{3}' --text '123'
The script does not import provider clients, download models, or call a network service.
Non-Negotiable Checks
- Never claim a provider supports regex/CFG just because a local model does. Check provider capability first.
- Never use
backend="outlines_core"for CFG; usellguidanceorxgrammarwith a compatible steerable model. - Never set
JsonSchema(..., whitespace_pattern=...)withllguidanceorxgrammar; useoutlines_coreif whitespace control is required. - Never execute model output as code while validating structure. Parse JSON, match regex, or validate with a schema.
- Never tell future agents to open original repository docs/examples/tests; this sub-skill and its bundled references/scripts are the runtime source.
04 同仓库更多 Skills
passnet-triton-opt
Make a single PassNet Triton kernel fast AND numerically faithful: performance model (when a replacement can win at all), block/grid/warp tuning, autotune policy, launch overhead, and per-op numeric recipes to pass the dtype baseline tolerances. Use when a pass matches and is correct but speedup below expected, or when correctness fails by small numeric margins.
passnet-solve
END-TO-END playbook for solving one PassNet sample: analyze the computation graphs, decide the optimization strategy (what to fuse, what to replace, what to leave alone), drive the iteration loop, and maximize the sample score. This is the ENTRY skill — invoke it first for any PassNet optimization task; it tells you when to use passnet-pattern-fusion, passnet-triton-opt and passnet-feedback.
passnet-skill
PassNet GPU kernel optimization via compiler passes. Design and implement Triton-based optimization passes, create pass files under ./pass_dir/, self-evaluate with pass_evaluator, and iterate to maximize GPU speedup.
passnet-pattern-fusion
Author PassNet pass files: write patterns that actually MATCH the FX graph, pick fusion regions (which ops to absorb into one Triton kernel), and structure multi-pass submissions with the mandatory shared-dispatch architecture. Use when creating or fixing pattern()/replacement_args()/replacement_func() files, when a pass "failed to match", or when deciding how to fuse multiple kernels/ops.
passnet-orchestrate
Round/batch planning and decision support for PassNet graph optimization. Use for multi-sample triage, worker allocation, eval-budget policy, and post-eval keep/revert decisions. For solving one concrete sample end to end, use passnet-solve as the entry skill; consult this skill only when you need broader planning or a specific decision gate.
passnet-feedback
Fast, deterministic feedback for PassNet work: pre-flight pattern/match verification WITHOUT burning a GPU evaluation, per-node bottleneck analysis of a sample's graphs, and eval-log parsing into per-variant status + estimated score + failure classification. Use BEFORE every GPU evaluation (check_pattern), at the START of a sample (analyze_graph), and AFTER every evaluation (parse_eval_log).