From d15ce36e0bb30f2579099bcfc23e7657f21dfc61 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Fri, 13 Nov 2026 20:30:31 -0400 Subject: [PATCH] fix(OpenAI): keep logprobs on streamed chat choices Streamed chat chunks requested with logprobs=true carry choices[].logprobs, but CreateStreamedResponseChoice dropped them. Parse it with CreateResponseChoiceLogprobs, as the non-streamed choice does. --- .../Chat/CreateStreamedResponseChoice.php | 14 +++++-- .../Chat/CreateStreamedResponseChoice.php | 39 +++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/Responses/Chat/CreateStreamedResponseChoice.php b/src/Responses/Chat/CreateStreamedResponseChoice.php index 40f15a51c..75f30d74a 100644 --- a/src/Responses/Chat/CreateStreamedResponseChoice.php +++ b/src/Responses/Chat/CreateStreamedResponseChoice.php @@ -9,30 +9,38 @@ final class CreateStreamedResponseChoice private function __construct( public readonly int $index, public readonly CreateStreamedResponseDelta $delta, + public readonly ?CreateResponseChoiceLogprobs $logprobs, public readonly ?string $finishReason, ) {} /** - * @param array{index: int, delta?: array{role?: string, content?: string}, finish_reason: string|null} $attributes + * @param array{index: int, delta?: array{role?: string, content?: string}, logprobs?: ?array{content: ?array}>}, finish_reason: string|null} $attributes */ public static function from(array $attributes): self { return new self( $attributes['index'], CreateStreamedResponseDelta::from($attributes['delta'] ?? []), + isset($attributes['logprobs']) ? CreateResponseChoiceLogprobs::from($attributes['logprobs']) : null, $attributes['finish_reason'] ?? null, ); } /** - * @return array{index: int, delta: array{role?: string, content?: string}|array{role?: string, content: null, function_call: array{name?: string, arguments?: string}}, finish_reason: string|null} + * @return array{index: int, delta: array{role?: string, content?: string}|array{role?: string, content: null, function_call: array{name?: string, arguments?: string}}, logprobs?: array{content: ?array}>}, finish_reason: string|null} */ public function toArray(): array { - return [ + $result = [ 'index' => $this->index, 'delta' => $this->delta->toArray(), 'finish_reason' => $this->finishReason, ]; + + if ($this->logprobs !== null) { + $result['logprobs'] = $this->logprobs->toArray(); + } + + return $result; } } diff --git a/tests/Responses/Chat/CreateStreamedResponseChoice.php b/tests/Responses/Chat/CreateStreamedResponseChoice.php index fedb21621..72490d3de 100644 --- a/tests/Responses/Chat/CreateStreamedResponseChoice.php +++ b/tests/Responses/Chat/CreateStreamedResponseChoice.php @@ -1,5 +1,6 @@ toArray()) ->toBe(chatCompletionStreamFirstChunk()['choices'][0]); }); + +test('from with logprobs', function () { + $data = chatCompletionStreamContentChunk()['choices'][0]; + $data['logprobs'] = [ + 'content' => [ + ['token' => 'Hello', 'logprob' => -0.31, 'bytes' => [72, 101, 108, 108, 111], 'top_logprobs' => []], + ], + 'refusal' => null, + ]; + + $result = CreateStreamedResponseChoice::from($data); + + expect($result->logprobs) + ->toBeInstanceOf(CreateResponseChoiceLogprobs::class) + ->content->toHaveCount(1) + ->content->{0}->token->toBe('Hello') + ->content->{0}->logprob->toBe(-0.31); +}); + +test('from without logprobs', function () { + $result = CreateStreamedResponseChoice::from(chatCompletionStreamContentChunk()['choices'][0]); + + expect($result->logprobs)->toBeNull(); +}); + +test('to array with logprobs', function () { + $data = chatCompletionStreamContentChunk()['choices'][0]; + $data['logprobs'] = [ + 'content' => [ + ['token' => 'Hello', 'logprob' => -0.31, 'bytes' => [72, 101, 108, 108, 111], 'top_logprobs' => []], + ], + ]; + + $array = CreateStreamedResponseChoice::from($data)->toArray(); + + expect($array['logprobs']['content'][0]) + ->toMatchArray(['token' => 'Hello', 'logprob' => -0.31]); +});