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.

dict_utils helper to insert the fixed inputs at proper place in blueprint

Louis Choquel40e3a3aedf1c
diff --git a/pipelex/create/build_blueprint.py b/pipelex/create/build_blueprint.py
index 8308506a7..feb0b3573 100644
--- a/pipelex/create/build_blueprint.py
+++ b/pipelex/create/build_blueprint.py
@@ -1,6 +1,5 @@
from __future__ import annotations
-from pathlib import Path
from typing import Optional
from pipelex import log, pretty_print
@@ -39,9 +38,9 @@ async def do_build_blueprint(
output_path_base = output_path or "pipelex/libraries/pipelines/temp/gen_blueprint"
draft_path = f"{output_path_base}_draft.md"
save_text_to_path(text=pipeline_draft, path=draft_path)
- rough_toml_path = f"{output_path_base}.toml"
+ rough_toml_path = f"{output_path_base}_rough.toml"
save_pipeline_blueprint_toml_to_path(blueprint=blueprint, path=rough_toml_path)
- rough_json_path = f"{output_path_base}.json"
+ rough_json_path = f"{output_path_base}_rough.json"
save_as_json_to_path(object_to_save=blueprint, path=rough_json_path)
log.info(f"✅ Rough blueprint saved to '{output_path_base}'")
diff --git a/pipelex/create/validate_blueprint.py b/pipelex/create/validate_blueprint.py
index 6b7be0a8d..39fae3dbc 100644
--- a/pipelex/create/validate_blueprint.py
+++ b/pipelex/create/validate_blueprint.py
@@ -9,6 +9,7 @@ from pipelex.libraries.library_manager import LibraryManager
from pipelex.libraries.pipeline_blueprint import PipelineBlueprint
from pipelex.pipe_controllers.pipe_controller import PipeController
from pipelex.pipe_works.pipe_dry import dry_run_pipe_codes
+from pipelex.tools.misc.dict_utils import insert_before
async def validate_blueprint(blueprint: PipelineBlueprint, is_error_fixing_enabled: bool) -> None:
@@ -37,11 +38,20 @@ async def validate_blueprint(blueprint: PipelineBlueprint, is_error_fixing_enabl
if not required_concept_codes:
raise PipelexCLIError(f"No required concept codes found for static validation error: {static_validation_error}")
log.error(f"❌ Missing: {variable_names} / concepts: {required_concept_codes}")
+ new_inputs: Dict[str, Any] = blueprint.pipe[pipe_code].get("inputs") or {}
for variable_name, concept_code in zip(variable_names, required_concept_codes):
- if "inputs" not in blueprint.pipe[pipe_code]:
- blueprint.pipe[pipe_code]["inputs"] = {}
- blueprint.pipe[pipe_code]["inputs"][variable_name] = concept_code
+ new_inputs[variable_name] = concept_code
log.info(f"✅ Added: {variable_name} / {concept_code}")
+ if "inputs" in blueprint.pipe[pipe_code]:
+ blueprint.pipe[pipe_code]["inputs"] = new_inputs
+ else:
+ blueprint.pipe[pipe_code] = insert_before(
+ blueprint.pipe[pipe_code],
+ "output",
+ "inputs",
+ new_inputs,
+ )
+ log.dev(f"✅ Fixed missing inputs for pipe '{pipe_code}'")
await validate_blueprint(blueprint=blueprint, is_error_fixing_enabled=True)
else:
raise PipelexCLIError(f"Unexpected validation error:\n{static_validation_error}") from static_validation_error
diff --git a/pipelex/tools/misc/__init__.py b/pipelex/tools/misc/__init__.py
index 8b1378917..e69de29bb 100644
--- a/pipelex/tools/misc/__init__.py
+++ b/pipelex/tools/misc/__init__.py
@@ -1 +0,0 @@
-
diff --git a/pipelex/tools/misc/dict_utils.py b/pipelex/tools/misc/dict_utils.py
new file mode 100644
index 000000000..ac48cdb45
--- /dev/null
+++ b/pipelex/tools/misc/dict_utils.py
@@ -0,0 +1,43 @@
+"""Dictionary utility functions for manipulating dictionary order and structure."""
+
+from typing import Dict, TypeVar
+
+K = TypeVar("K")
+V = TypeVar("V")
+
+
+def insert_before(dictionary: Dict[K, V], target_key: K, new_key: K, new_value: V) -> Dict[K, V]:
+ """
+ Insert a new key-value pair before a target key in a dictionary.
+
+ Creates a new dictionary with the new item positioned before the target key.
+ If the target key doesn't exist, the new item is added at the end.
+
+ Args:
+ dictionary: The source dictionary
+ target_key: The key before which to insert the new item
+ new_key: The new key to insert
+ new_value: The new value to insert
+
+ Returns:
+ A new dictionary with the item inserted at the specified position
+
+ Example:
+ >>> d = {'a': 1, 'c': 3}
+ >>> insert_before(d, 'c', 'b', 2)
+ {'a': 1, 'b': 2, 'c': 3}
+ """
+ result: Dict[K, V] = {}
+ inserted = False
+
+ for key, value in dictionary.items():
+ if key == target_key and not inserted:
+ result[new_key] = new_value
+ inserted = True
+ result[key] = value
+
+ # If target key wasn't found, add at the end
+ if not inserted:
+ result[new_key] = new_value
+
+ return result
diff --git a/tests/unit/pipelex/tools/misc/test_dict_utils.py b/tests/unit/pipelex/tools/misc/test_dict_utils.py
new file mode 100644
index 000000000..e20ae33fb
--- /dev/null
+++ b/tests/unit/pipelex/tools/misc/test_dict_utils.py
@@ -0,0 +1,48 @@
+"""Unit tests for dict_utils module."""
+
+from pipelex.tools.misc.dict_utils import insert_before
+
+
+class TestDictUtils:
+ """Test class for dict_utils functions."""
+
+ def test_insert_before_basic(self) -> None:
+ """Test basic insert_before functionality."""
+ original = {"a": 1, "c": 3}
+ result = insert_before(original, "c", "b", 2)
+
+ expected_keys = ["a", "b", "c"]
+ assert list(result.keys()) == expected_keys
+ assert result["a"] == 1
+ assert result["b"] == 2
+ assert result["c"] == 3
+ assert original != result # Should return new dict, not modify original
+
+ def test_insert_before_target_not_found(self) -> None:
+ """Test insert_before when target key doesn't exist."""
+ original = {"a": 1, "b": 2}
+ result = insert_before(original, "z", "c", 3)
+
+ expected_keys = ["a", "b", "c"]
+ assert list(result.keys()) == expected_keys
+ assert result["c"] == 3
+
+ def test_preserve_original_dict(self) -> None:
+ """Test that original dictionary is not modified."""
+ original = {"a": 1, "b": 2, "c": 3}
+ original_copy = original.copy()
+
+ insert_before(original, "b", "x", 999)
+
+ assert original == original_copy
+
+ def test_complex_nested_structure(self) -> None:
+ """Test with complex nested dictionary structure."""
+ original = {"type": "PipeLLM", "definition": "Test pipe", "output": "Text", "system_prompt": "Test prompt"}
+
+ # Insert inputs before output
+ result = insert_before(original, "output", "inputs", "InputText")
+
+ expected_keys = ["type", "definition", "inputs", "output", "system_prompt"]
+ assert list(result.keys()) == expected_keys
+ assert result["inputs"] == "InputText"