From 37df33c8037ea29f376f0d92eb5d0866ff6cf045 Mon Sep 17 00:00:00 2001 From: DZH166 <248446924+DZH166@users.noreply.github.com> Date: Sat, 21 Nov 2026 21:18:57 +0800 Subject: [PATCH] fix(utils): keep non-string instruct fields in Message JSON --- metagpt/utils/serialize.py | 36 ++++++++++++++++++++------- tests/metagpt/utils/test_serialize.py | 25 +++++++++++++++++++ 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/metagpt/utils/serialize.py b/metagpt/utils/serialize.py index c6bd8ad75f..fd0f3e26da 100644 --- a/metagpt/utils/serialize.py +++ b/metagpt/utils/serialize.py @@ -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"^\([A-Za-z_][A-Za-z0-9_]*)'>, Ellipsis\)$") + + def actionoutout_schema_to_mapping(schema: dict) -> dict: """ directly traverse the `properties` in the first level. @@ -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 @@ -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 == "(, Ellipsis)": - new_mapping[key] = (str, ...) + builtin_type = BUILTIN_TYPE_MAPPING_PATTERN.match(value) + # `"(, 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 diff --git a/tests/metagpt/utils/test_serialize.py b/tests/metagpt/utils/test_serialize.py index 0ba3a8d411..1736d49871 100644 --- a/tests/metagpt/utils/test_serialize.py +++ b/tests/metagpt/utils/test_serialize.py @@ -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