From 1645be2e591e7e0220a796b3afaf30ed5a817abb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Lochm=C3=BCller?= Date: Thu, 18 Dec 2025 11:14:31 +0100 Subject: [PATCH] [Platform] Streamline RawHttpResult from streaming Fix: - Skip SSE comments - Skip SSE errors - Drop manual JSON convert process - Use JSON decode from the ServerSentEvent --- src/platform/src/Result/RawHttpResult.php | 31 +++++++++-------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/platform/src/Result/RawHttpResult.php b/src/platform/src/Result/RawHttpResult.php index cf3ced922..c8da82397 100644 --- a/src/platform/src/Result/RawHttpResult.php +++ b/src/platform/src/Result/RawHttpResult.php @@ -33,30 +33,23 @@ public function getData(): array public function getDataStream(): iterable { foreach ((new EventSourceHttpClient())->stream($this->response) as $chunk) { - if ($chunk->isFirst() || $chunk->isLast() || ($chunk instanceof ServerSentEvent && '[DONE]' === $chunk->getData())) { + // Handle only complete events (no need for handle DataChunk`s) + if (!$chunk instanceof ServerSentEvent) { continue; } - $jsonDelta = $chunk instanceof ServerSentEvent ? $chunk->getData() : $chunk->getContent(); - - // Remove leading/trailing brackets - if (str_starts_with($jsonDelta, '[') || str_starts_with($jsonDelta, ',')) { - $jsonDelta = substr($jsonDelta, 1); - } - if (str_ends_with($jsonDelta, ']')) { - $jsonDelta = substr($jsonDelta, 0, -1); + // Do not handle: Init, Terminate, Errors, Comments and openAI specific termination via DONE + if ( + $chunk->isFirst() + || $chunk->isLast() + || null !== $chunk->getError() + || str_starts_with($chunk->getContent(), ':') + || '[DONE]' === $chunk->getData() + ) { + continue; } - // Split in case of multiple JSON objects - $deltas = explode(",\r\n", $jsonDelta); - - foreach ($deltas as $delta) { - if ('' === trim($delta)) { - continue; - } - - yield json_decode($delta, true, flags: \JSON_THROW_ON_ERROR); - } + yield $chunk->getArrayData(); } }