Skip to content

Commit 8d7970f

Browse files
authored
fix: JsonException on last chunk for Symfony 6.4 (#153)
1 parent 0aa5c0b commit 8d7970f

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/Bridge/Anthropic/ModelHandler.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpLlm\LlmChain\Platform\ResponseConverter;
1414
use Symfony\Component\HttpClient\Chunk\ServerSentEvent;
1515
use Symfony\Component\HttpClient\EventSourceHttpClient;
16+
use Symfony\Component\HttpClient\Exception\JsonException;
1617
use Symfony\Contracts\HttpClient\HttpClientInterface;
1718
use Symfony\Contracts\HttpClient\ResponseInterface;
1819
use Webmozart\Assert\Assert;
@@ -72,7 +73,12 @@ private function convertStream(ResponseInterface $response): \Generator
7273
continue;
7374
}
7475

75-
$data = $chunk->getArrayData();
76+
try {
77+
$data = $chunk->getArrayData();
78+
} catch (JsonException) {
79+
// try catch only needed for Symfony 6.4
80+
continue;
81+
}
7682

7783
if ('content_block_delta' != $data['type'] || !isset($data['delta']['text'])) {
7884
continue;

src/Bridge/OpenAI/GPT/ResponseConverter.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PhpLlm\LlmChain\Platform\ResponseConverter as PlatformResponseConverter;
1818
use Symfony\Component\HttpClient\Chunk\ServerSentEvent;
1919
use Symfony\Component\HttpClient\EventSourceHttpClient;
20+
use Symfony\Component\HttpClient\Exception\JsonException;
2021
use Symfony\Contracts\HttpClient\ResponseInterface as HttpResponse;
2122

2223
final class ResponseConverter implements PlatformResponseConverter
@@ -70,7 +71,12 @@ private function streamResponse(HttpResponse $response): \Generator
7071
continue;
7172
}
7273

73-
yield $chunk->getArrayData();
74+
try {
75+
yield $chunk->getArrayData();
76+
} catch (JsonException) {
77+
// try catch only needed for Symfony 6.4
78+
continue;
79+
}
7480
}
7581
}
7682

0 commit comments

Comments
 (0)