skills/llm/svg-constrained-generation/SKILL.md
Prompts an LLM to generate valid SVG by embedding an explicit element/attribute allowlist and a one-shot example, then extracts the last valid SVG block from output.
npx skillsauth add wenmin-wu/ds-skills llm-svg-constrained-generationInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
LLMs can generate SVG code, but without constraints they produce invalid elements, inline styles, scripts, or malformed paths. This technique embeds the exact allowed elements and attributes directly in the prompt as a constraint block, provides one concrete example, and ends the prompt mid-tag so the model continues inside a valid SVG root. After generation, regex extracts the last <svg>...</svg> block. This pattern generalizes to any structured markup where you need to enforce a schema via prompting.
import re
PROMPT = """Generate SVG code for the following description.
<constraints>
* Allowed elements: svg, path, circle, rect, ellipse, line, polygon, polyline, g
* Allowed attributes: viewBox, width, height, d, cx, cy, r, x, y, fill, stroke, stroke-width, points, transform
* No text, no style tags, no scripts
</constraints>
<example>
Description: "A red circle"
```svg
<svg viewBox="0 0 256 256" width="256" height="256">
<circle cx="128" cy="128" r="80" fill="red"/>
</svg>
</example>
Description: "{description}"
<svg viewBox="0 0 256 256" width="256" height="256">
"""
def extract_svg(output):
matches = re.findall(r'<svg.*?</svg>', output, re.DOTALL | re.IGNORECASE)
return matches[-1] if matches else DEFAULT_SVG
svg = extract_svg(model.generate(PROMPT.format(description=desc)))
<svg>...</svg> match via regexdata-ai
Scaled Pinball Loss (SPL) metric for evaluating quantile forecasts, normalized by mean absolute successive differences of training data
data-ai
Walk backward through a time series and multiplicatively rescale segments when jumps exceed a fraction of the running mean to correct data collection anomalies
testing
Transform forecasting target to next/current ratio minus one so that optimizing MAE or squared error implicitly minimizes SMAPE
tools
Convert point forecasts to prediction intervals by scaling with logit-transformed quantile ratios passed through a Normal CDF