From dff86faf0ec2c4d73cc1200664db412489284d62 Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Thu, 17 Apr 2025 11:41:05 -0400 Subject: [PATCH] feat: Handle lists of pydantic models when dumping to yaml Before the change, lists of models were not converted to dicts before passing them to ruamel representor. It broke `config show` when a `models` section was added to it because the field is s list of model_info classes. With this change, we should correctly handle tuples and lists. If we ever need something else, like dicts, we may expand the serialization code further. Signed-off-by: Ihar Hrachyshka --- src/instructlab/configuration.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/instructlab/configuration.py b/src/instructlab/configuration.py index ef72e6ec4f..bb5d5b80cc 100644 --- a/src/instructlab/configuration.py +++ b/src/instructlab/configuration.py @@ -33,7 +33,7 @@ model_validator, ) from pydantic_core import PydanticUndefined -from ruamel.yaml import YAML, CommentedMap +from ruamel.yaml import YAML, CommentedMap, CommentedSeq from typing_extensions import deprecated as Deprecated import click @@ -1205,8 +1205,20 @@ def config_to_commented_map( examples = field.examples default_factory = field.default_factory + # Recursively handle iterables + if isinstance(value, list | tuple): + # If the value is a list or tuple, handle each item + cm[field_name] = CommentedSeq() + for item in value: + if isinstance(item, BaseModel): + # If the item is a BaseModel, convert it to a CommentedMap + nested_cm = config_to_commented_map(item, indent + 2) + cm[field_name].append(nested_cm) + else: + cm[field_name].append(item) + # Recursively handle nested models - if isinstance(value, BaseModel): + elif isinstance(value, BaseModel): # If the value is a BaseModel but has Field attributes honor them set_comment( cm, field_name, description, default_value, deprecated, examples, indent