Skip to content

Commit b6e6633

Browse files
fix: openAI input image sanitization (#384)
Co-authored-by: Aleksander Błaszkiewicz <kqmdjc8@gmail.com>
1 parent 4f32fa4 commit b6e6633

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

posthog/ai/sanitization.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ def sanitize_openai_image(item: Any) -> Any:
8383
if not isinstance(item, dict):
8484
return item
8585

86+
if item.get("type") == "input_image" and isinstance(item.get("image_url"), str):
87+
return {
88+
**item,
89+
"image_url": redact_base64_data_url(item["image_url"]),
90+
}
91+
8692
if (
8793
item.get("type") == "image_url"
8894
and isinstance(item.get("image_url"), dict)

posthog/test/ai/test_sanitization.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,25 @@ def test_sanitize_openai(self):
6969
)
7070
self.assertEqual(result[0]["content"][1]["image_url"]["detail"], "high")
7171

72+
def test_sanitize_openai_input_image(self):
73+
input_data = [
74+
{
75+
"role": "user",
76+
"content": [
77+
{
78+
"type": "input_image",
79+
"image_url": self.sample_base64_image,
80+
}
81+
],
82+
}
83+
]
84+
85+
result = sanitize_openai(input_data)
86+
87+
self.assertEqual(
88+
result[0]["content"][0]["image_url"], REDACTED_IMAGE_PLACEHOLDER
89+
)
90+
7291
def test_sanitize_openai_preserves_regular_urls(self):
7392
input_data = [
7493
{

0 commit comments

Comments
 (0)