openai-php / client

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

fix: expect delta to not exist #408

Closed beliven-daniele-sarnari closed 1 month ago

beliven-daniele-sarnari commented 1 month ago

What:

Description:

On Azure, with Asynchronous Filter enabled, in the stream a different packet can exist and throw the following error:

ErrorException

  Undefined array key "delta"

  at vendor/openai-php/client/src/Responses/Chat/CreateStreamedResponseChoice.php:23
     19▕     public static function from(array $attributes): self
     20▕     {
     21▕         return new self(
     22▕             $attributes['index'],
  ➜  23▕             CreateStreamedResponseDelta::from($attributes['delta']),
     24▕             $attributes['finish_reason'] ?? null,
     25▕         );
     26▕     }
     27▕

      +2 vendor frames

  3   [internal]:0
      OpenAI\Responses\Chat\CreateStreamedResponse::OpenAI\Responses\Chat\{closure}()
      +2 vendor frames

Normal packet:

array(3) {
  ["delta"]=>
  array(1) {
    ["content"]=>
    string(6) "amento"
  }
  ["finish_reason"]=>
  NULL
  ["index"]=>
  int(0)
}

Asynchronous Filter packet:

array(4) {
  ["content_filter_offsets"]=>
  array(3) {
    ["check_offset"]=>
    int(61342)
    ["start_offset"]=>
    int(61342)
    ["end_offset"]=>
    int(62102)
  }
  ["content_filter_results"]=>
  array(4) {
    ["hate"]=>
    array(2) {
      ["filtered"]=>
      bool(false)
      ["severity"]=>
      string(4) "safe"
    }
    ["self_harm"]=>
    array(2) {
      ["filtered"]=>
      bool(false)
      ["severity"]=>
      string(4) "safe"
    }
    ["sexual"]=>
    array(2) {
      ["filtered"]=>
      bool(false)
      ["severity"]=>
      string(4) "safe"
    }
    ["violence"]=>
    array(2) {
      ["filtered"]=>
      bool(false)
      ["severity"]=>
      string(4) "safe"
    }
  }
  ["finish_reason"]=>
  NULL
  ["index"]=>
  int(0)
}

Related:

https://github.com/openai-php/client/issues/407

gehrisandro commented 1 month ago

Checkout discussion here: #407