Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

feat: Allow any normalizable result from a tool #355

Merged
merged 1 commit into from
Jun 28, 2025
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
33 changes: 17 additions & 16 deletions src/Chain/Toolbox/ToolResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,33 @@

namespace PhpLlm\LlmChain\Chain\Toolbox;

use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @author Christopher Hertel <mail@christopher-hertel.de>
*/
final readonly class ToolResultConverter
{
/**
* @param \JsonSerializable|\Stringable|array<int|string, mixed>|float|string|null $result
*/
public function convert(\JsonSerializable|\Stringable|array|float|string|\DateTimeInterface|null $result): ?string
{
if (null === $result) {
return null;
}
public function __construct(
private SerializerInterface $serializer = new Serializer([new JsonSerializableNormalizer(), new DateTimeNormalizer(), new ObjectNormalizer()], [new JsonEncoder()]),
) {
}

if ($result instanceof \JsonSerializable || \is_array($result)) {
return json_encode($result, flags: \JSON_THROW_ON_ERROR);
public function convert(mixed $result): ?string
{
if (null === $result || \is_string($result)) {
return $result;
}

if (\is_float($result) || $result instanceof \Stringable) {
if ($result instanceof \Stringable) {
return (string) $result;
}

if ($result instanceof \DateTimeInterface) {
return $result->format(\DATE_ATOM);
}

return $result;
return $this->serializer->serialize($result, 'json');
}
}
14 changes: 13 additions & 1 deletion tests/Chain/Toolbox/ToolResultConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace PhpLlm\LlmChain\Tests\Chain\Toolbox;

use PhpLlm\LlmChain\Chain\Toolbox\ToolResultConverter;
use PhpLlm\LlmChain\Tests\Fixture\StructuredOutput\UserWithConstructor;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
Expand Down Expand Up @@ -34,7 +35,7 @@ public static function provideResults(): \Generator

yield 'string' => ['plain string', 'plain string'];

yield 'datetime' => [new \DateTimeImmutable('2021-07-31 12:34:56'), '2021-07-31T12:34:56+00:00'];
yield 'datetime' => [new \DateTimeImmutable('2021-07-31 12:34:56'), '"2021-07-31T12:34:56+00:00"'];

yield 'stringable' => [
new class implements \Stringable {
Expand All @@ -55,5 +56,16 @@ public function jsonSerialize(): array
},
'{"key":"value"}',
];

yield 'object' => [
new UserWithConstructor(
id: 123,
name: 'John Doe',
createdAt: new \DateTimeImmutable('2021-07-31 12:34:56'),
isActive: true,
age: 18,
),
'{"id":123,"name":"John Doe","createdAt":"2021-07-31T12:34:56+00:00","isActive":true,"age":18}',
];
}
}