client icon indicating copy to clipboard operation
client copied to clipboard

[Responses] Support Image Generation Tool

Open iBotPeaches opened this issue 1 year ago • 9 comments

  • [ ] Support Image Generation Tool - https://platform.openai.com/docs/guides/image-generation?image-generation-model=gpt-image-1#generate-images

iBotPeaches avatar May 21 '25 16:05 iBotPeaches

It looks like a normal responses request, but maybe the SDK doesn't support chasing down the image(s) via type "image_generation_call". Could it be worked around using the existing implementation? Looks like it.

andersborgabiro avatar May 22 '25 13:05 andersborgabiro

Its more we don't yet support that tool type response so its missing from SDK as unparsed. Since the Responses API is pretty complex I want to support a more fluent setup as well. So instead of passing a large array you do with ->withImageGeneration()->asBackground(foo) etc

iBotPeaches avatar May 22 '25 13:05 iBotPeaches

I noted that the Python SDK has a separate method altogether:

result = client.images.generate(
    prompt = prompt,
    model = "gpt-image-1",
    size = image_size,
    quality = image_quality,
    output_format = "png"
)

image_base64 = result.data[0].b64_json

andersborgabiro avatar May 22 '25 22:05 andersborgabiro

We do support that one through the images routes - https://github.com/openai-php/client/blob/199d23715ae9568307bbc2d68e24787cb78b3cca/src/Resources/Images.php#L25. This ticket is supporting that same level of behavior but through the Responses API - which has evolved to do basically do everything all the specialized endpoints used to do.

iBotPeaches avatar May 23 '25 10:05 iBotPeaches

Understood. I thought only Dall-E used that one.

andersborgabiro avatar May 23 '25 20:05 andersborgabiro

The image method works, but as GPT Image 1 takes so much time without a known way to take back control, I had to extend default_socket_timeout (now 240).

Maybe Responses has a queue approach to avoid this.

andersborgabiro avatar May 24 '25 08:05 andersborgabiro

To be fair, it feels (judging from the OpenAI documentation) that this should be part of the CreateResponse.php from Responses:

openai-php/client/src/Responses/Responses/CreateResponse.php:120

        $tools = array_map(
            fn (array $tool): ComputerUseTool|FileSearchTool|FunctionTool|WebSearchTool => match ($tool['type']) {
                'file_search' => FileSearchTool::from($tool),
                'web_search_preview', 'web_search_preview_2025_03_11' => WebSearchTool::from($tool),
                'function' => FunctionTool::from($tool),
                'computer_use_preview' => ComputerUseTool::from($tool),
            },
            $attributes['tools'],
        )

The error, at this time, is a simple unhandled match case:

  Unhandled match case 'image_generatio...'

Called with the following:

[{"type":"image_generation","background":"auto","moderation":"auto","n":1,"output_compression":100,"output_format":"png","quality":"auto","size":"auto"}]

I am creating a Agent Swarm factory that has an Agent specialized in generation images. As such, I only call the endpoint with these mere attributes:

        [
            'model' => 'gpt-4.1-mini',
            'tools' => [
                [
                    'type' => 'image_generation',
                ]
            ]
        ]

CyntaxDotDev avatar Jun 02 '25 14:06 CyntaxDotDev

That was my understanding. Responses API just gained a bunch of new tools (image gen, summary, mcp, etc). Those were all those tickets I seeded into backlog.

iBotPeaches avatar Jun 02 '25 14:06 iBotPeaches

Excellent. Looks like they might be tweaking it after the release of the Responses API compatibility with gpt-image-1. https://platform.openai.com/docs/guides/image-generation?image-generation-model=gpt-image-1#model-comparison

CyntaxDotDev avatar Jun 02 '25 14:06 CyntaxDotDev