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
3 changes: 2 additions & 1 deletion src/Responses/StreamResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public function getIterator(): Generator
throw new ErrorException($response['error'], $this->response);
}

if (isset($response['type']) && $response['type'] === 'ping') {
$skippableTypes = ['ping', 'keepalive'];
if (isset($response['type']) && in_array($response['type'], $skippableTypes, true)) {
continue;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Fixtures/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,14 @@ function chatCompletionStreamPing()
return fopen(__DIR__.'/Streams/ChatCompletionPing.txt', 'r');
}

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

/**
* @return resource
*/
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixtures/Streams/ChatCompletionKeepAlive.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
data: {"id":"msg_0111RgCFCqN68mJbev6Rq1cz","choices":[{"index":0,"delta":{"role":"assistant"}}],"created":1744469024,"model":"claude-3-7-sonnet-20250219","object":"chat.completion.chunk"}
data: {"type": "keepalive"}
data: {"id":"msg_0111RgCFCqN68mJbev6Rq1cz","choices":[{"index":0,"delta":{"content":"Hello!"}}],"created":1744469024,"model":"claude-3-7-sonnet-20250219","object":"chat.completion.chunk"}
data: [DONE]
1 change: 1 addition & 0 deletions tests/Fixtures/Streams/ResponseCompletionCreate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ data: {"type":"response.output_text.done","item_id":"msg_67ccf190ca3881909d433c5
data: {"type":"response.content_part.done","item_id":"msg_67ccf190ca3881909d433c50b1f6357e087bb177ab789d5c","output_index":1,"content_index":0,"part":{"type":"output_text","text":"As of today, March 9, 2025, one notable positive news story...","annotations":[{"type":"url_citation","start_index":442,"end_index":557,"url":"https://.../?utm_source=chatgpt.com","title":"..."},{"type":"url_citation","start_index":962,"end_index":1077,"url":"https://.../?utm_source=chatgpt.com","title":"..."},{"type":"url_citation","start_index":1336,"end_index":1451,"url":"https://.../?utm_source=chatgpt.com","title":"..."}]}, "sequence_number": 9}
data: {"type":"response.output_item.done","output_index":1,"item":{"id":"msg_67ccf190ca3881909d433c50b1f6357e087bb177ab789d5c","type":"message","status":"completed","role":"assistant","content":[{"type":"output_text","text":"As of today, March 9, 2025, one notable positive news story...","annotations":[{"type":"url_citation","start_index":442,"end_index":557,"url":"https://.../?utm_source=chatgpt.com","title":"..."},{"type":"url_citation","start_index":962,"end_index":1077,"url":"https://.../?utm_source=chatgpt.com","title":"..."},{"type":"url_citation","start_index":1336,"end_index":1451,"url":"https://.../?utm_source=chatgpt.com","title":"..."}]}]}, "sequence_number": 10}
data: {"type":"response.completed","response":{"id":"resp_67ccf18ef5fc8190b16dbee19bc54e5f087bb177ab789d5c","object":"response","created_at":1741484430,"status":"completed","error":null,"incomplete_details":null,"instructions":null,"max_output_tokens":null,"model":"gpt-4o-2024-08-06","output":[{"type":"web_search_call","id":"ws_67ccf18f64008190a39b619f4c8455ef087bb177ab789d5c","status":"completed","action":{"type":"search","query":"what was a positive news story from today?","sources":[{"type":"url","url":"https://example.com/news/positive-story"},{"type":"url","url":"https://another.example.com/related-article"}]}},{"type":"message","id":"msg_67ccf190ca3881909d433c50b1f6357e087bb177ab789d5c","status":"completed","role":"assistant","content":[{"type":"output_text","text":"As of today, March 9, 2025, one notable positive news story...","annotations":[{"type":"url_citation","start_index":442,"end_index":557,"url":"https://.../?utm_source=chatgpt.com","title":"..."},{"type":"url_citation","start_index":962,"end_index":1077,"url":"https://.../?utm_source=chatgpt.com","title":"..."},{"type":"url_citation","start_index":1336,"end_index":1451,"url":"https://.../?utm_source=chatgpt.com","title":"..."}]}]}],"parallel_tool_calls":true,"previous_response_id":null,"reasoning":{"effort":null,"generate_summary":null},"store":true,"temperature":1.0,"text":{"format":{"type":"text"}},"tool_choice":"auto","tools":[{"type":"web_search_preview","domains":[],"search_context_size":"medium","user_location":{"type":"approximate","city":null,"country":"US","region":null,"timezone":null}}],"top_p":1.0,"truncation":"disabled","usage":{"input_tokens":328,"input_tokens_details":{"cached_tokens":0},"output_tokens":356,"output_tokens_details":{"reasoning_tokens":0},"total_tokens":684},"user":null,"metadata":{}}, "sequence_number": 11}
data: {"type":"keepalive", "sequence_number": 12}
23 changes: 23 additions & 0 deletions tests/Resources/Chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@
}
});

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

$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