Skip to content

[BUG] OpenAI provider cannot use tools that return images #320

Open
@zastrowm

Description

@zastrowm

Checks

  • I have updated to the lastest minor and patch version of Strands
  • I have checked the documentation and this is not expected behavior
  • I have searched ./issues and there are no duplicates of my issue

Strands Version

source

Python Version

3.13

Operating System

macOS

Installation Method

git clone

Steps to Reproduce

  1. Write a tool that returns an image
  2. Provide that tool to a strands agent using OpenAI
  3. Guide agent to use the tool
  4. Exception!
@tool
def tool_with_image_return():
    print(test_image_path)
    with open(test_image_path, "rb") as image_file:
        encoded_image = image_file.read()

    return {
            "status": "success",
            "content": [
                {
                    "image": {
                        "format": "png",
                        "source": {"bytes": encoded_image},
                    }
                },
            ],
        }

agent = Agent(model=OpenAIModel(
    model_id="gpt-4o",
    client_args={
        "api_key": os.getenv("OPENAI_API_KEY"),
    },
), tools=[tool_with_image_return])
agent("Run the the tool and analyze the image")

Expected Behavior

It should work like the BedrockModel does:

Tool #1: tool_with_image_return
The image shows a solid bright yellow square. It's a simple geometric shape with a vibrant, intense yellow color that fills the entire frame. This is a basic color block or color swatch that displays a pure yellow hue without any patterns, textures, or other elements.

Yellow is often associated with:
- Happiness and optimism
- Energy and warmth
- Attention-grabbing (it's one of the most visible colors in the spectrum)
- Caution or warning in some contexts

This appears to be a simple color sample that might be used in design work, color theory demonstrations, or as a placeholder image.

Actual Behavior

It throws an exception:

strands.types.exceptions.EventLoopException: Error code: 400 - {'error': {'message': "Invalid 'messages[3]'. Image URLs are only allowed for messages with role 'user', but this message with role 'tool' contains an image URL.", 'type': 'invalid_request_error', 'param': 'messages[3]', 'code': 'invalid_value'}}

Additional Context

It seems like this is a limitation of the OpenAI api:

Returning image as tool output in Assistants API? - API - OpenAI Developer Community indicates:

Only “user” role messages can include images. You cannot push a user message onto a thread stack while a tool call is still open, and returning the tool call makes the AI write language about the results.

As does Returning image as result of function call to gpt-4-turbo - API / Bugs - OpenAI Developer Community:

Images are allowed in user, system and assistant messages.

Here’s the vision quick start guide:


This was discovered when the generate_image_stability was being added: strands-agents/tools#101 (comment)

Possible Solution

No response

Related Issues

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions