From e39437ad57a976de51043d99a9934dd056ca7f41 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Sat, 25 Oct 2025 00:43:08 +0200 Subject: [PATCH] Catch serializer exception --- src/agent/src/Toolbox/ToolResultConverter.php | 11 ++++++++++- .../src/StructuredOutput/ResultConverter.php | 12 ++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/agent/src/Toolbox/ToolResultConverter.php b/src/agent/src/Toolbox/ToolResultConverter.php index 8d839ed86..bab14798a 100644 --- a/src/agent/src/Toolbox/ToolResultConverter.php +++ b/src/agent/src/Toolbox/ToolResultConverter.php @@ -11,7 +11,9 @@ namespace Symfony\AI\Agent\Toolbox; +use Symfony\AI\Agent\Exception\RuntimeException; use Symfony\Component\Serializer\Encoder\JsonEncoder; +use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerExceptionInterface; use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; @@ -28,6 +30,9 @@ public function __construct( ) { } + /** + * @throws RuntimeException + */ public function convert(ToolResult $toolResult): ?string { $result = $toolResult->getResult(); @@ -40,6 +45,10 @@ public function convert(ToolResult $toolResult): ?string return (string) $result; } - return $this->serializer->serialize($result, 'json'); + try { + return $this->serializer->serialize($result, 'json'); + } catch (SerializerExceptionInterface $e) { + throw new RuntimeException('Cannot serialize the tool result.', previous: $e); + } } } diff --git a/src/platform/src/StructuredOutput/ResultConverter.php b/src/platform/src/StructuredOutput/ResultConverter.php index c6e8a07e3..a7222f5d8 100644 --- a/src/platform/src/StructuredOutput/ResultConverter.php +++ b/src/platform/src/StructuredOutput/ResultConverter.php @@ -11,12 +11,14 @@ namespace Symfony\AI\Platform\StructuredOutput; +use Symfony\AI\Platform\Exception\RuntimeException; use Symfony\AI\Platform\Model; use Symfony\AI\Platform\Result\ObjectResult; use Symfony\AI\Platform\Result\RawResultInterface; use Symfony\AI\Platform\Result\ResultInterface; use Symfony\AI\Platform\Result\TextResult; use Symfony\AI\Platform\ResultConverterInterface; +use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerExceptionInterface; use Symfony\Component\Serializer\SerializerInterface; final readonly class ResultConverter implements ResultConverterInterface @@ -41,8 +43,14 @@ public function convert(RawResultInterface $result, array $options = []): Result return $innerResult; } - $structure = null === $this->outputClass ? json_decode($innerResult->getContent(), true) - : $this->serializer->deserialize($innerResult->getContent(), $this->outputClass, 'json'); + try { + $structure = null === $this->outputClass ? json_decode($innerResult->getContent(), true, flags: \JSON_THROW_ON_ERROR) + : $this->serializer->deserialize($innerResult->getContent(), $this->outputClass, 'json'); + } catch (\JsonException $e) { + throw new RuntimeException('Cannot json decode the content.', previous: $e); + } catch (SerializerExceptionInterface $e) { + throw new RuntimeException(\sprintf('Cannot deserialize the content into the "%s" class.', $this->outputClass), previous: $e); + } $objectResult = new ObjectResult($structure); $objectResult->setRawResult($result);