openai-php / client

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

Handle missing attributes for assistant streaming #392

Closed robbie-thompson closed 6 months ago

robbie-thompson commented 6 months ago

What:

Description:

Fixes an error when OpenAI's API returns a delta object which has empty tool calls:

Undefined array key "id" {"exception":"[object] (ErrorException(code: 0): Undefined array key \"id\" at /var/www/html/vendor/openai-php/client/src/Responses/Threads/Runs/Steps/ThreadRunStepResponseFunctionToolCall.php:41)

(
    [id] => step_voklimBxRmx023PqaIxX6hya
    [object] => thread.run.step.delta
    [delta] => Array
        (
            [step_details] => Array
                (
                    [type] => tool_calls
                    [tool_calls] => Array
                        (
                            [0] => Array
                                (
                                    [index] => 0
                                    [type] => function
                                    [function] => Array
                                        (
                                            [arguments] => {}
                                        )
                                )
                        )
                )
        )
)

This is happening to me for all streamed assistant responses that invoke function calls, the function itself appears in one delta, and the next delta then shows the empty function above.

The current implementation is expecting these delta objects to have ID and name fields, which it seems the empty functions don't have.

This is related to the assistants streaming functionality merged in earlier today: https://github.com/openai-php/client/pull/367/

kaipiyann commented 6 months ago

Thks @robbie-thompson ! I had the same problem...And your fix is working !

gehrisandro commented 6 months ago

Thanks @robbie-thompson for the PR!