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

Handle missing attributes for assistant streaming #392

Closed robbie-thompson closed 1 month ago

robbie-thompson commented 1 month 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 1 month ago

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

gehrisandro commented 1 month ago

Thanks @robbie-thompson for the PR!