From 4553e68b047839ab8054f038ea6a94b99211d656 Mon Sep 17 00:00:00 2001 From: Mathias Rasmussen Date: Fri, 24 Oct 2025 07:39:54 +0200 Subject: [PATCH 1/2] Feat: Add usage to streamed completions --- src/Responses/Completions/CreateStreamedResponse.php | 3 +++ tests/Fixtures/Streams/CompletionCreate.txt | 2 +- tests/Resources/Completions.php | 5 +++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Responses/Completions/CreateStreamedResponse.php b/src/Responses/Completions/CreateStreamedResponse.php index a0f140c6..8a581b1c 100644 --- a/src/Responses/Completions/CreateStreamedResponse.php +++ b/src/Responses/Completions/CreateStreamedResponse.php @@ -28,6 +28,7 @@ private function __construct( public readonly string $object, public readonly int $created, public readonly string $model, + public readonly? CreateResponseUsage $usage, public readonly array $choices, ) {} @@ -47,6 +48,7 @@ public static function from(array $attributes): self $attributes['object'], $attributes['created'], $attributes['model'], + isset($attributes['usage']) ? CreateResponseUsage::from($attributes['usage']) : null, $choices, ); } @@ -65,6 +67,7 @@ public function toArray(): array static fn (CreateResponseChoice $result): array => $result->toArray(), $this->choices, ), + 'usage' => $this->usage?->toArray(), ]; } } diff --git a/tests/Fixtures/Streams/CompletionCreate.txt b/tests/Fixtures/Streams/CompletionCreate.txt index e9442e0a..dd4bdc56 100644 --- a/tests/Fixtures/Streams/CompletionCreate.txt +++ b/tests/Fixtures/Streams/CompletionCreate.txt @@ -7,5 +7,5 @@ data: {"id": "cmpl-6wcyFqMKXiZffiydSfWHhjcgsf3KD", "object": "text_completion", data: {"id": "cmpl-6wcyFqMKXiZffiydSfWHhjcgsf3KD", "object": "text_completion", "created": 1679430847, "choices": [{"text": " wanted", "index": 0, "logprobs": null, "finish_reason": null}], "model": "gpt-3.5-turbo-instruct"} data: {"id": "cmpl-6wcyFqMKXiZffiydSfWHhjcgsf3KD", "object": "text_completion", "created": 1679430847, "choices": [{"text": " to", "index": 0, "logprobs": null, "finish_reason": null}], "model": "gpt-3.5-turbo-instruct"} data: {"id": "cmpl-6wcyFqMKXiZffiydSfWHhjcgsf3KD", "object": "text_completion", "created": 1679430847, "choices": [{"text": " check", "index": 0, "logprobs": null, "finish_reason": null}], "model": "gpt-3.5-turbo-instruct"} -data: {"id": "cmpl-6wcyFqMKXiZffiydSfWHhjcgsf3KD", "object": "text_completion", "created": 1679430847, "choices": [{"text": " in", "index": 0, "logprobs": null, "finish_reason": "length"}], "model": "gpt-3.5-turbo-instruct"} +data: {"id": "cmpl-6wcyFqMKXiZffiydSfWHhjcgsf3KD", "object": "text_completion", "created": 1679430847, "choices": [{"text": " in", "index": 0, "logprobs": null, "finish_reason": "length"}], "model": "gpt-3.5-turbo-instruct", "usage": {"prompt_tokens": 1, "completion_tokens": 16, "total_tokens": 17}} data: [DONE] diff --git a/tests/Resources/Completions.php b/tests/Resources/Completions.php index 21b53aef..046a729d 100644 --- a/tests/Resources/Completions.php +++ b/tests/Resources/Completions.php @@ -94,6 +94,11 @@ ->logprobs->toBe(null) ->finishReason->toBeNull(); + $iterator = iterator_to_array($result->getIterator()); + $last = end($iterator); + expect($last->usage) + ->toBeInstanceOf(CreateResponseUsage::class); + expect($result->meta()) ->toBeInstanceOf(MetaInformation::class); }); From 4fb61511334333b6869e409fdbe230a1732302b7 Mon Sep 17 00:00:00 2001 From: Mathias Rasmussen Date: Fri, 24 Oct 2025 12:13:45 +0200 Subject: [PATCH 2/2] run lint --- src/Responses/Completions/CreateStreamedResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Responses/Completions/CreateStreamedResponse.php b/src/Responses/Completions/CreateStreamedResponse.php index 8a581b1c..002ac7b5 100644 --- a/src/Responses/Completions/CreateStreamedResponse.php +++ b/src/Responses/Completions/CreateStreamedResponse.php @@ -28,7 +28,7 @@ private function __construct( public readonly string $object, public readonly int $created, public readonly string $model, - public readonly? CreateResponseUsage $usage, + public readonly ?CreateResponseUsage $usage, public readonly array $choices, ) {}