Skip to content

Commit c94d334

Browse files
behnam-oBehnam Ousat
andauthored
FIX send single-image edit requests as single element not array (#1773)
Co-authored-by: Behnam Ousat <behnamousat@microsoft.com>
1 parent 782cf98 commit c94d334

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

pyrit/prompt_target/openai/openai_image_target.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ async def _send_edit_request_async(self, message: Message) -> Message:
296296
# Construct request parameters for image editing
297297
image_edit_args: dict[str, Any] = {
298298
"model": self._model_name,
299-
"image": image_files,
299+
# Single image can be sent (and MUST BE in MAI models) as a tuple, multiple images must be a list
300+
"image": image_files[0] if len(image_files) == 1 else image_files,
300301
"prompt": text_prompt,
301302
"size": self.image_size,
302303
}

tests/unit/prompt_target/target/test_image_target.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,42 @@ async def test_send_prompt_async_edit(
131131
os.remove(image_piece.original_value)
132132

133133

134+
async def test_send_prompt_async_edit_single_image_passes_tuple_not_list(
135+
image_target: OpenAIImageTarget,
136+
):
137+
image_piece = get_image_message_piece()
138+
text_piece = MessagePiece(
139+
role="user",
140+
conversation_id=image_piece.conversation_id,
141+
original_value="edit this image",
142+
converted_value="edit this image",
143+
original_value_data_type="text",
144+
converted_value_data_type="text",
145+
)
146+
147+
mock_response = MagicMock()
148+
mock_image = MagicMock()
149+
mock_image.b64_json = "aGVsbG8="
150+
mock_response.data = [mock_image]
151+
152+
with patch.object(image_target._async_client.images, "edit", new_callable=AsyncMock) as mock_edit:
153+
mock_edit.return_value = mock_response
154+
155+
resp = await image_target.send_prompt_async(message=Message([text_piece, image_piece]))
156+
assert resp
157+
158+
call_kwargs = mock_edit.call_args[1]
159+
assert isinstance(call_kwargs["image"], tuple)
160+
assert len(call_kwargs["image"]) == 3
161+
162+
path = resp[0].message_pieces[0].original_value
163+
if os.path.isfile(path):
164+
os.remove(path)
165+
166+
if os.path.isfile(image_piece.original_value):
167+
os.remove(image_piece.original_value)
168+
169+
134170
async def test_send_prompt_async_edit_multiple_images(
135171
image_target: OpenAIImageTarget,
136172
):

0 commit comments

Comments
 (0)