Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
udgithub opened this issue Jan 4, 2024 · 1 comment
Closed

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

udgithub opened this issue Jan 4, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@udgithub
Copy link

udgithub commented Jan 4, 2024

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

@udgithub udgithub added the bug Something isn't working label Jan 4, 2024
@udgithub
Copy link
Author

udgithub commented Jan 4, 2024

OK, I found the solution:
Your documenation said: `$client->files()->download($file);
So I retrieved the file info.
But now it works, when I use the fileId:

$imageFileId = $contentPart->imageFile->fileId;
# don't do THIS:   $imageFile = $client->files()->retrieve($imageFileId);
$imageData = $client->files()->download($imageFileId);

Sorry for misunderstanding your excellent documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant