-
Notifications
You must be signed in to change notification settings - Fork 4.3k
fix(images): add optional content_filter_results to Image model + test coverage #2597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lucasalencarxisto-stack
wants to merge
1
commit into
openai:main
from
lucasalencarxisto-stack:tests/images-content-filter-results
+50
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import httpx | ||
import pytest | ||
from openai import AsyncOpenAI, DefaultAsyncHttpxClient | ||
|
||
@pytest.mark.anyio | ||
async def test_images_generate_includes_content_filter_results_async(): | ||
""" | ||
Ensure the Image model exposes optional fields returned by the API, | ||
specifically `content_filter_results` (keeping `revised_prompt` coverage). | ||
""" | ||
mock_json = { | ||
"created": 1711111111, | ||
"data": [ | ||
{ | ||
"url": "https://example.test/cat.png", | ||
"revised_prompt": "a cute cat wearing sunglasses", | ||
"content_filter_results": { | ||
"sexual_minors": {"filtered": False}, | ||
"violence": {"filtered": False}, | ||
}, | ||
} | ||
], | ||
} | ||
|
||
# Async handler because we'll use AsyncOpenAI (httpx.AsyncClient under the hood) | ||
async def ahandler(request: httpx.Request) -> httpx.Response: | ||
assert "images" in str(request.url).lower() | ||
return httpx.Response(200, json=mock_json) | ||
|
||
atransport = httpx.MockTransport(ahandler) | ||
|
||
client = AsyncOpenAI( | ||
api_key="test", | ||
http_client=DefaultAsyncHttpxClient(transport=atransport), | ||
timeout=10.0, | ||
) | ||
|
||
resp = await client.images.generate(model="gpt-image-1", prompt="cat with glasses") # type: ignore | ||
|
||
assert hasattr(resp, "data") and isinstance(resp.data, list) and resp.data | ||
item = resp.data[0] | ||
|
||
# existing field | ||
assert item.revised_prompt == "a cute cat wearing sunglasses" | ||
|
||
# new optional field | ||
cfr = item.content_filter_results | ||
assert isinstance(cfr, dict), f"content_filter_results should be dict, got {type(cfr)}" | ||
assert cfr.get("violence", {}).get("filtered") is False | ||
assert cfr.get("sexual_minors", {}).get("filtered") is False |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.