mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-29 15:59:42 +02:00
fixbug: Optional not working
This commit is contained in:
parent
0592b1f9e1
commit
e199c6b476
2 changed files with 31 additions and 5 deletions
|
|
@ -236,13 +236,27 @@ class ActionNode:
|
|||
def create_model_class(cls, class_name: str, mapping: Dict[str, Tuple[Type, Any]]):
|
||||
"""基于pydantic v2的模型动态生成,用来检验结果类型正确性"""
|
||||
|
||||
def is_optional_type(tp):
|
||||
if typing.get_origin(tp) is Union:
|
||||
args = typing.get_args(tp)
|
||||
non_none_types = [arg for arg in args if arg is not type(None)]
|
||||
return len(non_none_types) == 1 and len(args) == 2
|
||||
return False
|
||||
|
||||
def check_fields(cls, values):
|
||||
required_fields = set(mapping.keys())
|
||||
all_fields = set(mapping.keys())
|
||||
required_fields = set()
|
||||
for k, v in mapping.items():
|
||||
type_v, field_info = v
|
||||
if is_optional_type(type_v):
|
||||
continue
|
||||
required_fields.add(k)
|
||||
|
||||
missing_fields = required_fields - set(values.keys())
|
||||
if missing_fields:
|
||||
raise ValueError(f"Missing fields: {missing_fields}")
|
||||
|
||||
unrecognized_fields = set(values.keys()) - required_fields
|
||||
unrecognized_fields = set(values.keys()) - all_fields
|
||||
if unrecognized_fields:
|
||||
logger.warning(f"Unrecognized fields: {unrecognized_fields}")
|
||||
return values
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue