Skip to content
Open
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
36 changes: 27 additions & 9 deletions metagpt/utils/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,23 @@

import copy
import pickle
import re

from metagpt.utils.common import import_class


SCHEMA_TYPE_TO_PY_TYPE = {
"string": str,
"integer": int,
"number": float,
"boolean": bool,
"object": dict,
}

PY_TYPE_TO_MAPPING_STR = {py_type.__name__: py_type for py_type in SCHEMA_TYPE_TO_PY_TYPE.values()}
BUILTIN_TYPE_MAPPING_PATTERN = re.compile(r"^\(<class '(?P<name>[A-Za-z_][A-Za-z0-9_]*)'>, Ellipsis\)$")


def actionoutout_schema_to_mapping(schema: dict) -> dict:
"""
directly traverse the `properties` in the first level.
Expand All @@ -30,13 +43,16 @@ def actionoutout_schema_to_mapping(schema: dict) -> dict:
"""
mapping = dict()
for field, property in schema["properties"].items():
if property["type"] == "string":
mapping[field] = (str, ...)
elif property["type"] == "array" and property["items"]["type"] == "string":
mapping[field] = (list[str], ...)
elif property["type"] == "array" and property["items"]["type"] == "array":
# here only consider the `list[list[str]]` situation
mapping[field] = (list[list[str]], ...)
type_ = property.get("type")
if type_ == "array":
item_type = property.get("items", {}).get("type")
if item_type == "array":
# here only consider the `list[list[str]]` situation
mapping[field] = (list[list[str]], ...)
elif item_type in SCHEMA_TYPE_TO_PY_TYPE:
mapping[field] = (list[SCHEMA_TYPE_TO_PY_TYPE[item_type]], ...)
elif type_ in SCHEMA_TYPE_TO_PY_TYPE:
mapping[field] = (SCHEMA_TYPE_TO_PY_TYPE[type_], ...)
return mapping


Expand All @@ -50,8 +66,10 @@ def actionoutput_mapping_to_str(mapping: dict) -> dict:
def actionoutput_str_to_mapping(mapping: dict) -> dict:
new_mapping = {}
for key, value in mapping.items():
if value == "(<class 'str'>, Ellipsis)":
new_mapping[key] = (str, ...)
builtin_type = BUILTIN_TYPE_MAPPING_PATTERN.match(value)
# `"(<class 'str'>, Ellipsis)"` is not valid python syntax, so resolve builtin types by name
if builtin_type and builtin_type.group("name") in PY_TYPE_TO_MAPPING_STR:
new_mapping[key] = (PY_TYPE_TO_MAPPING_STR[builtin_type.group("name")], ...)
else:
new_mapping[key] = eval(value) # `"'(list[str], Ellipsis)"` to `(list[str], ...)`
return new_mapping
Expand Down
25 changes: 25 additions & 0 deletions tests/metagpt/utils/test_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,28 @@ def test_serialize_and_deserialize_message():
assert new_message.content == message.content
assert new_message.cause_by == message.cause_by
assert new_message.instruct_content.field1 == out_data["field1"]


def test_serialize_and_deserialize_message_with_non_string_fields():
out_mapping = {
"page_count": (int, ...),
"confidence": (float, ...),
"is_final": (bool, ...),
"payload": (dict, ...),
"summary": (str, ...),
}
out_data = {
"page_count": 7,
"confidence": 0.87,
"is_final": True,
"payload": {"key": "value"},
"summary": "prd demand",
}
ic_obj = ActionNode.create_model_class("plan", out_mapping)
message = Message(content="prd demand", instruct_content=ic_obj(**out_data))

new_message = deserialize_message(serialize_message(message))
assert new_message.instruct_content.model_dump() == out_data

restored_message = Message.model_validate_json(message.model_dump_json())
assert restored_message.instruct_content.model_dump() == out_data
Loading