Skip to content

Commit d4a3eed

Browse files
committed
fix(serialization): update from_plain calls to use new signature
Update all from_plain usages to pass factory ref as second parameter: - Message class registration in message.py - Custom external example - Test for custom registration Signed-off-by: Sakeeb91 <rahman.sakeeb@gmail.com>
1 parent 404a053 commit d4a3eed

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

python/beeai_framework/backend/message.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ def _to_plain(message: Message[Any]) -> dict[str, Any]:
391391
"content": [fragment.model_dump() for fragment in message.content],
392392
}
393393

394-
def _from_plain(payload: Mapping[str, Any]) -> Message[Any]:
394+
def _from_plain(payload: Mapping[str, Any], ref: type[Any]) -> Any:
395395
content_payload = payload.get("content", []) or []
396396
content_models: list[Any] = []
397397

@@ -408,13 +408,13 @@ def _from_plain(payload: Mapping[str, Any]) -> Message[Any]:
408408
model_cls = allowed_content.get(content_type)
409409
if model_cls is None:
410410
raise SerializerError(
411-
f"Unsupported message content '{content_type}' for {message_cls.__name__}.",
411+
f"Unsupported message content '{content_type}' for {ref.__name__}.",
412412
)
413413
content_models.append(model_cls.model_validate(dict(item)))
414414

415415
meta = payload.get("meta") or {}
416416
meta_copy = dict(meta)
417-
return message_cls(content_models, meta=meta_copy, id=payload.get("id"))
417+
return ref(content_models, meta=meta_copy, id=payload.get("id"))
418418

419419
Serializer.register(
420420
message_cls,

python/examples/serialization/custom_external.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ApiToken:
1919
"value": token.value,
2020
"expires_at": token.expires_at,
2121
},
22-
from_plain=lambda payload: ApiToken(
22+
from_plain=lambda payload, ref: ref(
2323
value=payload["value"],
2424
expires_at=payload["expires_at"],
2525
),

python/tests/serialization/test_serializer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ApiToken:
4848
"value": token.value,
4949
"expires_at": token.expires_at,
5050
},
51-
from_plain=lambda payload: ApiToken(
51+
from_plain=lambda payload, ref: ref(
5252
value=payload["value"],
5353
expires_at=payload["expires_at"],
5454
),

0 commit comments

Comments
 (0)