Skip to content

Conversation

clementmas
Copy link
Contributor

What:

  • Bug Fix
  • New Feature

Description:

When a response is streaming tool calls, we have to know the index of the delta to recompose the JSON arguments properly.

See tool calls chunk combination code
foreach ($toolCallsDelta ?? [] as $toolCallDelta) {
    if (! isset($toolCalls[$toolCallDelta->index])) {
        // Initialize the tool call when first encountered
        $toolCalls[$toolCallDelta->index] = [
            'id' => $toolCallDelta->id,
            'type' => $toolCallDelta->type,
            'function' => [
                'name' => $toolCallDelta->function->name ?? '',
                'arguments' => $toolCallDelta->function->arguments ?? '',
            ],
        ];

        continue;
    }

    // Append to arguments for subsequent chunks
    if (isset($toolCallDelta->function->arguments)) {
        $toolCalls[$toolCallDelta->index]['function']['arguments'] .= $toolCallDelta->function->arguments;
    }
}

I don't see any other way to do this except not use any streaming which doesn't provide an acceptable UX.

I'm open to suggestions if I missed something.

Copy link
Collaborator

@iBotPeaches iBotPeaches left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make 2 changes? then good to go

  1. Add the missing property to the toArray function.
  2. Update a test to assert the index property can be pulled off the object.

@clementmas clementmas requested a review from iBotPeaches May 15, 2025 08:54
@iBotPeaches
Copy link
Collaborator

src/Responses/Chat/CreateStreamedResponseToolCall.php ............. pa39 96%  

Odd that file does not report as 100% covered anymore. You may have to run the coverage report locally and see what line its complaining about.

@clementmas
Copy link
Contributor Author

I fixed the missing type on the callback fn ($value) => $value !== null. I need it to make sure index = 0 is not removed from the array.

@iBotPeaches iBotPeaches changed the title Add index to CreateStreamedResponseToolCall fix(OpenAI) - Add index to CreateStreamedResponseToolCall May 22, 2025
@iBotPeaches iBotPeaches merged commit 199d237 into openai-php:main May 22, 2025
12 checks passed
@iBotPeaches
Copy link
Collaborator

thanks!

@iBotPeaches iBotPeaches added this to the v0.13.1 milestone May 22, 2025
@iBotPeaches iBotPeaches added the bug Something isn't working label Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants