Skip to content

Commit ffeeb5f

Browse files
committed
minor #813 Catch serializer exception (VincentLanglet)
This PR was squashed before being merged into the main branch. Discussion ---------- Catch serializer exception | Q | A | ------------- | --- | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Docs? | no <!-- required for new features --> | Issues | Fix #786 | License | MIT I don't really have inspiration for error message, I'm pretty sure you'll have good suggestion `@OskarStark` Commits ------- e39437a Catch serializer exception
2 parents c4a9e4e + e39437a commit ffeeb5f

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/agent/src/Toolbox/ToolResultConverter.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\AI\Agent\Toolbox;
1313

14+
use Symfony\AI\Agent\Exception\RuntimeException;
1415
use Symfony\Component\Serializer\Encoder\JsonEncoder;
16+
use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerExceptionInterface;
1517
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
1618
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
1719
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
@@ -28,6 +30,9 @@ public function __construct(
2830
) {
2931
}
3032

33+
/**
34+
* @throws RuntimeException
35+
*/
3136
public function convert(ToolResult $toolResult): ?string
3237
{
3338
$result = $toolResult->getResult();
@@ -40,6 +45,10 @@ public function convert(ToolResult $toolResult): ?string
4045
return (string) $result;
4146
}
4247

43-
return $this->serializer->serialize($result, 'json');
48+
try {
49+
return $this->serializer->serialize($result, 'json');
50+
} catch (SerializerExceptionInterface $e) {
51+
throw new RuntimeException('Cannot serialize the tool result.', previous: $e);
52+
}
4453
}
4554
}

src/platform/src/StructuredOutput/ResultConverter.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111

1212
namespace Symfony\AI\Platform\StructuredOutput;
1313

14+
use Symfony\AI\Platform\Exception\RuntimeException;
1415
use Symfony\AI\Platform\Model;
1516
use Symfony\AI\Platform\Result\ObjectResult;
1617
use Symfony\AI\Platform\Result\RawResultInterface;
1718
use Symfony\AI\Platform\Result\ResultInterface;
1819
use Symfony\AI\Platform\Result\TextResult;
1920
use Symfony\AI\Platform\ResultConverterInterface;
21+
use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerExceptionInterface;
2022
use Symfony\Component\Serializer\SerializerInterface;
2123

2224
final 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

Comments
 (0)