Skip to content

Commit

Permalink
Make finishReason nullable openai-php#51
Browse files Browse the repository at this point in the history
A recent change to the OpenAI API means that finish_reason is sometimes returned as NULL and causes an exception.

This change allows finishReason to be nullable.
  • Loading branch information
careybaird committed Feb 26, 2023
1 parent 578e2da commit c76e169
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ foreach ($response->choices as $result) {
$result->text; // '\n\nThis is a test'
$result->index; // 0
$result->logprobs; // null
$result->finishReason; // 'length'
$result->finishReason; // 'length' or null
}

$response->usage->promptTokens; // 5,
Expand Down
2 changes: 1 addition & 1 deletion src/Responses/Completions/CreateResponseChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ private function __construct(
public readonly string $text,
public readonly int $index,
public readonly ?CreateResponseChoiceLogprobs $logprobs,
public readonly string $finishReason,
public readonly ?string $finishReason,
) {
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Responses/Completions/CreateResponseChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
->text->toBe("el, she elaborates more on the Corruptor's role, suggesting K")
->index->toBe(0)
->logprobs->toBe(null)
->finishReason->toBe('length');
->finishReason->toBeIn(['length', null]);
});

test('to array', function () {
Expand All @@ -27,7 +27,7 @@
->text->toBe('PHP is')
->index->toBe(0)
->logprobs->toBeInstanceOf(CreateResponseChoiceLogprobs::class)
->finishReason->toBe('length');
->finishReason->toBeIn(['length', null]);
});

test('to array with logprobs', function () {
Expand Down

0 comments on commit c76e169

Please sign in to comment.