Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Responses/StreamResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ public function getIterator(): Generator
break;
}

/** @var array{error?: array{message: string|array<int, string>, type: string, code: string}} $response */
/** @var array{error?: array{message: string|array<int, string>, type: string, code: string}, type?: string} $response */
$response = json_decode($data, true, flags: JSON_THROW_ON_ERROR);

if (isset($response['error'])) {
throw new ErrorException($response['error'], $this->response->getStatusCode());
}

if (isset($response['type']) && $response['type'] === 'ping') {
continue;
}

if ($event !== null) {
$response['__event'] = $event;
$response['__meta'] = $this->meta();
Expand Down
8 changes: 8 additions & 0 deletions tests/Fixtures/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ function chatCompletionStream()
return fopen(__DIR__.'/Streams/ChatCompletionCreate.txt', 'r');
}

/**
* @return resource
*/
function chatCompletionStreamPing()
{
return fopen(__DIR__.'/Streams/ChatCompletionPing.txt', 'r');
}

/**
* @return resource
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/Fixtures/Streams/ChatCompletionPing.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data: {"id":"msg_0111RgCFCqN68mJbev6Rq1cz","choices":[{"index":0,"delta":{"role":"assistant"}}],"created":1744469024,"model":"claude-3-7-sonnet-20250219","object":"chat.completion.chunk"}
data: {"type": "ping"}
data: {"id":"msg_0111RgCFCqN68mJbev6Rq1cz","choices":[{"index":0,"delta":{"content":"Hello!"}}],"created":1744469024,"model":"claude-3-7-sonnet-20250219","object":"chat.completion.chunk"}
data: {"id":"msg_0111RgCFCqN68mJbev6Rq1cz","choices":[{"index":0,"delta":{"content":" How can I assist you today? I'm here to help"}}],"created":1744469024,"model":"claude-3-7-sonnet-20250219","object":"chat.completion.chunk"}
data: {"id":"msg_0111RgCFCqN68mJbev6Rq1cz","choices":[{"index":0,"delta":{"content":" with information, answer questions, or discuss"}}],"created":1744469024,"model":"claude-3-7-sonnet-20250219","object":"chat.completion.chunk"}
data: {"id":"msg_0111RgCFCqN68mJbev6Rq1cz","choices":[{"index":0,"delta":{"content":" various topics. Feel free to let me know what you're"}}],"created":1744469024,"model":"claude-3-7-sonnet-20250219","object":"chat.completion.chunk"}
data: {"id":"msg_0111RgCFCqN68mJbev6Rq1cz","choices":[{"index":0,"delta":{"content":" interested in talking about."}}],"created":1744469024,"model":"claude-3-7-sonnet-20250219","object":"chat.completion.chunk"}
data: {"id":"msg_0111RgCFCqN68mJbev6Rq1cz","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"created":1744469024,"model":"claude-3-7-sonnet-20250219","object":"chat.completion.chunk"}
data: [DONE]
23 changes: 23 additions & 0 deletions tests/Resources/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@
->toBeInstanceOf(MetaInformation::class);
});

test('handles ping messages in stream', function () {
$response = new Response(
body: new Stream(chatCompletionStreamPing()),
headers: metaHeaders(),
);

$client = mockStreamClient('POST', 'chat/completions', [
'model' => 'gpt-3.5-turbo',
'messages' => ['role' => 'user', 'content' => 'Hello!'],
'stream' => true,
], $response);

$stream = $client->chat()->createStreamed([
'model' => 'gpt-3.5-turbo',
'messages' => ['role' => 'user', 'content' => 'Hello!'],
]);

foreach ($stream as $response) {
expect($response)
->toBeInstanceOf(CreateStreamedResponse::class);
}
});

test('handles error messages in stream', function () {
$response = new Response(
body: new Stream(chatCompletionStreamError())
Expand Down
7 changes: 7 additions & 0 deletions tests/Responses/Chat/CreateStreamedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,10 @@
expect($response->getIterator()->current())
->id->toBe('chatcmpl-6wdIE4DsUtqf1srdMTsfkJp0VWZgz');
});

test('fake with ping', function () {
$response = CreateStreamedResponse::fake(chatCompletionStreamPing());

expect($response->getIterator()->current())
->id->toBe('msg_0111RgCFCqN68mJbev6Rq1cz');
});