Skip to content

Commit b90b80a

Browse files
authored
Merge pull request #317 from nakshaatraa/fix/image-generator-matplotlib-warnings
fix: set matplotlib Agg backend and sanitize prompt whitespace
2 parents b827a0b + 6dec776 commit b90b80a

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

agentic_security/probe_data/image_generator.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import base64
22
import io
3+
import re
34

45
import httpx
5-
import matplotlib.pyplot as plt
6+
import matplotlib
7+
8+
matplotlib.use("Agg")
9+
import matplotlib.pyplot as plt # noqa: E402
610
from cache_to_disk import cache_to_disk
711
from tqdm import tqdm
812

@@ -49,6 +53,10 @@ def generate_image(prompt: str, variant: int = 0) -> bytes:
4953
Returns:
5054
bytes: The image data in JPG format.
5155
"""
56+
# Sanitize prompt: replace non-renderable whitespace characters (tabs, etc.)
57+
# with spaces to avoid matplotlib UserWarning about missing glyphs.
58+
prompt = re.sub(r"[\t\r\x0b\x0c]", " ", prompt)
59+
5260
# Create a matplotlib figure
5361
fig, ax = plt.subplots(figsize=(6, 4))
5462

agentic_security/probe_data/test_image_generator.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,12 @@ def test_generate_image_dataset(mock_generate_image):
3939
assert isinstance(image_datasets[0], ImageProbeDataset)
4040
assert image_datasets[0].test_dataset.dataset_name == test_dataset_name
4141
assert image_datasets[0].image_prompts[0] == b"dummy_image_bytes"
42+
43+
44+
def test_generate_image_with_special_whitespace():
45+
"""Test that prompts with tab and other non-renderable whitespace don't raise warnings."""
46+
prompt_with_tabs = "Hello\tWorld\tTest"
47+
image_bytes = generate_image(prompt_with_tabs, 0)
48+
49+
assert isinstance(image_bytes, bytes)
50+
assert len(image_bytes) > 0

0 commit comments

Comments
 (0)