Skip to content

Commit 9aedefd

Browse files
author
Lucas Alencar Xisto
committed
tests(images): add coverage for optional content_filter_results on Image
1 parent 3154a78 commit 9aedefd

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import httpx
2+
import pytest
3+
from openai import AsyncOpenAI, DefaultAsyncHttpxClient
4+
5+
@pytest.mark.anyio
6+
async def test_images_generate_includes_content_filter_results_async():
7+
"""
8+
Ensure the Image model exposes optional fields returned by the API,
9+
specifically `content_filter_results` (keeping `revised_prompt` coverage).
10+
"""
11+
mock_json = {
12+
"created": 1711111111,
13+
"data": [
14+
{
15+
"url": "https://example.test/cat.png",
16+
"revised_prompt": "a cute cat wearing sunglasses",
17+
"content_filter_results": {
18+
"sexual_minors": {"filtered": False},
19+
"violence": {"filtered": False},
20+
},
21+
}
22+
],
23+
}
24+
25+
# Async handler because we'll use AsyncOpenAI (httpx.AsyncClient under the hood)
26+
async def ahandler(request: httpx.Request) -> httpx.Response:
27+
assert "images" in str(request.url).lower()
28+
return httpx.Response(200, json=mock_json)
29+
30+
atransport = httpx.MockTransport(ahandler)
31+
32+
client = AsyncOpenAI(
33+
api_key="test",
34+
http_client=DefaultAsyncHttpxClient(transport=atransport),
35+
timeout=10.0,
36+
)
37+
38+
resp = await client.images.generate(model="gpt-image-1", prompt="cat with glasses") # type: ignore
39+
40+
assert hasattr(resp, "data") and isinstance(resp.data, list) and resp.data
41+
item = resp.data[0]
42+
43+
# existing field
44+
assert item.revised_prompt == "a cute cat wearing sunglasses"
45+
46+
# new optional field
47+
cfr = item.content_filter_results
48+
assert isinstance(cfr, dict), f"content_filter_results should be dict, got {type(cfr)}"
49+
assert cfr.get("violence", {}).get("filtered") is False
50+
assert cfr.get("sexual_minors", {}).get("filtered") is False

0 commit comments

Comments
 (0)