From e2e00beb755bb10c73460cad2f19944567cbd4ea Mon Sep 17 00:00:00 2001 From: better629 Date: Tue, 9 Jan 2024 15:40:42 +0800 Subject: [PATCH] make instruct_content support any inherited basemodel ser&deser --- metagpt/schema.py | 25 ++++--- .../serialize_deserialize/test_schema.py | 68 +++++++++++++++---- .../test_serdeser_base.py | 10 +-- 3 files changed, 77 insertions(+), 26 deletions(-) diff --git a/metagpt/schema.py b/metagpt/schema.py index a557951c7..7d1c2b539 100644 --- a/metagpt/schema.py +++ b/metagpt/schema.py @@ -182,12 +182,16 @@ class Message(BaseModel): @field_validator("instruct_content", mode="before") @classmethod def check_instruct_content(cls, ic: Any) -> BaseModel: - if ic and not isinstance(ic, BaseModel) and "class" in ic: - # compatible with custom-defined ActionOutput - mapping = actionoutput_str_to_mapping(ic["mapping"]) - - actionnode_class = import_class("ActionNode", "metagpt.actions.action_node") # avoid circular import - ic_obj = actionnode_class.create_model_class(class_name=ic["class"], mapping=mapping) + if ic and isinstance(ic, dict) and "class" in ic: + if "mapping" in ic: + # compatible with custom-defined ActionOutput + mapping = actionoutput_str_to_mapping(ic["mapping"]) + actionnode_class = import_class("ActionNode", "metagpt.actions.action_node") # avoid circular import + ic_obj = actionnode_class.create_model_class(class_name=ic["class"], mapping=mapping) + elif "module" in ic: + ic_obj = import_class(ic["class"], ic["module"]) + else: + raise KeyError("missing required key to init Message.instruct_content from dict") ic = ic_obj(**ic["value"]) return ic @@ -212,13 +216,16 @@ class Message(BaseModel): if ic: # compatible with custom-defined ActionOutput schema = ic.model_json_schema() - # `Documents` contain definitions - if "definitions" not in schema: - # TODO refine with nested BaseModel + ic_type = str(type(ic)) + if "