Your AI-Generated SVG Won't Render: The Same 8 Defects Every Time

Your AI-Generated SVG Won’t Render: The Same 8 Defects Every Time

You asked a model for an SVG. The code looks right — tags, paths, colors, all plausible. You paste it into a file, open it in a browser, and get… a blank rectangle. No error, no warning, nothing. If this happens to you regularly, it’s not bad luck. SVG is XML, and XML is unforgiving in a way HTML never is. A browser reading HTML will patch over a missing quote or an unclosed tag and render what it can. A browser reading SVG does none of that: one syntax slip and the whole document is rejected — usually silently.

你向模型索要 SVG。代码看起来没问题——标签、路径、颜色,一切都很合理。你将其粘贴到文件中,在浏览器中打开,结果却是一个空白矩形。没有错误,没有警告,什么都没有。如果这种情况经常发生在你身上,那不是运气不好。SVG 是 XML,而 XML 的严苛程度是 HTML 无法比拟的。浏览器读取 HTML 时会修补缺失的引号或未闭合的标签并尽可能渲染,但读取 SVG 时则完全不会:只要出现一个语法错误,整个文档就会被拒绝——通常是静默拒绝。

Language models generate SVG the way they generate everything else: token by token, plausibly. They pattern-match on millions of examples without running an XML validator or a geometry engine. So they fail on exactly the details a validator would have caught — and they fail in the same ways, over and over. Here are the eight defects behind almost every broken AI-generated SVG I’ve seen.

语言模型生成 SVG 的方式与生成其他内容一样:逐个 token 生成,看起来合理即可。它们在数百万个示例上进行模式匹配,而不会运行 XML 验证器或几何引擎。因此,它们恰恰在验证器本可以捕捉到的细节上出错,并且以同样的方式反复失败。以下是我见过的几乎所有损坏的 AI 生成 SVG 背后的八个缺陷。

The 8 defects

8 个缺陷

  1. Missing xmlns attribute. The #1 killer. Without xmlns=“http://www.w3.org/2000/svg” the browser treats the file as generic XML. It may render inline in an HTML page, then fail as a standalone file or inside an tag — which makes it look like a caching bug. Models forget it constantly.

  2. 缺失 xmlns 属性。这是头号杀手。如果没有 xmlns=“http://www.w3.org/2000/svg”,浏览器会将文件视为通用 XML。它可能在 HTML 页面中内联渲染,但在作为独立文件或在 标签内使用时会失败——这看起来像是一个缓存错误。模型经常忘记这一点。

  3. Missing or invented viewBox. No viewBox, and the browser has no idea which part of the coordinate system to show. Or the model writes viewBox=“0 0 24 24” and then draws at x=“150” — every shape lands off-canvas and you get an empty frame.

  4. 缺失或虚构的 viewBox。没有 viewBox,浏览器就不知道该显示坐标系的哪一部分。或者模型写了 viewBox=“0 0 24 24” 却在 x=“150” 处绘图——所有形状都落在了画布之外,你得到的是一个空框架。

  5. Shapes translated out of view. AI loves decorative transform=“translate(350, 280)” guesswork. The markup is valid, the preview is blank, and nothing tells you the artwork is sitting just outside the frame.

  6. 形状被平移到视野之外。AI 喜欢随意猜测并添加装饰性的 transform=“translate(350, 280)“。标记是有效的,预览是空白的,没有任何提示告诉你艺术品正位于框架之外。

  7. A raw & in a label. SVG is strict XML. A label like Tom & Jerry with a bare unescaped & invalidates the entire document. One character, whole file dead.

  8. 标签中包含原始的 & 符号。SVG 是严格的 XML。像 “Tom & Jerry” 这样带有未转义 & 符号的标签会使整个文档失效。一个字符,整个文件就报废了。

  9. NaN and Infinity coordinates. Models do arithmetic in public: d=“M 10 10 L NaN 50” or a radius of Infinity. The path is syntactically present and renders as nothing.

  10. NaN 和 Infinity 坐标。模型在生成时进行算术运算:d=“M 10 10 L NaN 50” 或半径为 Infinity。路径在语法上存在,但渲染出来什么都没有。

  11. Unclosed or misnested tags. Truncated output, a that never closes, an attribute missing its quote. XML parsers are all-or-nothing — one structural slip and nothing renders at all.

  12. 未闭合或嵌套错误的标签。输出被截断、 标签未闭合、属性缺少引号。XML 解析器要么全盘接受,要么完全拒绝——一个结构性错误就会导致无法渲染任何内容。

  13. Chat prose and code fences. “Sure! Here’s your logo:” … `svg … “Let me know if you’d like adjustments!” Paste that into a file and you have prose, fences, and markup layered together. Browsers don’t strip any of it for you.

  14. 聊天正文和代码块标记。例如:“好的!这是你的 Logo:” … `svg … “如果需要调整请告诉我!” 将其粘贴到文件中,你就会得到正文、代码块标记和标记语言混合在一起的内容。浏览器不会为你剔除这些多余信息。

  15. Everything fill=“none”. The model styles each shape with fill=“none” and forgets to add a stroke. The file parses, the shapes exist, the image is empty. This one fools people the most because the SVG technically “works”.

  16. 所有内容 fill=“none”。模型为每个形状设置了 fill=“none” 却忘记添加描边(stroke)。文件可以解析,形状也存在,但图像是空的。这一点最容易欺骗用户,因为从技术上讲,SVG 是“正常工作”的。

Defects 1–6 kill the file outright at parse time. 7–8 are sneakier: the file renders, it just renders empty — worse to debug because there’s no error message to search for.

缺陷 1–6 会在解析时直接导致文件失效。7–8 则更隐蔽:文件可以渲染,只是渲染出来是空的——这更难调试,因为没有错误信息可供搜索。

The 30-second diagnosis

30 秒诊断

Before fixing anything, figure out which defect you have: Open the file and search for xmlns. Missing? That’s defect 1. Check the viewBox against the largest coordinates in the file. Drawing at 400 but viewing 0–24? Defect 2 or 3. Look for NaN, Infinity, or a bare & outside entities. Defects 4 and 5. Text at the top of the file that isn’t markup? Defect 7. Everything has fill=“none” and no stroke? Defect 8.

在修复之前,先找出你遇到了哪个缺陷:打开文件并搜索 xmlns。缺失?那是缺陷 1。检查 viewBox 与文件中最大坐标的匹配情况。在 400 处绘图但视图范围是 0–24?那是缺陷 2 或 3。查找 NaN、Infinity 或实体之外的原始 & 符号。那是缺陷 4 和 5。文件顶部有非标记文本?那是缺陷 7。所有内容都有 fill=“none” 且没有描边?那是缺陷 8。

Or skip the manual pass: paste the raw chat output — prose and fences and all — into SVG Repair. It extracts the real markup, repairs the XML, adds the missing xmlns and viewBox, zeroes out NaN coordinates, expands the canvas to include off-screen shapes, makes invisible shapes visible, and shows you an itemized report of every defect next to a before/after preview. It actually renders the repaired file and checks it paints a non-empty image before claiming success. Everything runs client-side — the file never leaves your browser.

或者跳过手动检查:将原始聊天输出(包括正文和代码块标记)粘贴到 SVG Repair 中。它会提取真实的标记,修复 XML,添加缺失的 xmlns 和 viewBox,将 NaN 坐标归零,扩展画布以包含屏幕外的形状,使不可见的形状可见,并在预览对比图旁边显示每个缺陷的详细报告。它会实际渲染修复后的文件,并在确认绘制出非空图像后才算成功。所有操作都在客户端运行——文件永远不会离开你的浏览器。

Prevent it at the source

从源头预防

Repairing is faster than regenerating, but the cheapest fix is an SVG that isn’t broken. Four lines added to the prompt eliminate most defects: “Output only the raw SVG markup, no explanation, no code fences.” — kills defect 7 entirely. “Include xmlns and a viewBox that matches the coordinate range you draw in.” — kills defects 1 and 2. “Draw all shapes inside 0 0 512 512. Use no transforms.” — kills defect 3. “No

修复比重新生成更快,但最省事的修复方法是让 SVG 一开始就不损坏。在提示词中添加四行内容可以消除大多数缺陷:“仅输出原始 SVG 标记,不要解释,不要代码块标记。”——彻底消除缺陷 7。“包含 xmlns 和与你绘制的坐标范围相匹配的 viewBox。”——消除缺陷 1 和 2。“在 0 0 512 512 范围内绘制所有形状。不要使用变换。”——消除缺陷 3。“不要 <script>,不要 CSS 类,不要文本——将文本转换为路径。”——消除下文提到的意外情况。

One more thing AI SVGs smuggle in: scripts

AI SVG 夹带的另一件事:脚本

Models occasionally emit

模型偶尔会输出 <script> 标签、onload 处理程序或 foreignObject 块——有时是因为你要求动画,有时则是莫名其妙地出现。这不仅仅影响渲染:一旦有人直接打开或将其内联到页面中,带有脚本的 SVG 就会成为一个活跃的 XSS 攻击向量。如果你正在接收用户的 SVG,或将 AI 输出嵌入到任何公共场所,请在发布前剔除活动内容。上述修复流程默认会剔除这些内容。

When it renders again, keep going

渲染成功后,继续优化

A repaired file is a working file, not a finished one. AI output carries redundant precision and empty groups that a proper SVG optimizer typically shaves 30–60%. Whole icon sets can be repaired and converted in batch. And when the geometry is almost right, a free browser-based SVG editor lets you fix it visually — select and move shapes, edit text, adjust fills and strokes — without installing anything or uploading the file anywhere.

修复后的文件只是能用,还不是成品。AI 输出带有冗余的精度和空组,合适的 SVG 优化器通常可以将其压缩 30–60%。整套图标集可以批量修复和转换。当几何形状基本正确时,免费的基于浏览器的 SVG 编辑器可以让你进行可视化修复——选择并移动形状、编辑文本、调整填充和描边——无需安装任何东西或将文件上传到任何地方。