Skip to content

Commit c09ae1e

Browse files
committed
fix(openai): address comments
1 parent 767f5b7 commit c09ae1e

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/strands/models/openai.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,27 @@ def _split_tool_message_images(
209209
if not image_content:
210210
return tool_message, None
211211

212-
# Create tool message with only text content
213-
# If no text content, add a simple success message
212+
# Let the user know that we are modifying the messages for OpenAI compatibility
213+
logger.warning(
214+
"tool_call_id=<%s> | Moving image from tool message to a new user message for OpenAI compatibility",
215+
tool_message["tool_call_id"],
216+
)
217+
218+
# Append a message to the text content to inform the model about the upcoming image
219+
text_content.append(
220+
{
221+
"type": "text",
222+
"text": (
223+
"Tool successfully returned an image. The image is being provided in the following user message."
224+
),
225+
}
226+
)
227+
228+
# Create the clean tool message with the updated text content
214229
tool_message_clean = {
215230
"role": "tool",
216231
"tool_call_id": tool_message["tool_call_id"],
217-
"content": text_content
218-
if text_content
219-
else [{"type": "text", "text": "Tool execution completed successfully."}],
232+
"content": text_content,
220233
}
221234

222235
# Create user message with only images

tests/strands/models/test_openai.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,13 @@ def test_split_tool_message_images_with_image():
192192

193193
tool_clean, user_with_image = OpenAIModel._split_tool_message_images(tool_message)
194194

195-
# Tool message should only have text
195+
# Tool message should now have the original text plus the appended informational text
196196
assert tool_clean["role"] == "tool"
197197
assert tool_clean["tool_call_id"] == "c1"
198-
assert len(tool_clean["content"]) == 1
198+
assert len(tool_clean["content"]) == 2
199199
assert tool_clean["content"][0]["type"] == "text"
200+
assert tool_clean["content"][0]["text"] == "Result"
201+
assert "Tool successfully returned an image" in tool_clean["content"][1]["text"]
200202

201203
# User message should have the image
202204
assert user_with_image is not None

0 commit comments

Comments
 (0)