Urgent.News

What's breaking now, across thousands of outlets.

AI

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…

When you ask an AI model to generate an SVG file, the resulting code often appears correct at first glance. The tags, paths, and colors seem plausible. However, when you open the file in a browser and see only a blank rectangle, it's not a sign of bad luck. SVG is an XML-based format, and XML is very strict about syntax. If there's a single syntax error, the entire document is rejected, usually silently.

AI models generate SVG code by tokenizing and matching patterns to millions of examples, without running an XML validator or geometry engine. Consequently, they frequently fail on the same details that a validator would catch.

Here are the eight common defects that lead to AI-generated SVG files failing to render:

1. Missing xmlns attribute: This is the most common mistake. Without the xmlns= "http://www.w3.org/2000/svg" attribute, the browser treats the file as generic XML. It may render the SVG inline in an HTML page, but it will fail when opened as a standalone file or in an img tag.

2. Missing or invented viewBox: If there's no viewBox specified, the browser doesn't know which part of the coordinate system to display. Alternatively, the model might write viewBox="0 0 24 24" but then draw shapes at positions like x=150, causing every shape to be off-canvas and resulting in an empty frame.

3. Shapes translated out of view: AI often guesses decorative transformations like transform="translate(350, 280)". The markup is valid, but the preview is blank, and nothing indicates that the artwork is sitting just outside the frame.

4. A raw '&' in a label: SVG is a strict XML format. Any unescaped '&' character, like in the label "Tom & Jerry", invalidates the entire document. One character, and the file is dead.

5. NaN and Infinity coordinates: Models perform arithmetic operations, which can result in NaN (Not a Number) or Infinity values for coordinates. When used in the SVG path data (d= attribute), these values make the path syntactically present but render as nothing.

6. Unclosed or misnested tags: If the SVG markup contains truncated or improperly nested tags, the XML parser will reject the entire file. Even a single structural slip is enough for no rendering to occur.

7. Chat prose and code fences: If the AI model includes prose, code fences, or mixes markup with plain text (e.g., pasting the raw chat output), the browser will interpret it as part of the SVG document. This can lead to rendering issues or syntax errors.

8. Everything fill= none: If every shape in the SVG file is assigned fill= "none", the file technically parses, but the shapes are empty, creating an empty image.

The first six defects prevent the SVG file from rendering at all, whereas the last two defects create an empty image. After diagnosing the issue, you can use tools like SVG Repair to fix the file. The repair process involves adding missing attributes, correcting coordinate values, removing invalid characters, and optimizing the SVG code. Once the file is repaired, you can further optimize it using an SVG optimizer to remove redundant precision and empty groups.

To avoid these issues, include the following four lines in your AI-generated SVG prompt:

- Output only the raw SVG markup, no explanation or code fences.

- Include xmlns and a viewBox that matches the coordinate range you draw in.

- Draw all shapes inside 0 0 512 512. Use no transforms.

- Convert any text to paths and remove any script tags or CSS classes.

Additionally, when accepting SVG files from users or embedding AI-generated SVGs in public-facing applications, make sure to strip any active content, such as script tags or onload handlers, to prevent XSS vulnerabilities. After repairing the SVG file, it's essential to optimize it further using an SVG optimizer to ensure the final output is clean, efficient, and ready for deployment.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in AI

More from Monday 2 November →