Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.
Better TOML style
Louis Choquel05112205bc0f
diff --git a/pipelex/create/build_blueprint.py b/pipelex/create/build_blueprint.pyindex d73b72b1e..6e9ab90da 100644--- a/pipelex/create/build_blueprint.py+++ b/pipelex/create/build_blueprint.py@@ -9,7 +9,7 @@ from pydantic import ValidationErrorfrom pipelex import pretty_printfrom pipelex.create.helpers import get_pipeline_creation_rules-from pipelex.create.pipeline_toml import save_pipeline_blueprint_toml_to_path, save_toml_to_path+from pipelex.create.pipeline_toml import save_pipeline_blueprint_toml_to_pathfrom pipelex.exceptions import PipeDefinitionError, PipelexCLIErrorfrom pipelex.hub import get_library_managerfrom pipelex.libraries.library_manager import LibraryManagerdiff --git a/pipelex/create/pipeline_toml.py b/pipelex/create/pipeline_toml.pyindex 6f97c2ad6..947b7690f 100644--- a/pipelex/create/pipeline_toml.py+++ b/pipelex/create/pipeline_toml.py@@ -1,6 +1,6 @@from __future__ import annotations-from typing import Any, Dict, List, Mapping, Optional, cast+from typing import Any, Dict, List, Mapping, castimport tomlkitfrom tomlkit import array, document, inline_table, table@@ -74,8 +74,25 @@ def _convert_to_inline(value: Any):return value+def _convert_mapping_to_table(mapping: Mapping[str, Any]) -> Any:+ """Convert a mapping into a TOML Table where any nested mappings (third level+)+ are converted to inline tables.++ This creates a second-level standard table, and only uses inline tables for+ third level and deeper structures.+ """+ tbl = table()+ for field_key, field_value in mapping.items():+ if isinstance(field_value, Mapping):+ # Third-level mapping -> inline table+ tbl.add(field_key, _convert_to_inline(field_value))+ else:+ tbl.add(field_key, _convert_to_inline(field_value))+ return tbl++def dict_to_toml(data: Mapping[str, Any]) -> str:- """Top-level keys become tables; nested dicts become inline tables."""+ """Top-level keys become tables; second-level mappings become tables; inline tables start at third level."""data = remove_none_values_from_dict(data=data)document_root = document()for section_key, section_value in data.items():@@ -83,7 +100,12 @@ def dict_to_toml(data: Mapping[str, Any]) -> str:section_value = cast(Mapping[str, Any], section_value)table_obj = table()for field_key, field_value in section_value.items():- table_obj.add(field_key, _convert_to_inline(field_value))+ if isinstance(field_value, Mapping):+ # Second level mapping -> standard table [section.field]+ sub_table = _convert_mapping_to_table(cast(Mapping[str, Any], field_value))+ table_obj.add(field_key, sub_table)+ else:+ table_obj.add(field_key, _convert_to_inline(field_value))document_root.add(section_key, table_obj) # `[section_key]` sectionelse:document_root.add(section_key, _convert_to_inline(section_value))diff --git a/pipelex/tools/misc/toml_utils.py b/pipelex/tools/misc/toml_utils.pyindex f8c486f1b..4dd7d9a79 100644--- a/pipelex/tools/misc/toml_utils.py+++ b/pipelex/tools/misc/toml_utils.py@@ -1,13 +1,10 @@from __future__ import annotations-from typing import Any, Dict, List, Mapping, Optional, cast+from typing import Any, Dict, List, Optionalimport toml-import tomlkit-from tomlkit import array, document, inline_table, tablefrom pipelex.tools.misc.file_utils import path_exists-from pipelex.tools.misc.json_utils import remove_none_values_from_dictclass TOMLValidationError(Exception):