Skip to content

Commit 7e8b3b4

Browse files
committed
feature #70 feat: allow JsonSerializable objects in contract (DZunke)
This PR was merged into the main branch. Discussion ---------- feat: allow JsonSerializable objects in contract | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Docs? | no | Issues | | License | MIT Cherry picking php-llm/llm-chain#377 Commits ------- 9dde800 feat: allow JsonSerializable objects in contract (#377)
2 parents fac418b + 9dde800 commit 7e8b3b4

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)