1111
1212namespace Symfony \AI \Platform \StructuredOutput ;
1313
14+ use Symfony \AI \Platform \Exception \RuntimeException ;
1415use Symfony \AI \Platform \Model ;
1516use Symfony \AI \Platform \Result \ObjectResult ;
1617use Symfony \AI \Platform \Result \RawResultInterface ;
1718use Symfony \AI \Platform \Result \ResultInterface ;
1819use Symfony \AI \Platform \Result \TextResult ;
1920use Symfony \AI \Platform \ResultConverterInterface ;
21+ use Symfony \Component \Serializer \Exception \ExceptionInterface as SerializerExceptionInterface ;
2022use Symfony \Component \Serializer \SerializerInterface ;
2123
2224final readonly class ResultConverter implements ResultConverterInterface
@@ -41,8 +43,14 @@ public function convert(RawResultInterface $result, array $options = []): Result
4143 return $ innerResult ;
4244 }
4345
44- $ structure = null === $ this ->outputClass ? json_decode ($ innerResult ->getContent (), true )
45- : $ this ->serializer ->deserialize ($ innerResult ->getContent (), $ this ->outputClass , 'json ' );
46+ try {
47+ $ structure = null === $ this ->outputClass ? json_decode ($ innerResult ->getContent (), true , flags: \JSON_THROW_ON_ERROR )
48+ : $ this ->serializer ->deserialize ($ innerResult ->getContent (), $ this ->outputClass , 'json ' );
49+ } catch (\JsonException $ e ) {
50+ throw new RuntimeException ('Cannot json decode the content. ' , previous: $ e );
51+ } catch (SerializerExceptionInterface $ e ) {
52+ throw new RuntimeException (\sprintf ('Cannot deserialize the content into the "%s" class. ' , $ this ->outputClass ), previous: $ e );
53+ }
4654
4755 $ objectResult = new ObjectResult ($ structure );
4856 $ objectResult ->setRawResult ($ result );
0 commit comments