openai-php / client

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

CreateResponseEmbedding - ErrorException - typed argument is null #182

Closed coreation closed 1 year ago

coreation commented 1 year ago

Hi there,

When I'm using the embeddings endpoint I sometimes get the following ErrorException - which cannot be caught:

## Get some embeddings
try { 
     $this
      ->getApiClient()
       ->embeddings()
        ->create([
           'model' => self::OPEN_AI_EMBEDDING_MODEL,
            'input' => $texts,
        ]);
} catch (\Exception $ex) {
//
}

### This sometimes gives the following error

the following error occurred: OpenAI\Responses\Embeddings\CreateResponseEmbedding::__construct(): Argument #3 ($embedding) must be of type array, null given, called in /var/www/minerva/vendor/openai-php/client/src/Responses/Embeddings/CreateResponseEmbedding.php on line 27

TypeError - /vendor/openai-php/client/src/Responses/Embeddings/CreateResponseEmbedding.php:12
#0 /var/www/minerva/vendor/openai-php/client/src/Responses/Embeddings/CreateResponseEmbedding.php(27): OpenAI\Responses\Embeddings\CreateResponseEmbedding->__construct()
#1 /var/www/minerva/vendor/openai-php/client/src/Responses/Embeddings/CreateResponse.php(41): OpenAI\Responses\Embeddings\CreateResponseEmbedding::from()
#2 [internal function]: OpenAI\Responses\Embeddings\CreateResponse::OpenAI\Responses\Embeddings\{closure}()

It seems to me that OpenAI can provide a null response for one or more embeddings, but the DTOs that are made do no take this into account, making the entire request fail, without the possibility to catch the failure.

If I make a change to make the embedding nullable in the CreateResponseEmbedding constructor, would that be accepted or do you see a different / better approach?

gehrisandro commented 1 year ago

Hi @coreation

According to the official OpenAI docs this should always be an array: https://github.com/openai/openai-openapi/blob/master/openapi.yaml#L3281

Can you give as an example how to reproduce the issue?

coreation commented 1 year ago

Hi @gehrisandro , unfortunately I can't. This is only occurring in the proverbial 1-2% of bulk embed API calls. That's why I didn't include a code example, but the stack trace itself. I can provide the API call that I'm making, but it's just a straightforward bulk embed API call:

$this
                ->getApiClient()
                ->embeddings()
                ->create([
                    'model' => 'text-embedding-ada-002',
                    'input' => $texts, // $texts being an array of strings
                ]);

Most of the times, this works out without a hitch, sometimes though, I get the error mentioned in the original post.

gehrisandro commented 1 year ago

I tried to reproduce the issue by hitting the API in an endless loop. But even after thousands of requests, I never ran into this issue.

Currently I think it would be bad practice to adjust the library based on an undocumented and probably unintended behaviour. What do you think?

coreation commented 1 year ago

Hm, could try sneaking in a null as a value in the array or an empty string? My code trims all of the texts, so a null value is highly unlikely to be the cause, but an empty string might be it.

If that's still ending up with nothing, you can close the issue and I'll just use my work-around, which is just embedding texts one by one.

gehrisandro commented 1 year ago

I've already tried empty strings and null values, but both instantly return an API error:

  "error": {
    "message": "'$.input' is invalid. Please check the API reference: https://platform.openai.com/docs/api-reference.",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
coreation commented 1 year ago

Ok, then this can be closed as far as I'm concerned. :)