Skip to content

Commit 9dde800

Browse files
DZunkechr-hertel
authored andcommitted
feat: allow JsonSerializable objects in contract (#377)
As brought up in #371 it could be a possible extension point to the contract to reintroduce the support for `JsonSerializable` objects when there are no normalizers for an object. So it would be a possible alternative for custom `MessageBagInterface`, `MessageInterface`, `ContentInterface`, etc. by allowing them to implement the `JsonSerializable` interface.
1 parent 7f0b662 commit 9dde800

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/platform/src/Contract.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Symfony\AI\Platform\Contract\Normalizer\ToolNormalizer;
2525
use Symfony\AI\Platform\Tool\Tool;
2626
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
27+
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
2728
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2829
use Symfony\Component\Serializer\Serializer;
2930

@@ -60,6 +61,9 @@ public static function create(NormalizerInterface ...$normalizer): self
6061
// Response
6162
$normalizer[] = new ToolCallNormalizer();
6263

64+
// JsonSerializable objects as extension point to library interfaces
65+
$normalizer[] = new JsonSerializableNormalizer();
66+
6367
return new self(
6468
new Serializer($normalizer),
6569
);

src/platform/tests/ContractTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@
3737
use Symfony\AI\Platform\Message\Content\ImageUrl;
3838
use Symfony\AI\Platform\Message\Message;
3939
use Symfony\AI\Platform\Message\MessageBag;
40+
use Symfony\AI\Platform\Message\MessageInterface;
41+
use Symfony\AI\Platform\Message\Role;
4042
use Symfony\AI\Platform\Message\SystemMessage;
4143
use Symfony\AI\Platform\Message\UserMessage;
4244
use Symfony\AI\Platform\Model;
45+
use Symfony\Component\Uid\Uuid;
4346

4447
#[Large]
4548
#[CoversClass(Contract::class)]
@@ -198,6 +201,37 @@ public static function providePayloadTestCases(): iterable
198201
'model' => 'gpt-4o',
199202
],
200203
];
204+
205+
$customSerializableMessage = new class implements MessageInterface, \JsonSerializable {
206+
public function getRole(): Role
207+
{
208+
return Role::User;
209+
}
210+
211+
public function getId(): Uuid
212+
{
213+
return Uuid::v7();
214+
}
215+
216+
public function jsonSerialize(): array
217+
{
218+
return [
219+
'role' => 'user',
220+
'content' => 'This is a custom serializable message.',
221+
];
222+
}
223+
};
224+
225+
yield 'MessageBag with custom message from GPT' => [
226+
'model' => new GPT(),
227+
'input' => new MessageBag($customSerializableMessage),
228+
'expected' => [
229+
'messages' => [
230+
['role' => 'user', 'content' => 'This is a custom serializable message.'],
231+
],
232+
'model' => 'gpt-4o',
233+
],
234+
];
201235
}
202236

203237
#[Test]

0 commit comments

Comments
 (0)