Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/agent/src/Toolbox/ToolResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +30,9 @@ public function __construct(
) {
}

/**
* @throws RuntimeException
*/
public function convert(ToolResult $toolResult): ?string
{
$result = $toolResult->getResult();
Expand All @@ -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);
}
}
}
12 changes: 10 additions & 2 deletions src/platform/src/StructuredOutput/ResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down