Skip to content

[Bug]: Can't download imageFile generated by code_interpreter #313

Closed
@udgithub

Description

@udgithub

Description

Hello guys,

first of all: thanks for your great development.
So I as an PHP developer can use OpenAI API almost as convenient as with Python.

Now the problem:
If I go through all messages I can find a $contentPart->type of "image_file" .
If I try to get the image data like this:

$imageFileId = $contentPart->imageFile->fileId;
$imageFile = $client->files()->retrieve($imageFileId);
$imageData = $client->files()->download($imageFile);

I get an error:
Fatal error: Uncaught TypeError: OpenAI\Responses\Files\RetrieveResponse::__construct(): Argument #3 ($bytes) must be of type int, null given, called in /my-project-path/vendor/openai-php/client/src/Responses/Files/RetrieveResponse.php on line 51 and defined in /my-project-path/vendor/openai-php/client/src/Responses/Files/RetrieveResponse.php on line 30

That is the loop to process the messages:

    foreach ($messages->data as $message) {
        if (!empty($message->content)) {
            foreach ($message->content as $contentPart) {
                if ($contentPart->type == "text") {
		    # skipping this to keep code small
                } elseif ($contentPart->type == "image_file") {
                    $imageFileId = $contentPart->imageFile->fileId;
                    $imageFile = $client->files()->retrieve($imageFileId);
                    $imageData = $client->files()->download($imageFile);
                    print_r($imageData);
                    file_put_contents("image_$imageCounter.png", $imageData);
                    $imageCounter++;
                }
            }
        }
    }

It would be great if you could give me an hint what I'm doing wrong or fix the bug if it is one.
Thanks a lot in advance for your help.

Steps To Reproduce

create an assistant of type code_interpreter and send a request like
plot these numbers in a graph: 1,2,3,4,15,73,12
then try to read the messages and download the imageFile

OpenAI PHP Client Version

0.8.1

PHP Version

8.1.2

Notes

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions