openai-php / client

⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API.
MIT License
4.94k stars 513 forks source link

[Bug]: Undefined array key "file_id #456

Open ckefalianou opened 3 months ago

ckefalianou commented 3 months ago

Description

Undefined array key "file_id"

I did some digging, and found out that the issue is created in that file: openai-php/client/src/Responses/Threads/Messages/ThreadMessageResponseContentImageUrl.php

because it looks for file_id not url , which file_id doesn't exist in the content I sent.

Can you please resolve it or suggest a solution? Thank you in advance!

Steps To Reproduce

I am using the library, sending a text and some image_url as content. After retrieving the Assistant, creating a Thread, I create the following Message:

$openAI_message = $client->threads()->messages()->create($openAI_thread->id, [ 'role' => 'user', 'content' => [ [ 'type' => 'text', 'text' => $prompt['title'] ], [ 'type' => 'image_url', 'image_url' => [ 'url' => $photo[0]->large, 'detail' => 'high', ], ], [ 'type' => 'image_url', 'image_url' => [ 'url' => $photo[1]->large, 'detail' => 'high', ], ], ] ]); After creating the Run I get the following issue: Undefined array key "file_id"

OpenAI PHP Client Version

^0.10.1

PHP Version

8.3

Notes

No response

SergiuDihel commented 3 months ago

I have the same issue.

I am trying to do something like:

'thread' => [
    'messages' => [[
        'role' => 'user',
        'content' => [
            [ 'type' => 'text', 'text' => $description ],
            [ 'type' => 'image_url', 'image_url' => [ 'url' => 'https://example.com/image.jpg']],
        ],
    ]],
],

From what I can understand, the issue is originating inside src/Responses/Threads/Messages/ThreadMessageResponseContentImageUrlObject.php on line 41.

return new self(
    $attributes['type'],
    ThreadMessageResponseContentImageUrl::from($attributes['image_url']),
);

ThreadMessageResponseContentImageUrl::from($attributes['image_url']) is trying to send a string (probably) containing the URL of the image. But if you look inside the static from, in ThreadMessageResponseContentImageUrl, you'll see it expects an array of attributes:

public static function from(array $attributes): self
{
    return new self(
        $attributes['file_id'],
        $attributes['detail'] ?? null,
    );
}

So it fails. Hope I'm correct on this, I haven't spent too much time going deeper than this. 🤞

MaskowLabs commented 3 months ago

Everyone having the same issue, this is the fix:

  1. Change the File /vendor/openai-php/client/src/Responses/Threads/Messages/ThreadMessageResponseContentImageUrl.php
private function __construct(
        public string $imageUrl,
        public ?string $detail,
    ) {
    }

    public static function from(array $attributes): self {
        return new self(
            $attributes['url'],
            $attributes['detail'] ?? null,
        );
    }
    public function toArray(): array {
        return array_filter([
            'url' => $this->imageUrl,
            'detail' => $this->detail,
        ], fn (?string $value): bool => $value !== null);
    }
  1. Change the file /vendor/openai-php/client/src/Responses/Threads/Messages/ThreadMessageResponseContentImageUrlObject.php

    public static function from(array $attributes): self{
        return new self(
            $attributes['type'],
            ThreadMessageResponseContentImageUrl::from($attributes), 
        );
    }
ckefalianou commented 3 months ago

@jumaskow @SergiuDihel thank you for your responses.

I did the same and I was about to send a PR but I found out that issue is fixed at https://github.com/openai-php/client/blob/main/src/Responses/Threads/Messages/ThreadMessageResponseContentImageUrl.php

but not released yet.

I just replaced the version with "dev-main" until there's a release.

Thank you again! :)

SergiuDihel commented 3 months ago

Hey @ckefalianou! Damn, we synced just now. I was about to comment on this myself since I also realized it just now. 😆

The commit fixing this issue is here: https://github.com/openai-php/client/commit/62a09ae36a19a42f2592ad38cd0e3d6add05a84d

It was merged on June 7, with this PR: https://github.com/openai-php/client/pull/422

But it is not yet released, as you've said. For time being, I also currently use the current latest commit on HEAD. "openai-php/client": "dev-main#e9ca2886aa9c0bf60fb3b8df00ad9e5b876a0916"

UPDATE: @jumaskow, I've also tested this and it works, so I don't think we need https://github.com/openai-php/client/pull/459.

gehrisandro commented 2 months ago

Going to make a new release tomorrow. Sorry for the long waiting time.