From 2488d8187869eaa4fe0f07cb8cf2e8f3ae661daf Mon Sep 17 00:00:00 2001 From: Dick Tump Date: Sun, 1 Nov 2026 09:42:20 +0100 Subject: [PATCH 1/3] fix: parse array content in streamed chat responses Mistral streams content as a list of typed chunks. The SSE parser only accepted string content, so answer text was dropped silently. Assisted-by: ClaudeCode:deepseek-v4.1-flash Signed-off-by: Dick Tump --- lib/Service/StreamingService.php | 24 ++++++++++++++++- tests/unit/Service/StreamingServiceTest.php | 30 +++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lib/Service/StreamingService.php b/lib/Service/StreamingService.php index d3ebd1b7..ef708c2f 100644 --- a/lib/Service/StreamingService.php +++ b/lib/Service/StreamingService.php @@ -146,7 +146,13 @@ private function parseSseEvent(string $event, bool &$done, ?array &$usage, array $choices[$index]['finish_reason'] = $choice['finish_reason']; } - if (isset($choice['delta']['content']) && is_string($choice['delta']['content']) && $choice['delta']['content'] !== '') { + if (isset($choice['delta']['content']) && is_array($choice['delta']['content'])) { + $deltaText = $this->contentChunksToText($choice['delta']['content']); + if ($deltaText !== '') { + $choices[$index]['message']['content'] = ($choices[$index]['message']['content'] ?? '') . $deltaText; + $partials[] = ['kind' => 'content', 'text' => $deltaText, 'index' => $index]; + } + } elseif (isset($choice['delta']['content']) && is_string($choice['delta']['content']) && $choice['delta']['content'] !== '') { $choices[$index]['message']['content'] = ($choices[$index]['message']['content'] ?? '') . $choice['delta']['content']; $partials[] = ['kind' => 'content', 'text' => $choice['delta']['content'], 'index' => $index]; } elseif (isset($choice['message']['content']) && is_string($choice['message']['content']) && $choice['message']['content'] !== '') { @@ -238,6 +244,22 @@ private function parseSseChunk(string $chunk, string &$buffer, bool &$done, ?arr return $partials; } + /** + * Extract the text of typed content chunks, such as Mistral's `text`, `thinking` and + * `reference` parts. Parts with another type are skipped. + * + * @param array $chunks + */ + private function contentChunksToText(array $chunks): string { + $text = ''; + foreach ($chunks as $chunk) { + if (is_array($chunk) && ($chunk['type'] ?? null) === 'text' && isset($chunk['text']) && is_string($chunk['text'])) { + $text .= $chunk['text']; + } + } + return $text; + } + /** * Merge fragmented `tool_calls` deltas from multiple SSE events into the accumulated * message structure for a single choice. diff --git a/tests/unit/Service/StreamingServiceTest.php b/tests/unit/Service/StreamingServiceTest.php index 56808bfa..b6f8afe2 100644 --- a/tests/unit/Service/StreamingServiceTest.php +++ b/tests/unit/Service/StreamingServiceTest.php @@ -40,6 +40,36 @@ public function testEofFlushesAnIncompleteFinalEventAndClosesTheStream(): void { $this->assertFalse(is_resource($body)); } + public function testTypedContentChunksAreAssembledAndStreamed(): void { + $body = fopen('php://temp', 'r+'); + $this->assertIsResource($body); + fwrite($body, "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":[{\"type\":\"thinking\",\"thinking\":[{\"type\":\"text\",\"text\":\"Thinking\"}]}]}}]}\n\n" + . "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":[{\"type\":\"text\",\"text\":\"Hel\"}]}}]}\n\n" + . "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":[{\"type\":\"text\",\"text\":\"lo\"},{\"type\":\"reference\",\"reference_ids\":[1]}]},\"finish_reason\":\"stop\"}]}\n\n" + . "data: [DONE]\n\n"); + rewind($body); + + $l10n = $this->createMock(IL10N::class); + $l10n->method('t')->willReturnArgument(0); + $service = new StreamingService($l10n); + $generator = $service->parseStreamChatResponse([ + 'body' => $body, + 'content-type' => 'text/event-stream', + ]); + + $this->assertSame([ + ['kind' => 'content', 'text' => 'Hel', 'index' => 0], + ['kind' => 'content', 'text' => 'lo', 'index' => 0], + ], iterator_to_array($generator, false)); + $this->assertSame([ + 'choices' => [[ + 'index' => 0, + 'message' => ['content' => 'Hello'], + 'finish_reason' => 'stop', + ]], + ], $generator->getReturn()); + } + public function testDoneEventStopsReadingAnOpenStreamAndClosesIt(): void { $events = "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hello\"}}]}\n\n" . "data: {\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n" From aaec314b49e7825afd5e4a3be686791dc634f5f6 Mon Sep 17 00:00:00 2001 From: Dick Tump Date: Sun, 8 Nov 2026 08:47:06 +0100 Subject: [PATCH 2/3] fix: handle empty streamed content arrays and share the text extraction Check that a streamed content array is not empty before parsing it, and reuse the same chunk-to-text extraction in createChatCompletion() instead of a duplicate loop. Assisted-by: ClaudeCode:deepseek-v4.1-flash Signed-off-by: Dick Tump --- lib/Service/OpenAiAPIService.php | 8 +-- lib/Service/StreamingService.php | 10 ++-- tests/unit/Service/StreamingServiceTest.php | 57 +++++++++++++++++++++ 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/lib/Service/OpenAiAPIService.php b/lib/Service/OpenAiAPIService.php index 55bd8fe6..e397805a 100644 --- a/lib/Service/OpenAiAPIService.php +++ b/lib/Service/OpenAiAPIService.php @@ -1630,14 +1630,8 @@ private function normalizeChatCompletionResponse(array $response): array { if (is_string($choice['message']['content'])) { $completions['messages'][] = $choice['message']['content']; } elseif (is_array($choice['message']['content'])) { - $messageContent = []; // Handles more complex mistral message content (TODO: missing reasoning for now look at https://github.com/nextcloud/integration_openai/pull/409#discussion_r3638108824) - foreach ($choice['message']['content'] as $content) { - if ($content['type'] === 'text' && is_string($content['text'])) { - $messageContent[] = $content['text']; - } - } - $completions['messages'][] = implode('', $messageContent); + $completions['messages'][] = $this->streamingService->contentChunksToText($choice['message']['content']); } } if (isset($choice['message']['reasoning_content']) && is_string($choice['message']['reasoning_content'])) { diff --git a/lib/Service/StreamingService.php b/lib/Service/StreamingService.php index ef708c2f..290d127f 100644 --- a/lib/Service/StreamingService.php +++ b/lib/Service/StreamingService.php @@ -146,7 +146,7 @@ private function parseSseEvent(string $event, bool &$done, ?array &$usage, array $choices[$index]['finish_reason'] = $choice['finish_reason']; } - if (isset($choice['delta']['content']) && is_array($choice['delta']['content'])) { + if (isset($choice['delta']['content']) && is_array($choice['delta']['content']) && $choice['delta']['content'] !== []) { $deltaText = $this->contentChunksToText($choice['delta']['content']); if ($deltaText !== '') { $choices[$index]['message']['content'] = ($choices[$index]['message']['content'] ?? '') . $deltaText; @@ -245,12 +245,14 @@ private function parseSseChunk(string $chunk, string &$buffer, bool &$done, ?arr } /** - * Extract the text of typed content chunks, such as Mistral's `text`, `thinking` and - * `reference` parts. Parts with another type are skipped. + * Extract the text of typed content chunks. Only chunks with type `text` and a + * string `text` value contribute; `thinking`, `reference` and other types are skipped. + * + * This is shared with the non-streamed response path in OpenAiAPIService. * * @param array $chunks */ - private function contentChunksToText(array $chunks): string { + public function contentChunksToText(array $chunks): string { $text = ''; foreach ($chunks as $chunk) { if (is_array($chunk) && ($chunk['type'] ?? null) === 'text' && isset($chunk['text']) && is_string($chunk['text'])) { diff --git a/tests/unit/Service/StreamingServiceTest.php b/tests/unit/Service/StreamingServiceTest.php index b6f8afe2..df53a226 100644 --- a/tests/unit/Service/StreamingServiceTest.php +++ b/tests/unit/Service/StreamingServiceTest.php @@ -70,6 +70,63 @@ public function testTypedContentChunksAreAssembledAndStreamed(): void { ], $generator->getReturn()); } + public function testStringDeltasContinueAfterATypedContentChunk(): void { + $body = fopen('php://temp', 'r+'); + $this->assertIsResource($body); + fwrite($body, "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":[{\"type\":\"thinking\",\"thinking\":[{\"type\":\"text\",\"text\":\"Thinking\"}]},{\"type\":\"text\",\"text\":\"Hel\"}]}}]}\n\n" + . "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"lo\"}}]}\n\n" + . "data: {\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n" + . "data: [DONE]\n\n"); + rewind($body); + + $l10n = $this->createMock(IL10N::class); + $l10n->method('t')->willReturnArgument(0); + $service = new StreamingService($l10n); + $generator = $service->parseStreamChatResponse([ + 'body' => $body, + 'content-type' => 'text/event-stream', + ]); + + $this->assertSame([ + ['kind' => 'content', 'text' => 'Hel', 'index' => 0], + ['kind' => 'content', 'text' => 'lo', 'index' => 0], + ], iterator_to_array($generator, false)); + $this->assertSame([ + 'choices' => [[ + 'index' => 0, + 'message' => ['content' => 'Hello'], + 'finish_reason' => 'stop', + ]], + ], $generator->getReturn()); + } + + public function testEmptyContentArrayFallsBackToMessageContent(): void { + $body = fopen('php://temp', 'r+'); + $this->assertIsResource($body); + fwrite($body, "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":[]},\"message\":{\"content\":\"Fallback\"},\"finish_reason\":\"stop\"}]}\n\n" + . "data: [DONE]\n\n"); + rewind($body); + + $l10n = $this->createMock(IL10N::class); + $l10n->method('t')->willReturnArgument(0); + $service = new StreamingService($l10n); + $generator = $service->parseStreamChatResponse([ + 'body' => $body, + 'content-type' => 'text/event-stream', + ]); + + $this->assertSame([ + ['kind' => 'content', 'text' => 'Fallback', 'index' => 0], + ], iterator_to_array($generator, false)); + $this->assertSame([ + 'choices' => [[ + 'index' => 0, + 'message' => ['content' => 'Fallback'], + 'finish_reason' => 'stop', + ]], + ], $generator->getReturn()); + } + public function testDoneEventStopsReadingAnOpenStreamAndClosesIt(): void { $events = "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Hello\"}}]}\n\n" . "data: {\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n" From 865b6ee660d83e804e5f2fbc184c31a95c0c1240 Mon Sep 17 00:00:00 2001 From: Dick Tump Date: Sun, 15 Nov 2026 16:18:57 +0100 Subject: [PATCH 3/3] refactor: move typed content parsing into a static utility class Move contentChunksToText() out of StreamingService so the streamed and non-streamed paths do not depend on each other for it. Both now call ContentChunkParser::contentChunksToText(). Add a direct unit test for the parser. Assisted-by: ClaudeCode:deepseek-v4.1-flash Signed-off-by: Dick Tump --- lib/Service/ContentChunkParser.php | 29 +++++++++++++++ lib/Service/OpenAiAPIService.php | 2 +- lib/Service/StreamingService.php | 20 +--------- tests/unit/Service/ContentChunkParserTest.php | 37 +++++++++++++++++++ 4 files changed, 68 insertions(+), 20 deletions(-) create mode 100644 lib/Service/ContentChunkParser.php create mode 100644 tests/unit/Service/ContentChunkParserTest.php diff --git a/lib/Service/ContentChunkParser.php b/lib/Service/ContentChunkParser.php new file mode 100644 index 00000000..fed4cb61 --- /dev/null +++ b/lib/Service/ContentChunkParser.php @@ -0,0 +1,29 @@ + $chunks + * @return string + */ + public static function contentChunksToText(array $chunks): string { + $text = ''; + foreach ($chunks as $chunk) { + if (is_array($chunk) && ($chunk['type'] ?? null) === 'text' && isset($chunk['text']) && is_string($chunk['text'])) { + $text .= $chunk['text']; + } + } + return $text; + } +} diff --git a/lib/Service/OpenAiAPIService.php b/lib/Service/OpenAiAPIService.php index e397805a..ed2fceb5 100644 --- a/lib/Service/OpenAiAPIService.php +++ b/lib/Service/OpenAiAPIService.php @@ -1631,7 +1631,7 @@ private function normalizeChatCompletionResponse(array $response): array { $completions['messages'][] = $choice['message']['content']; } elseif (is_array($choice['message']['content'])) { // Handles more complex mistral message content (TODO: missing reasoning for now look at https://github.com/nextcloud/integration_openai/pull/409#discussion_r3638108824) - $completions['messages'][] = $this->streamingService->contentChunksToText($choice['message']['content']); + $completions['messages'][] = ContentChunkParser::contentChunksToText($choice['message']['content']); } } if (isset($choice['message']['reasoning_content']) && is_string($choice['message']['reasoning_content'])) { diff --git a/lib/Service/StreamingService.php b/lib/Service/StreamingService.php index 290d127f..bf37dfb2 100644 --- a/lib/Service/StreamingService.php +++ b/lib/Service/StreamingService.php @@ -147,7 +147,7 @@ private function parseSseEvent(string $event, bool &$done, ?array &$usage, array } if (isset($choice['delta']['content']) && is_array($choice['delta']['content']) && $choice['delta']['content'] !== []) { - $deltaText = $this->contentChunksToText($choice['delta']['content']); + $deltaText = ContentChunkParser::contentChunksToText($choice['delta']['content']); if ($deltaText !== '') { $choices[$index]['message']['content'] = ($choices[$index]['message']['content'] ?? '') . $deltaText; $partials[] = ['kind' => 'content', 'text' => $deltaText, 'index' => $index]; @@ -244,24 +244,6 @@ private function parseSseChunk(string $chunk, string &$buffer, bool &$done, ?arr return $partials; } - /** - * Extract the text of typed content chunks. Only chunks with type `text` and a - * string `text` value contribute; `thinking`, `reference` and other types are skipped. - * - * This is shared with the non-streamed response path in OpenAiAPIService. - * - * @param array $chunks - */ - public function contentChunksToText(array $chunks): string { - $text = ''; - foreach ($chunks as $chunk) { - if (is_array($chunk) && ($chunk['type'] ?? null) === 'text' && isset($chunk['text']) && is_string($chunk['text'])) { - $text .= $chunk['text']; - } - } - return $text; - } - /** * Merge fragmented `tool_calls` deltas from multiple SSE events into the accumulated * message structure for a single choice. diff --git a/tests/unit/Service/ContentChunkParserTest.php b/tests/unit/Service/ContentChunkParserTest.php new file mode 100644 index 00000000..49a71118 --- /dev/null +++ b/tests/unit/Service/ContentChunkParserTest.php @@ -0,0 +1,37 @@ +assertSame('Amsterdam. It is worth visiting.', ContentChunkParser::contentChunksToText([ + ['type' => 'text', 'text' => 'Amsterdam'], + ['type' => 'reference', 'reference_ids' => [1]], + ['type' => 'thinking', 'thinking' => [['type' => 'text', 'text' => 'Thinking']]], + ['type' => 'text', 'text' => '. It is worth visiting.'], + ])); + } + + public function testMalformedChunksAreSkipped(): void { + $this->assertSame('Hello', ContentChunkParser::contentChunksToText([ + 'not a chunk', + ['type' => 'text'], + ['type' => 'text', 'text' => 1], + ['type' => 'text', 'text' => 'Hello'], + ])); + } + + public function testEmptyChunksReturnAnEmptyString(): void { + $this->assertSame('', ContentChunkParser::contentChunksToText([])); + } +}