diff --git a/evoagentx/models/base_model.py b/evoagentx/models/base_model.py index 1edf8099..fce3f1a7 100644 --- a/evoagentx/models/base_model.py +++ b/evoagentx/models/base_model.py @@ -493,12 +493,15 @@ def _parse_title_content(cls, content: str, title_format: str = "## {title}", ** if len(attr_types) == 0: return {} - output_titles = [title_format.format(title=attr) for attr in attr_types.keys()] + output_titles = [ + (title_format.format(title=attr), attr) + for attr in attr_types + ] def is_output_title(text: str): - for title in output_titles: + for title, attr in output_titles: if text.strip().lower().startswith(title.lower()): - return True, title + return True, attr return False, None def process_lines(lines: List[str], datatype: str): @@ -517,7 +520,7 @@ def process_lines(lines: List[str], datatype: str): current_attr_name: str = None current_attr_lines: list = None for line in content.split("\n"): - is_title, title = is_output_title(line) + is_title, attr_name = is_output_title(line) if is_title: if current_attr_name is not None and current_attr_lines is not None: # if we already have some content for a title, and now we reach a new title @@ -527,8 +530,7 @@ def process_lines(lines: List[str], datatype: str): # reset content for new title current_attr_lines = [] - current_attr_name = title.replace("#", "").strip() - output_titles.remove(title) + current_attr_name = attr_name else: if current_attr_lines is not None: current_attr_lines.append(line) diff --git a/tests/src/models/test_base_model.py b/tests/src/models/test_base_model.py new file mode 100644 index 00000000..502d2983 --- /dev/null +++ b/tests/src/models/test_base_model.py @@ -0,0 +1,28 @@ +from evoagentx.models.base_model import LLMOutputParser + + +class RepeatedHeadingOutput(LLMOutputParser): + count: int + enabled: bool + label: str + + +def test_title_parser_uses_latest_value_for_repeated_headings(): + content = """## count +1 +## count +2 +## enabled +false +## enabled +true +## label +alpha +## label +beta""" + + result = RepeatedHeadingOutput.parse(content, parse_mode="title") + + assert result.count == 2 + assert result.enabled is True + assert result.label == "beta"