fix(design): keep icon generator from corrupting SVGs or truncating output (#526)

* fix(design): keep icon generator from corrupting SVGs or truncating output

apply_viewbox_size rewrote every width/height attribute in the document,
including stroke-width and child elements, and only handled double quotes.
Restrict it to the root <svg> tag and set width and height independently.

generate_icon capped output at 4096 tokens, which truncates detailed icons.
Raise it to 16384 and report finish_reason MAX_TOKENS separately from
empty or malformed output.

Fixes #525

* fix(design): apply the response guard and MAX_TOKENS diagnosis to icon batch

generate_batch still indexed candidates[0].content.parts unguarded and
printed a bare "No valid SVGs generated." Share the response-text
extraction and the no-SVG diagnosis with generate_icon, treat text=None
as an empty response, and warn when a batch is cut off at MAX_TOKENS
after returning fewer icons than requested.

Refs #525

* fix(design): match only real root attributes when sizing icon SVGs

_set_root_attr searched the root tag with a regex, so width= or height=
text inside another quoted attribute value (aria-label="height='2'") was
rewritten and the output stopped being valid XML. Walk the tag's
attributes instead, consuming each quoted value whole, and only replace
an attribute whose name is exactly width or height.

Refs #525
This commit is contained in:
Younes Beriane 2026-10-12 07:05:52 +02:00 committed by GitHub
parent 67803eac81
commit 026b6e172c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 416 additions and 44 deletions

View File

@ -174,18 +174,68 @@ def apply_color(svg_code, color):
return svg_code
# Root <svg ...> opening tag; quoted attribute values may contain ">".
_SVG_ROOT_TAG = re.compile(r"""<svg\b(?:[^>"']|"[^"]*"|'[^']*')*>""")
# One token of an opening tag: name[=value], or a stray quoted string.
# Quoted values are consumed whole, so text inside them is never a name.
_TAG_ATTR = re.compile(
r"""([^\s=/>"']+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>"']+))?|"[^"]*"|'[^']*'""")
def _set_root_attr(tag, attr, value):
"""Set attr on a single <svg ...> opening tag, whatever quote style it uses."""
for m in _TAG_ATTR.finditer(tag, len('<svg')):
if m.group(1) == attr:
return tag[:m.start()] + f'{attr}="{value}"' + tag[m.end():]
return tag.replace('<svg', f'<svg {attr}="{value}"', 1)
def apply_viewbox_size(svg_code, size):
"""Adjust SVG viewBox to target size"""
"""Set width/height on the root <svg> tag only, leaving the viewBox alone"""
if size:
# Update width/height attributes if present
svg_code = re.sub(r'width="[^"]*"', f'width="{size}"', svg_code)
svg_code = re.sub(r'height="[^"]*"', f'height="{size}"', svg_code)
# Add width/height if not present
if 'width=' not in svg_code:
svg_code = svg_code.replace('<svg', f'<svg width="{size}" height="{size}"', 1)
match = _SVG_ROOT_TAG.search(svg_code)
if match:
tag = _set_root_attr(match.group(0), 'width', size)
tag = _set_root_attr(tag, 'height', size)
svg_code = svg_code[:match.start()] + tag + svg_code[match.end():]
return svg_code
def _finish_reason(response):
"""Return the first candidate's finish reason as a string ('' if unknown)"""
try:
reason = response.candidates[0].finish_reason
except (AttributeError, IndexError, TypeError):
return ""
return str(getattr(reason, "name", reason) or "")
def _response_text(response):
"""Collect the text of a response; '' when it has none (blocked, empty)"""
text = getattr(response, 'text', None) or ""
if not text:
candidates = response.candidates or []
content = candidates[0].content if candidates else None
for part in (content.parts or []) if content else []:
if getattr(part, 'text', None):
text += part.text
return text
def _report_no_svg(response, response_text, what="SVG"):
"""Say why no SVG came back: truncated, empty, or malformed output"""
if "MAX_TOKENS" in _finish_reason(response):
print("Error: output hit the token limit (finish_reason MAX_TOKENS) "
"before the SVG was complete. Try a simpler prompt or style.")
elif not response_text.strip():
print("Error: model returned an empty response.")
else:
print(f"No valid {what} generated. Model response:")
print(response_text[:500])
def generate_icon(prompt, style=None, category=None, name=None,
color=None, size=24, output_path=None, viewbox=24):
"""Generate a single SVG icon using Gemini 3.1 Pro Preview"""
@ -237,22 +287,17 @@ def generate_icon(prompt, style=None, category=None, name=None,
contents=full_prompt,
config=types.GenerateContentConfig(
temperature=0.7,
max_output_tokens=4096,
max_output_tokens=16384,
)
)
# Extract SVG from response
response_text = response.text if hasattr(response, 'text') else ""
if not response_text:
for part in response.candidates[0].content.parts:
if hasattr(part, 'text') and part.text:
response_text += part.text
response_text = _response_text(response)
svgs = extract_svgs(response_text)
if not svgs:
print("No valid SVG generated. Model response:")
print(response_text[:500])
_report_no_svg(response, response_text)
return None
svg_code = svgs[0]
@ -329,17 +374,12 @@ def generate_batch(prompt, count, output_dir, style=None, color=None,
)
)
response_text = response.text if hasattr(response, 'text') else ""
if not response_text:
for part in response.candidates[0].content.parts:
if hasattr(part, 'text') and part.text:
response_text += part.text
response_text = _response_text(response)
svgs = extract_svgs(response_text)
if not svgs:
print("No valid SVGs generated.")
print(response_text[:500])
_report_no_svg(response, response_text, "SVGs")
return []
results = []
@ -357,6 +397,10 @@ def generate_batch(prompt, count, output_dir, style=None, color=None,
results.append(filepath)
print(f" [{i+1}/{len(svgs[:count])}] Saved: {filename}")
if len(results) < count and "MAX_TOKENS" in _finish_reason(response):
print("Warning: output hit the token limit (finish_reason MAX_TOKENS); "
"the last icon was cut off. Try fewer variants or a simpler prompt.")
print(f"\n{'='*60}")
print(f" BATCH COMPLETE: {len(results)}/{count} icons generated")
print(f"{'='*60}\n")

View File

@ -0,0 +1,142 @@
"""Offline regression tests for icon/generate.py (no network, mocked genai)."""
import importlib.util
import sys
import types as pytypes
import xml.etree.ElementTree as ET
from pathlib import Path
from unittest import mock
import pytest
SCRIPT = Path(__file__).resolve().parent.parent / "icon" / "generate.py"
class _Config:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
@pytest.fixture
def gen(monkeypatch):
google = pytypes.ModuleType("google")
genai = pytypes.ModuleType("google.genai")
types = pytypes.ModuleType("google.genai.types")
types.GenerateContentConfig = _Config
genai.Client = mock.MagicMock()
genai.types = types
google.genai = genai
monkeypatch.setitem(sys.modules, "google", google)
monkeypatch.setitem(sys.modules, "google.genai", genai)
monkeypatch.setitem(sys.modules, "google.genai.types", types)
spec = importlib.util.spec_from_file_location("icon_generate_under_test", SCRIPT)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
mod.GEMINI_API_KEY = "test-key"
return mod
def _response(text, finish_reason=None):
cand = mock.Mock()
cand.finish_reason = finish_reason
cand.content = None
return mock.Mock(text=text, candidates=[cand])
def test_viewbox_size_only_touches_root_tag(gen):
src = '<svg viewBox="0 0 24 24" stroke-width="2"><rect width="10" height="8"/></svg>'
out = gen.apply_viewbox_size(src, 48)
assert 'stroke-width="2"' in out
assert '<rect width="10" height="8"/>' in out
assert 'viewBox="0 0 24 24"' in out
root = out[: out.index(">") + 1]
assert 'width="48"' in root and 'height="48"' in root
def test_viewbox_size_handles_width_and_height_independently(gen):
out = gen.apply_viewbox_size("<svg width='10' viewBox='0 0 24 24'><path d='M0 0'/></svg>", 32)
root = out[: out.index(">") + 1]
assert "width=\"32\"" in root and "height=\"32\"" in root
assert "width='10'" not in root
@pytest.mark.parametrize("src, kept", [
('<svg aria-label="height=\'2\'" viewBox="0 0 24 24"><path/></svg>',
{"aria-label": "height='2'"}),
("<svg aria-label='width=\"2\" height=\"3\"' viewBox='0 0 24 24'><path/></svg>",
{"aria-label": 'width="2" height="3"'}),
('<svg data-note="a > b width=\'1\'" height="5" viewBox="0 0 24 24"><path/></svg>',
{"data-note": "a > b width='1'"}),
("<svg data-note='x>y height=\"2\"' width='7'><rect width='1' height='1'/></svg>",
{"data-note": 'x>y height="2"'}),
])
def test_viewbox_size_ignores_attribute_text_inside_quoted_values(gen, src, kept):
out = gen.apply_viewbox_size(src, 24)
root = ET.fromstring(out)
assert root.get("width") == "24" and root.get("height") == "24"
for name, value in kept.items():
assert root.get(name) == value
for child in root:
assert child.get("width") in (None, "1")
def test_generate_icon_uses_large_token_budget(gen, tmp_path):
client = gen.genai.Client.return_value
client.models.generate_content.return_value = _response(
'<svg viewBox="0 0 24 24"><path d="M0 0"/></svg>')
out = tmp_path / "a.svg"
assert gen.generate_icon("crane", output_path=str(out)) == str(out)
cfg = client.models.generate_content.call_args.kwargs["config"]
assert cfg.max_output_tokens >= 16384
def test_generate_icon_reports_max_tokens(gen, capsys):
client = gen.genai.Client.return_value
client.models.generate_content.return_value = _response(
'```svg\n<svg viewBox="0 0 24 24"><path d="M 15 14 H 19', "MAX_TOKENS")
assert gen.generate_icon("crane") is None
assert "MAX_TOKENS" in capsys.readouterr().out
assert client.models.generate_content.call_count == 1
def test_generate_icon_reports_empty_output_separately(gen, capsys):
client = gen.genai.Client.return_value
client.models.generate_content.return_value = _response("", "STOP")
assert gen.generate_icon("crane") is None
out = capsys.readouterr().out
assert "empty response" in out and "MAX_TOKENS" not in out
def test_generate_icon_handles_none_text(gen, capsys):
client = gen.genai.Client.return_value
client.models.generate_content.return_value = _response(None, "SAFETY")
assert gen.generate_icon("crane") is None
assert "empty response" in capsys.readouterr().out
def test_generate_batch_reports_max_tokens(gen, tmp_path, capsys):
client = gen.genai.Client.return_value
client.models.generate_content.return_value = _response(
'<svg viewBox="0 0 24 24"><path d="M 15 14 H 19', "MAX_TOKENS")
assert gen.generate_batch("crane", 2, str(tmp_path)) == []
out = capsys.readouterr().out
assert "MAX_TOKENS" in out and "Error generating icons" not in out
def test_generate_batch_handles_empty_response(gen, tmp_path, capsys):
client = gen.genai.Client.return_value
resp = _response(None, "SAFETY")
resp.candidates = []
client.models.generate_content.return_value = resp
assert gen.generate_batch("crane", 2, str(tmp_path)) == []
out = capsys.readouterr().out
assert "empty response" in out and "Error generating icons" not in out
def test_generate_batch_warns_when_truncated_after_some_icons(gen, tmp_path, capsys):
client = gen.genai.Client.return_value
client.models.generate_content.return_value = _response(
'<svg viewBox="0 0 24 24"><path d="M0 0"/></svg>\n'
'<svg viewBox="0 0 24 24"><path d="M 15 14 H 19', "MAX_TOKENS")
assert len(gen.generate_batch("crane", 2, str(tmp_path))) == 1
assert "MAX_TOKENS" in capsys.readouterr().out

View File

@ -174,18 +174,68 @@ def apply_color(svg_code, color):
return svg_code
# Root <svg ...> opening tag; quoted attribute values may contain ">".
_SVG_ROOT_TAG = re.compile(r"""<svg\b(?:[^>"']|"[^"]*"|'[^']*')*>""")
# One token of an opening tag: name[=value], or a stray quoted string.
# Quoted values are consumed whole, so text inside them is never a name.
_TAG_ATTR = re.compile(
r"""([^\s=/>"']+)(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>"']+))?|"[^"]*"|'[^']*'""")
def _set_root_attr(tag, attr, value):
"""Set attr on a single <svg ...> opening tag, whatever quote style it uses."""
for m in _TAG_ATTR.finditer(tag, len('<svg')):
if m.group(1) == attr:
return tag[:m.start()] + f'{attr}="{value}"' + tag[m.end():]
return tag.replace('<svg', f'<svg {attr}="{value}"', 1)
def apply_viewbox_size(svg_code, size):
"""Adjust SVG viewBox to target size"""
"""Set width/height on the root <svg> tag only, leaving the viewBox alone"""
if size:
# Update width/height attributes if present
svg_code = re.sub(r'width="[^"]*"', f'width="{size}"', svg_code)
svg_code = re.sub(r'height="[^"]*"', f'height="{size}"', svg_code)
# Add width/height if not present
if 'width=' not in svg_code:
svg_code = svg_code.replace('<svg', f'<svg width="{size}" height="{size}"', 1)
match = _SVG_ROOT_TAG.search(svg_code)
if match:
tag = _set_root_attr(match.group(0), 'width', size)
tag = _set_root_attr(tag, 'height', size)
svg_code = svg_code[:match.start()] + tag + svg_code[match.end():]
return svg_code
def _finish_reason(response):
"""Return the first candidate's finish reason as a string ('' if unknown)"""
try:
reason = response.candidates[0].finish_reason
except (AttributeError, IndexError, TypeError):
return ""
return str(getattr(reason, "name", reason) or "")
def _response_text(response):
"""Collect the text of a response; '' when it has none (blocked, empty)"""
text = getattr(response, 'text', None) or ""
if not text:
candidates = response.candidates or []
content = candidates[0].content if candidates else None
for part in (content.parts or []) if content else []:
if getattr(part, 'text', None):
text += part.text
return text
def _report_no_svg(response, response_text, what="SVG"):
"""Say why no SVG came back: truncated, empty, or malformed output"""
if "MAX_TOKENS" in _finish_reason(response):
print("Error: output hit the token limit (finish_reason MAX_TOKENS) "
"before the SVG was complete. Try a simpler prompt or style.")
elif not response_text.strip():
print("Error: model returned an empty response.")
else:
print(f"No valid {what} generated. Model response:")
print(response_text[:500])
def generate_icon(prompt, style=None, category=None, name=None,
color=None, size=24, output_path=None, viewbox=24):
"""Generate a single SVG icon using Gemini 3.1 Pro Preview"""
@ -237,22 +287,17 @@ def generate_icon(prompt, style=None, category=None, name=None,
contents=full_prompt,
config=types.GenerateContentConfig(
temperature=0.7,
max_output_tokens=4096,
max_output_tokens=16384,
)
)
# Extract SVG from response
response_text = response.text if hasattr(response, 'text') else ""
if not response_text:
for part in response.candidates[0].content.parts:
if hasattr(part, 'text') and part.text:
response_text += part.text
response_text = _response_text(response)
svgs = extract_svgs(response_text)
if not svgs:
print("No valid SVG generated. Model response:")
print(response_text[:500])
_report_no_svg(response, response_text)
return None
svg_code = svgs[0]
@ -329,17 +374,12 @@ def generate_batch(prompt, count, output_dir, style=None, color=None,
)
)
response_text = response.text if hasattr(response, 'text') else ""
if not response_text:
for part in response.candidates[0].content.parts:
if hasattr(part, 'text') and part.text:
response_text += part.text
response_text = _response_text(response)
svgs = extract_svgs(response_text)
if not svgs:
print("No valid SVGs generated.")
print(response_text[:500])
_report_no_svg(response, response_text, "SVGs")
return []
results = []
@ -357,6 +397,10 @@ def generate_batch(prompt, count, output_dir, style=None, color=None,
results.append(filepath)
print(f" [{i+1}/{len(svgs[:count])}] Saved: {filename}")
if len(results) < count and "MAX_TOKENS" in _finish_reason(response):
print("Warning: output hit the token limit (finish_reason MAX_TOKENS); "
"the last icon was cut off. Try fewer variants or a simpler prompt.")
print(f"\n{'='*60}")
print(f" BATCH COMPLETE: {len(results)}/{count} icons generated")
print(f"{'='*60}\n")

View File

@ -0,0 +1,142 @@
"""Offline regression tests for icon/generate.py (no network, mocked genai)."""
import importlib.util
import sys
import types as pytypes
import xml.etree.ElementTree as ET
from pathlib import Path
from unittest import mock
import pytest
SCRIPT = Path(__file__).resolve().parent.parent / "icon" / "generate.py"
class _Config:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
@pytest.fixture
def gen(monkeypatch):
google = pytypes.ModuleType("google")
genai = pytypes.ModuleType("google.genai")
types = pytypes.ModuleType("google.genai.types")
types.GenerateContentConfig = _Config
genai.Client = mock.MagicMock()
genai.types = types
google.genai = genai
monkeypatch.setitem(sys.modules, "google", google)
monkeypatch.setitem(sys.modules, "google.genai", genai)
monkeypatch.setitem(sys.modules, "google.genai.types", types)
spec = importlib.util.spec_from_file_location("icon_generate_under_test", SCRIPT)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
mod.GEMINI_API_KEY = "test-key"
return mod
def _response(text, finish_reason=None):
cand = mock.Mock()
cand.finish_reason = finish_reason
cand.content = None
return mock.Mock(text=text, candidates=[cand])
def test_viewbox_size_only_touches_root_tag(gen):
src = '<svg viewBox="0 0 24 24" stroke-width="2"><rect width="10" height="8"/></svg>'
out = gen.apply_viewbox_size(src, 48)
assert 'stroke-width="2"' in out
assert '<rect width="10" height="8"/>' in out
assert 'viewBox="0 0 24 24"' in out
root = out[: out.index(">") + 1]
assert 'width="48"' in root and 'height="48"' in root
def test_viewbox_size_handles_width_and_height_independently(gen):
out = gen.apply_viewbox_size("<svg width='10' viewBox='0 0 24 24'><path d='M0 0'/></svg>", 32)
root = out[: out.index(">") + 1]
assert "width=\"32\"" in root and "height=\"32\"" in root
assert "width='10'" not in root
@pytest.mark.parametrize("src, kept", [
('<svg aria-label="height=\'2\'" viewBox="0 0 24 24"><path/></svg>',
{"aria-label": "height='2'"}),
("<svg aria-label='width=\"2\" height=\"3\"' viewBox='0 0 24 24'><path/></svg>",
{"aria-label": 'width="2" height="3"'}),
('<svg data-note="a > b width=\'1\'" height="5" viewBox="0 0 24 24"><path/></svg>',
{"data-note": "a > b width='1'"}),
("<svg data-note='x>y height=\"2\"' width='7'><rect width='1' height='1'/></svg>",
{"data-note": 'x>y height="2"'}),
])
def test_viewbox_size_ignores_attribute_text_inside_quoted_values(gen, src, kept):
out = gen.apply_viewbox_size(src, 24)
root = ET.fromstring(out)
assert root.get("width") == "24" and root.get("height") == "24"
for name, value in kept.items():
assert root.get(name) == value
for child in root:
assert child.get("width") in (None, "1")
def test_generate_icon_uses_large_token_budget(gen, tmp_path):
client = gen.genai.Client.return_value
client.models.generate_content.return_value = _response(
'<svg viewBox="0 0 24 24"><path d="M0 0"/></svg>')
out = tmp_path / "a.svg"
assert gen.generate_icon("crane", output_path=str(out)) == str(out)
cfg = client.models.generate_content.call_args.kwargs["config"]
assert cfg.max_output_tokens >= 16384
def test_generate_icon_reports_max_tokens(gen, capsys):
client = gen.genai.Client.return_value
client.models.generate_content.return_value = _response(
'```svg\n<svg viewBox="0 0 24 24"><path d="M 15 14 H 19', "MAX_TOKENS")
assert gen.generate_icon("crane") is None
assert "MAX_TOKENS" in capsys.readouterr().out
assert client.models.generate_content.call_count == 1
def test_generate_icon_reports_empty_output_separately(gen, capsys):
client = gen.genai.Client.return_value
client.models.generate_content.return_value = _response("", "STOP")
assert gen.generate_icon("crane") is None
out = capsys.readouterr().out
assert "empty response" in out and "MAX_TOKENS" not in out
def test_generate_icon_handles_none_text(gen, capsys):
client = gen.genai.Client.return_value
client.models.generate_content.return_value = _response(None, "SAFETY")
assert gen.generate_icon("crane") is None
assert "empty response" in capsys.readouterr().out
def test_generate_batch_reports_max_tokens(gen, tmp_path, capsys):
client = gen.genai.Client.return_value
client.models.generate_content.return_value = _response(
'<svg viewBox="0 0 24 24"><path d="M 15 14 H 19', "MAX_TOKENS")
assert gen.generate_batch("crane", 2, str(tmp_path)) == []
out = capsys.readouterr().out
assert "MAX_TOKENS" in out and "Error generating icons" not in out
def test_generate_batch_handles_empty_response(gen, tmp_path, capsys):
client = gen.genai.Client.return_value
resp = _response(None, "SAFETY")
resp.candidates = []
client.models.generate_content.return_value = resp
assert gen.generate_batch("crane", 2, str(tmp_path)) == []
out = capsys.readouterr().out
assert "empty response" in out and "Error generating icons" not in out
def test_generate_batch_warns_when_truncated_after_some_icons(gen, tmp_path, capsys):
client = gen.genai.Client.return_value
client.models.generate_content.return_value = _response(
'<svg viewBox="0 0 24 24"><path d="M0 0"/></svg>\n'
'<svg viewBox="0 0 24 24"><path d="M 15 14 H 19', "MAX_TOKENS")
assert len(gen.generate_batch("crane", 2, str(tmp_path))) == 1
assert "MAX_TOKENS" in capsys.readouterr().out