Skip to content

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.py
index d73b72b1e..6e9ab90da 100644
--- a/pipelex/create/build_blueprint.py
+++ b/pipelex/create/build_blueprint.py
@@ -9,7 +9,7 @@ from pydantic import ValidationError
from pipelex import pretty_print
from 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_path
from pipelex.exceptions import PipeDefinitionError, PipelexCLIError
from pipelex.hub import get_library_manager
from pipelex.libraries.library_manager import LibraryManager
diff --git a/pipelex/create/pipeline_toml.py b/pipelex/create/pipeline_toml.py
index 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, cast
import tomlkit
from 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]` section
else:
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.py
index 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, Optional
import toml
-import tomlkit
-from tomlkit import array, document, inline_table, table
from pipelex.tools.misc.file_utils import path_exists
-from pipelex.tools.misc.json_utils import remove_none_values_from_dict
class TOMLValidationError(Exception):