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.

demo -> dev (#319)

Thomas Hebrard5777bf63d72e
diff --git a/.badges/tests.json b/.badges/tests.json
index 9172db5a6..04f36c790 100644
--- a/.badges/tests.json
+++ b/.badges/tests.json
@@ -1,7 +1,7 @@
{
"schemaVersion": 1,
"label": "tests",
- "message": "1081",
+ "message": "1095",
"color": "blue",
"cacheSeconds": 300
}
\ No newline at end of file
diff --git a/pipelex/cli/commands/build_cmd.py b/pipelex/cli/commands/build_cmd.py
index f4c8dd889..7036f43dd 100644
--- a/pipelex/cli/commands/build_cmd.py
+++ b/pipelex/cli/commands/build_cmd.py
@@ -9,6 +9,7 @@ from pipelex.hub import get_report_delegate
from pipelex.language.plx_factory import PlxFactory
from pipelex.libraries.pipelines.builder.builder import PipelexBundleSpec
from pipelex.libraries.pipelines.builder.builder_loop import BuilderLoop
+from pipelex.libraries.pipelines.builder.flow_factory import FlowFactory
from pipelex.pipelex import Pipelex
from pipelex.pipeline.execute import execute_pipeline
from pipelex.tools.misc.file_utils import ensure_directory_for_file_path, save_text_to_path
@@ -19,7 +20,9 @@ build_app = typer.Typer(help="Build artifacts like pipelines", no_args_is_help=T
"""
Today's example:
pipelex build pipe "Given an expense report, apply company rules"
+
pipelex build partial "Given an expense report, apply company rules" -o results/generated.json
+pipelex build flow "Given an expense report, apply company rules" -o results/flow.json
Other ideas:
pipelex build pipe "Take a photo as input, and render the opposite of the photo"
@@ -175,3 +178,54 @@ def build_partial_cmd(
typer.echo(typer.style(f"\n✅ Pipeline built in {end_time - start_time:.2f} seconds", fg=typer.colors.GREEN))
get_report_delegate().generate_report()
+
+
+@build_app.command("flow")
+def build_flow_cmd(
+ brief: Annotated[
+ str,
+ typer.Argument(help="Brief description of what the pipeline should do"),
+ ],
+ output_path: Annotated[
+ str,
+ typer.Option("--output", "-o", help="Path to save the generated PLX file"),
+ ] = "./results/generated_pipeline.plx",
+ no_output: Annotated[
+ bool,
+ typer.Option("--no-output", help="Skip saving the pipeline to file"),
+ ] = False,
+) -> None:
+ Pipelex.make(relative_config_folder_path="../../../pipelex/libraries", from_file=True)
+ typer.echo("=" * 70)
+ typer.echo(typer.style("🔥 Starting pipe builder... 🚀", fg=typer.colors.GREEN))
+ typer.echo("")
+
+ async def run_pipeline():
+ if no_output:
+ typer.echo(typer.style("\n⚠️ Pipeline will not be saved to file (--no-output specified)", fg=typer.colors.YELLOW))
+ elif not output_path:
+ typer.echo(typer.style("\n🛑 Cannot save a pipeline to an empty file name", fg=typer.colors.RED))
+ raise typer.Exit(1)
+ else:
+ ensure_directory_for_file_path(file_path=output_path)
+
+ pipe_output = await execute_pipeline(
+ pipe_code="pipe_builder",
+ input_memory={"brief": brief},
+ )
+ # Save to file unless explicitly disabled with --no-output
+ if no_output:
+ typer.echo(typer.style("\n⚠️ Pipeline not saved to file (--no-output specified)", fg=typer.colors.YELLOW))
+ return
+ pipelex_bundle_spec = pipe_output.main_stuff_as(content_type=PipelexBundleSpec)
+ flow = FlowFactory.make_from_bundle_spec(bundle_spec=pipelex_bundle_spec)
+ json_output = flow.smart_dump()
+ save_as_json_to_path(object_to_save=json_output, path=output_path)
+ typer.echo(typer.style(f"\n✅ Pipeline saved to: {output_path}", fg=typer.colors.GREEN))
+
+ start_time = time.time()
+ asyncio.run(run_pipeline())
+ end_time = time.time()
+ typer.echo(typer.style(f"\n✅ Pipeline built in {end_time - start_time:.2f} seconds", fg=typer.colors.GREEN))
+
+ get_report_delegate().generate_report()
diff --git a/pipelex/core/bundles/pipe_sorter.py b/pipelex/core/bundles/pipe_sorter.py
new file mode 100644
index 000000000..594ed3372
--- /dev/null
+++ b/pipelex/core/bundles/pipe_sorter.py
@@ -0,0 +1,89 @@
+"""Topological sorting utilities for pipe dependencies."""
+
+from pipelex.core.bundles.pipelex_bundle_blueprint import PipeBlueprintUnion
+from pipelex.exceptions import PipeDefinitionError
+
+
+def sort_pipes_by_dependencies(
+ pipes: dict[str, PipeBlueprintUnion],
+) -> list[tuple[str, PipeBlueprintUnion]]:
+ """Sort pipes by their dependencies using depth-first pre-order traversal.
+
+ This performs a depth-first traversal where controllers appear before their dependencies,
+ and dependencies are visited in the order specified by the controller (e.g., step order
+ for PipeSequence). This ordering is intuitive for understanding pipeline structure.
+
+ Args:
+ pipes: Dictionary mapping pipe_code to PipeBlueprintUnion
+
+ Returns:
+ List of (pipe_code, pipe_blueprint) tuples sorted with controllers before dependencies.
+ For PipeSequence, dependencies follow step order. For others, alphabetical order.
+
+ Raises:
+ PipeDefinitionError: If circular dependencies are detected among pipes
+
+ Example:
+ >>> pipes = {
+ ... "pipe_c": pipe_c_blueprint, # sequence with steps: pipe_a, pipe_b
+ ... "pipe_a": pipe_a_blueprint, # no dependencies
+ ... "pipe_b": pipe_b_blueprint, # no dependencies
+ ... }
+ >>> sorted_pipes = sort_pipes_by_dependencies(pipes)
+ >>> [code for code, _ in sorted_pipes]
+ ['pipe_c', 'pipe_a', 'pipe_b']
+ """
+ # Find root pipes (those not depended upon by anyone)
+ all_dependencies: set[str] = set()
+ for pipe_blueprint in pipes.values():
+ all_dependencies.update(pipe_blueprint.pipe_dependencies)
+
+ root_pipes = [code for code in pipes if code not in all_dependencies]
+
+ # Depth-first traversal to order pipes
+ visited: set[str] = set()
+ visiting: set[str] = set() # For cycle detection
+ sorted_pipes: list[tuple[str, PipeBlueprintUnion]] = []
+
+ def visit(pipe_code: str) -> None:
+ if pipe_code in visited:
+ return
+ if pipe_code in visiting:
+ msg = f"Circular dependency detected involving pipe: {pipe_code}"
+ raise PipeDefinitionError(message=msg)
+ if pipe_code not in pipes:
+ # Dependency not in this bundle, skip it
+ return
+
+ visiting.add(pipe_code)
+ pipe_blueprint = pipes[pipe_code]
+
+ # Add current pipe first (pre-order)
+ sorted_pipes.append((pipe_code, pipe_blueprint))
+
+ # Visit dependencies in order
+ ordered_deps = pipe_blueprint.ordered_pipe_dependencies
+ if ordered_deps:
+ # Use ordered dependencies (e.g., PipeSequence steps)
+ for dep_code in ordered_deps:
+ if dep_code in pipes:
+ visit(dep_code)
+ else:
+ # Use alphabetical order for determinism
+ for dep_code in sorted(pipe_blueprint.pipe_dependencies):
+ if dep_code in pipes:
+ visit(dep_code)
+
+ visited.add(pipe_code)
+ visiting.remove(pipe_code)
+
+ # Start traversal from root pipes (sorted for determinism)
+ for pipe_code in sorted(root_pipes):
+ visit(pipe_code)
+
+ # Ensure all pipes are included (handle disconnected components)
+ for pipe_code in sorted(pipes.keys()):
+ if pipe_code not in visited:
+ visit(pipe_code)
+
+ return sorted_pipes
diff --git a/pipelex/core/pipes/pipe_blueprint.py b/pipelex/core/pipes/pipe_blueprint.py
index b63ee8a04..f4f5d72e8 100644
--- a/pipelex/core/pipes/pipe_blueprint.py
+++ b/pipelex/core/pipes/pipe_blueprint.py
@@ -61,6 +61,30 @@ class PipeBlueprint(BaseModel):
inputs: dict[str, str | InputRequirementBlueprint] | None = None
output: str
+ @property
+ def pipe_dependencies(self) -> set[str]:
+ """Return the set of pipe codes that this pipe depends on.
+
+ This is overridden by PipeController subclasses to return their dependencies.
+ PipeOperators have no dependencies, so return an empty set.
+
+ Returns:
+ Set of pipe codes this pipe depends on
+ """
+ return set()
+
+ @property
+ def ordered_pipe_dependencies(self) -> list[str] | None:
+ """Return ordered dependencies if order matters (e.g., for PipeSequence steps).
+
+ This is overridden by controllers where dependency order is significant,
+ such as PipeSequence where steps should be processed in order.
+
+ Returns:
+ Ordered list of pipe codes if order matters, None otherwise
+ """
+ return None
+
@field_validator("type", mode="after")
@staticmethod
def validate_pipe_type(value: Any) -> Any:
diff --git a/pipelex/libraries/pipelines/builder/builder.py b/pipelex/libraries/pipelines/builder/builder.py
index 7822cff0b..957df1c67 100644
--- a/pipelex/libraries/pipelines/builder/builder.py
+++ b/pipelex/libraries/pipelines/builder/builder.py
@@ -2,6 +2,7 @@ from typing import Annotated, cast
from pydantic import ConfigDict, Field, ValidationError, field_validator
+from pipelex.core.bundles.pipe_sorter import sort_pipes_by_dependencies
from pipelex.core.bundles.pipelex_bundle_blueprint import PipeBlueprintUnion, PipelexBundleBlueprint
from pipelex.core.concepts.concept_blueprint import ConceptBlueprint
from pipelex.core.domains.domain_blueprint import DomainBlueprint
@@ -128,15 +129,26 @@ class PipelexBundleSpec(StructuredContent):
pipe: dict[str, PipeBlueprintUnion] | None = None
if self.pipe:
- pipe = {}
+ # First, convert all specs to blueprints
+ pipe_blueprints: dict[str, PipeBlueprintUnion] = {}
for pipe_code, pipe_spec in self.pipe.items():
try:
- pipe[pipe_code] = pipe_spec.to_blueprint()
+ pipe_blueprints[pipe_code] = pipe_spec.to_blueprint()
except ValidationError as exc:
msg = f"Failed to create pipe blueprint from spec for pipe code {pipe_code}: {format_pydantic_validation_error(exc)}"
pipe_failure = PipeFailure(pipe_spec=pipe_spec, error_message=msg)
raise PipeSpecError(message=msg, pipe_failure=pipe_failure) from exc
+ # Then, sort blueprints by dependencies
+ try:
+ sorted_pipe_items = sort_pipes_by_dependencies(pipe_blueprints)
+ except Exception as exc:
+ msg = f"Failed to sort pipes by dependencies: {exc}"
+ raise PipeBuilderError(msg) from exc
+
+ # Finally, create the ordered dict
+ pipe = dict(sorted_pipe_items)
+
return PipelexBundleBlueprint(
domain=self.domain,
description=self.description,
diff --git a/pipelex/libraries/pipelines/builder/flow.py b/pipelex/libraries/pipelines/builder/flow.py
new file mode 100644
index 000000000..d59e3e8da
--- /dev/null
+++ b/pipelex/libraries/pipelines/builder/flow.py
@@ -0,0 +1,38 @@
+from pydantic import ConfigDict, Field
+
+from pipelex.core.stuffs.stuff_content import StructuredContent
+from pipelex.libraries.pipelines.builder.pipe.pipe_signature import PipeSignature
+from pipelex.pipe_controllers.batch.pipe_batch_blueprint import PipeBatchBlueprint
+from pipelex.pipe_controllers.condition.pipe_condition_blueprint import PipeConditionBlueprint
+from pipelex.pipe_controllers.parallel.pipe_parallel_blueprint import PipeParallelBlueprint
+from pipelex.pipe_controllers.sequence.pipe_sequence_blueprint import PipeSequenceBlueprint
+
+# Union of possible pipe representations in flow view
+# Controllers keep their full blueprint, operators are converted to signatures
+FlowElementUnion = PipeSignature | PipeBatchBlueprint | PipeConditionBlueprint | PipeParallelBlueprint | PipeSequenceBlueprint
+
+
+class Flow(StructuredContent):
+ """Simplified view of a pipeline's flow structure.
+
+ This class provides a high-level overview of a pipeline's flow without
+ implementation details. It shows:
+ - Domain and description
+ - Pipe controllers (sequence, parallel, condition, batch) with their full structure
+ - Pipe operators (LLM, func, img_gen, compose, ocr) as signatures only
+
+ This representation is useful for understanding the overall workflow and
+ dependencies without getting into implementation specifics.
+
+ Attributes:
+ domain: The domain identifier for this pipeline in snake_case format.
+ description: Natural language description of the pipeline's purpose.
+ pipes: Dictionary mapping pipe codes to their specifications.
+ Controllers include full details, operators are simplified to signatures.
+ """
+
+ model_config = ConfigDict(extra="forbid")
+
+ domain: str
+ description: str | None = None
+ flow_elements: dict[str, FlowElementUnion] = Field(default_factory=dict)
diff --git a/pipelex/libraries/pipelines/builder/flow_factory.py b/pipelex/libraries/pipelines/builder/flow_factory.py
new file mode 100644
index 000000000..f70701270
--- /dev/null
+++ b/pipelex/libraries/pipelines/builder/flow_factory.py
@@ -0,0 +1,112 @@
+from pathlib import Path
+from typing import Any
+
+from pipelex.core.bundles.pipelex_bundle_blueprint import PipelexBundleBlueprint
+from pipelex.core.interpreter import PipelexInterpreter
+from pipelex.core.pipes.pipe_blueprint import AllowedPipeCategories
+from pipelex.libraries.pipelines.builder.builder import PipelexBundleSpec
+from pipelex.libraries.pipelines.builder.flow import Flow, FlowElementUnion
+from pipelex.libraries.pipelines.builder.pipe.pipe_signature import PipeSignature
+from pipelex.pipe_controllers.batch.pipe_batch_blueprint import PipeBatchBlueprint
+from pipelex.pipe_controllers.condition.pipe_condition_blueprint import PipeConditionBlueprint
+from pipelex.pipe_controllers.parallel.pipe_parallel_blueprint import PipeParallelBlueprint
+from pipelex.pipe_controllers.sequence.pipe_sequence_blueprint import PipeSequenceBlueprint
+
+
+class FlowFactory:
+ """Factory for creating Flow from PipelexBundleSpec or PLX files.
+
+ Converts a complete bundle specification into a simplified flow view
+ by keeping pipe controllers as-is and converting pipe operators to signatures.
+ """
+
+ @staticmethod
+ def make_from_plx_file(plx_file_path: Path | str) -> Flow:
+ """Create Flow from a PLX file.
+
+ Args:
+ plx_file_path: Path to the PLX file to load.
+
+ Returns:
+ Flow with controllers preserved and operators as signatures.
+ """
+ plx_path = Path(plx_file_path) if isinstance(plx_file_path, str) else plx_file_path
+ bundle_blueprint = PipelexInterpreter(file_path=plx_path).make_pipelex_bundle_blueprint()
+ return FlowFactory.make_from_bundle_blueprint(bundle_blueprint)
+
+ @staticmethod
+ def make_from_bundle_blueprint(bundle_blueprint: PipelexBundleBlueprint) -> Flow:
+ """Convert a PipelexBundleBlueprint to a Flow.
+
+ Args:
+ bundle_blueprint: The bundle blueprint to convert.
+
+ Returns:
+ Flow with controllers preserved and operators as signatures.
+ """
+ flow_elements: dict[str, FlowElementUnion] = {}
+
+ if bundle_blueprint.pipe:
+ for pipe_code, pipe_blueprint in bundle_blueprint.pipe.items():
+ if pipe_blueprint.category == AllowedPipeCategories.PIPE_CONTROLLER:
+ # Keep controllers as-is (they are already blueprints which match spec structure)
+ # Type check to ensure we only assign controller blueprints
+ if isinstance(
+ pipe_blueprint,
+ PipeBatchBlueprint | PipeConditionBlueprint | PipeParallelBlueprint | PipeSequenceBlueprint,
+ ): # pyright: ignore[reportUnnecessaryIsInstance]
+ flow_elements[pipe_code] = pipe_blueprint
+ else:
+ # Convert operators to signatures
+ flow_elements[pipe_code] = FlowFactory._convert_blueprint_to_signature(pipe_code, pipe_blueprint)
+
+ return Flow(
+ domain=bundle_blueprint.domain,
+ description=bundle_blueprint.description,
+ flow_elements=flow_elements,
+ )
+
+ @staticmethod
+ def _convert_blueprint_to_signature(pipe_code: str, pipe_blueprint: Any) -> PipeSignature:
+ """Convert a pipe blueprint to a pipe signature.
+
+ Args:
+ pipe_code: The code identifying the pipe.
+ pipe_blueprint: The pipe blueprint to convert.
+
+ Returns:
+ PipeSignature containing the contract information.
+ """
+ # Extract inputs as strings from InputRequirementBlueprint
+ inputs: dict[str, str] = {}
+ if pipe_blueprint.inputs:
+ for input_name, input_requirement in pipe_blueprint.inputs.items():
+ if hasattr(input_requirement, "concept"):
+ inputs[input_name] = input_requirement.concept
+ else:
+ inputs[input_name] = str(input_requirement)
+
+ return PipeSignature(
+ code=pipe_code,
+ category="PipeSignature",
+ type=pipe_blueprint.type,
+ description=pipe_blueprint.description or "",
+ inputs=inputs,
+ result=pipe_code,
+ output=pipe_blueprint.output,
+ pipe_dependencies=[],
+ )
+
+ @staticmethod
+ def make_from_bundle_spec(bundle_spec: PipelexBundleSpec) -> Flow:
+ """Convert a PipelexBundleSpec to a Flow.
+
+ Args:
+ bundle_spec: The complete bundle specification to convert.
+
+ Returns:
+ Flow with controllers preserved and operators as signatures.
+ """
+ # Convert the spec to blueprint first, then use the blueprint converter
+ bundle_blueprint = bundle_spec.to_blueprint()
+ return FlowFactory.make_from_bundle_blueprint(bundle_blueprint)
diff --git a/pipelex/libraries/pipelines/builder/pipe/pipe_llm_spec.py b/pipelex/libraries/pipelines/builder/pipe/pipe_llm_spec.py
index b360d286c..9412afce8 100644
--- a/pipelex/libraries/pipelines/builder/pipe/pipe_llm_spec.py
+++ b/pipelex/libraries/pipelines/builder/pipe/pipe_llm_spec.py
@@ -19,7 +19,6 @@ class AvailableLLM(StrEnum):
CLAUDE_4_SONNET = "claude-4-sonnet"
CLAUDE_4_1_OPUS = "claude-4.1-opus"
GPT_5 = "gpt-5"
- GPT_5_MINI = "gpt-5-mini"
GEMINI_2_5_PRO = "gemini-2.5-pro"
GEMINI_2_5_FLASH = "gemini-2.5-flash"
@@ -43,7 +42,7 @@ class LLMSkill(StrEnum):
case LLMSkill.LLM_TO_RETRIEVE:
return AvailableLLM.GEMINI_2_5_FLASH
case LLMSkill.LLM_CHEAP_FOR_EASY_QUESTIONS:
- return AvailableLLM.GPT_5_MINI
+ return AvailableLLM.CLAUDE_4_SONNET
case LLMSkill.LLM_TO_ANSWER_HARD_QUESTIONS:
return AvailableLLM.GPT_5
case LLMSkill.LLM_FOR_VISUAL_ANALYSIS:
diff --git a/pipelex/libraries/pipelines/builder/pipe/pipe_signature.py b/pipelex/libraries/pipelines/builder/pipe/pipe_signature.py
index 8affc9a9c..8c7867d5b 100644
--- a/pipelex/libraries/pipelines/builder/pipe/pipe_signature.py
+++ b/pipelex/libraries/pipelines/builder/pipe/pipe_signature.py
@@ -1,4 +1,4 @@
-from typing import Any
+from typing import Any, Literal
from pydantic import Field, field_validator
@@ -18,7 +18,7 @@ class PipeSignature(StructuredContent):
"""
code: str = Field(description="Pipe code identifying the pipe. Must be snake_case.")
- category: AllowedPipeCategories = Field(description="Pipe category.")
+ category: Literal["PipeSignature"] = "PipeSignature"
type: AllowedPipeTypes = Field(description="Pipe type.")
description: str = Field(description="What the pipe does")
inputs: dict[str, str] = Field(
diff --git a/pipelex/pipe_controllers/batch/pipe_batch_blueprint.py b/pipelex/pipe_controllers/batch/pipe_batch_blueprint.py
index 7f0655c0f..7fefdb2cf 100644
--- a/pipelex/pipe_controllers/batch/pipe_batch_blueprint.py
+++ b/pipelex/pipe_controllers/batch/pipe_batch_blueprint.py
@@ -1,5 +1,7 @@
from typing import Literal
+from typing_extensions import override
+
from pipelex.core.pipes.pipe_blueprint import PipeBlueprint
@@ -9,3 +11,9 @@ class PipeBatchBlueprint(PipeBlueprint):
branch_pipe_code: str
input_list_name: str | None = None
input_item_name: str | None = None
+
+ @property
+ @override
+ def pipe_dependencies(self) -> set[str]:
+ """Return the set containing the branch pipe code."""
+ return {self.branch_pipe_code}
diff --git a/pipelex/pipe_controllers/condition/pipe_condition_blueprint.py b/pipelex/pipe_controllers/condition/pipe_condition_blueprint.py
index 744b77413..f8bf90b64 100644
--- a/pipelex/pipe_controllers/condition/pipe_condition_blueprint.py
+++ b/pipelex/pipe_controllers/condition/pipe_condition_blueprint.py
@@ -1,8 +1,10 @@
from typing import Literal
from pydantic import Field
+from typing_extensions import override
from pipelex.core.pipes.pipe_blueprint import PipeBlueprint
+from pipelex.core.pipes.specific_pipe import SpecificPipeCodesEnum
class PipeConditionBlueprint(PipeBlueprint):
@@ -13,3 +15,15 @@ class PipeConditionBlueprint(PipeBlueprint):
pipe_map: dict[str, str] = Field(default_factory=dict)
default_pipe_code: str | None = None
add_alias_from_expression_to: str | None = None
+
+ @property
+ @override
+ def pipe_dependencies(self) -> set[str]:
+ """Return the set of pipe codes from pipe_map and default_pipe_code.
+
+ Excludes special pipe codes like 'continue'.
+ """
+ codes = set(self.pipe_map.values())
+ if self.default_pipe_code:
+ codes.add(self.default_pipe_code)
+ return codes - set(SpecificPipeCodesEnum.value_list())
diff --git a/pipelex/pipe_controllers/parallel/pipe_parallel_blueprint.py b/pipelex/pipe_controllers/parallel/pipe_parallel_blueprint.py
index b2b482f3b..8fba98166 100644
--- a/pipelex/pipe_controllers/parallel/pipe_parallel_blueprint.py
+++ b/pipelex/pipe_controllers/parallel/pipe_parallel_blueprint.py
@@ -1,6 +1,7 @@
from typing import Literal
from pydantic import field_validator, model_validator
+from typing_extensions import override
from pipelex.core.concepts.concept_blueprint import ConceptBlueprint
from pipelex.core.pipes.pipe_blueprint import PipeBlueprint
@@ -16,6 +17,12 @@ class PipeParallelBlueprint(PipeBlueprint):
add_each_output: bool = False
combined_output: str | None = None
+ @property
+ @override
+ def pipe_dependencies(self) -> set[str]:
+ """Return the set of pipe codes from the parallel branches."""
+ return {parallel.pipe for parallel in self.parallels}
+
@field_validator("combined_output", mode="before")
@classmethod
def validate_combined_output(cls, combined_output: str) -> str:
diff --git a/pipelex/pipe_controllers/sequence/pipe_sequence_blueprint.py b/pipelex/pipe_controllers/sequence/pipe_sequence_blueprint.py
index 47fe17dda..58a498e6c 100644
--- a/pipelex/pipe_controllers/sequence/pipe_sequence_blueprint.py
+++ b/pipelex/pipe_controllers/sequence/pipe_sequence_blueprint.py
@@ -1,5 +1,7 @@
from typing import Literal
+from typing_extensions import override
+
from pipelex.core.pipes.pipe_blueprint import PipeBlueprint
from pipelex.pipe_controllers.sub_pipe_blueprint import SubPipeBlueprint
@@ -8,3 +10,18 @@ class PipeSequenceBlueprint(PipeBlueprint):
type: Literal["PipeSequence"] = "PipeSequence"
category: Literal["PipeController"] = "PipeController"
steps: list[SubPipeBlueprint]
+
+ @property
+ @override
+ def pipe_dependencies(self) -> set[str]:
+ """Return the set of pipe codes from the sequence steps."""
+ return {step.pipe for step in self.steps}
+
+ @property
+ @override
+ def ordered_pipe_dependencies(self) -> list[str]:
+ """Return the ordered list of pipe codes from the sequence steps.
+
+ For sequences, the order of steps matters, so we preserve it.
+ """
+ return [step.pipe for step in self.steps]
diff --git a/pipelex/tools/misc/mermaid_utils.py b/pipelex/tools/misc/mermaid_utils.py
index 994a6a158..6f9795773 100644
--- a/pipelex/tools/misc/mermaid_utils.py
+++ b/pipelex/tools/misc/mermaid_utils.py
@@ -24,7 +24,7 @@ def make_mermaid_url(mermaid_code: str) -> str:
},
}
encoded = encode_pako_from_string(json.dumps(as_dict))
- return f"https://mermaid.ink/img/{encoded}"
+ return f"https://mermaid.ink/svg/{encoded}"
def clean_str_for_mermaid_node_title(text: str) -> str:
diff --git a/tests/integration/pipelex/pipes/test_flow_factory.py b/tests/integration/pipelex/pipes/test_flow_factory.py
new file mode 100644
index 000000000..c4ce678f0
--- /dev/null
+++ b/tests/integration/pipelex/pipes/test_flow_factory.py
@@ -0,0 +1,128 @@
+import os
+from pathlib import Path
+
+import pytest
+
+from pipelex import log, pretty_print
+from pipelex.libraries.pipelines.builder.flow_factory import FlowFactory
+from pipelex.pipe_controllers.sequence.pipe_sequence_blueprint import PipeSequenceBlueprint
+from pipelex.tools.misc.file_utils import get_incremental_directory_path, remove_folder
+from pipelex.tools.misc.json_utils import save_as_json_to_path
+from tests.conftest import TEST_OUTPUTS_DIR
+
+
[email protected](loop_scope="class")
+class TestFlowFactory:
+ """Test FlowFactory loading PLX files and converting to flow view."""
+
+ @pytest.mark.parametrize(
+ ("plx_name", "plx_file"),
+ [
+ ("discord_newsletter", "discord_newsletter.plx"),
+ ("tricky_questions", "tricky_questions.plx"),
+ ],
+ )
+ async def test_load_plx_and_convert_to_flow(
+ self,
+ plx_name: str,
+ plx_file: str,
+ ):
+ """Load a PLX file, convert to Flow, and save as JSON.
+
+ This test demonstrates the FlowFactory's ability to:
+ 1. Load a PLX file
+ 2. Convert it to a simplified flow view
+ 3. Serialize the result to JSON for inspection
+
+ The flow view shows:
+ - Pipe controllers with full details (sequence, parallel, condition, batch)
+ - Pipe operators as signatures only (contract without implementation)
+ """
+ # Setup result directory
+ result_dir_path = get_incremental_directory_path(
+ base_path=TEST_OUTPUTS_DIR,
+ base_name=f"flow_{plx_name}",
+ )
+
+ try:
+ # Load PLX file from test pipelines directory
+ test_pipelines_dir = Path(__file__).parent.parent.parent.parent / "test_pipelines"
+ plx_file_path = test_pipelines_dir / plx_file
+
+ log.info(f"Loading PLX file: {plx_file_path}")
+
+ # Convert to pipeline flow
+ flow = FlowFactory.make_from_plx_file(plx_file_path)
+
+ # Pretty print for console output
+ pretty_print(flow, title=f"Pipeline flow: {plx_name}")
+
+ # Save as JSON to results directory
+ json_output_path = os.path.join(result_dir_path, "pipeline_flow.json")
+ save_as_json_to_path(object_to_save=flow, path=json_output_path)
+
+ log.info(f"Saved flow to: {json_output_path}")
+
+ # Verify the flow was created correctly
+ assert flow is not None
+ assert flow.domain is not None
+ assert flow.flow_elements is not None
+ assert len(flow.flow_elements) > 0
+
+ # Log some details about what we found
+ controller_count = sum(1 for pipe in flow.flow_elements.values() if pipe.category == "PipeController")
+ operator_count = sum(1 for pipe in flow.flow_elements.values() if pipe.category == "PipeSignature")
+
+ log.info(f"flow contains {len(flow.flow_elements)} pipes: {controller_count} controllers, {operator_count} operators (as signatures)")
+
+ finally:
+ # Cleanup - remove the temporary result directory
+ remove_folder(result_dir_path)
+
+ async def test_flow_preserves_sequence_structure(self):
+ """Verify that PipeSequence controllers preserve their step structure."""
+ test_pipelines_dir = Path(__file__).parent.parent.parent.parent / "test_pipelines"
+ plx_file_path = test_pipelines_dir / "discord_newsletter.plx"
+
+ flow = FlowFactory.make_from_plx_file(plx_file_path)
+
+ # Find the sequence pipe
+ sequence_pipe = flow.flow_elements.get("write_discord_newsletter")
+ assert sequence_pipe is not None
+ assert sequence_pipe.type == "PipeSequence"
+
+ # Type narrow to PipeSequenceBlueprint for type safety
+ assert isinstance(sequence_pipe, PipeSequenceBlueprint)
+
+ # Verify steps are preserved
+ assert len(sequence_pipe.steps) > 0
+
+ log.info(f"Sequence pipe has {len(sequence_pipe.steps)} steps:")
+ for step in sequence_pipe.steps:
+ log.info(f" - {step.pipe} -> {step.result}")
+
+ async def test_flow_converts_operators_to_signatures(self):
+ """Verify that pipe operators are converted to signatures (simplified view)."""
+ test_pipelines_dir = Path(__file__).parent.parent.parent.parent / "test_pipelines"
+ plx_file_path = test_pipelines_dir / "discord_newsletter.plx"
+
+ flow = FlowFactory.make_from_plx_file(plx_file_path)
+
+ # Find an operator pipe (LLM pipe) - converted to signature
+ operator_pipe = flow.flow_elements.get("summarize_discord_channel_update_for_new_members")
+ assert operator_pipe is not None
+ assert operator_pipe.category == "PipeSignature"
+ assert operator_pipe.type == "PipeLLM"
+
+ # Verify it has the signature properties
+ assert hasattr(operator_pipe, "code")
+ assert hasattr(operator_pipe, "inputs")
+ assert hasattr(operator_pipe, "output")
+ assert hasattr(operator_pipe, "description")
+
+ # Verify it doesn't have implementation details (like prompt_template)
+ assert not hasattr(operator_pipe, "prompt_template")
+
+ log.info(f"Operator pipe '{operator_pipe.code}' converted to signature:")
+ log.info(f" Inputs: {operator_pipe.inputs}")
+ log.info(f" Output: {operator_pipe.output}")
diff --git a/tests/unit/pipelex/core/bundles/__init__.py b/tests/unit/pipelex/core/bundles/__init__.py
new file mode 100644
index 000000000..e978cd940
--- /dev/null
+++ b/tests/unit/pipelex/core/bundles/__init__.py
@@ -0,0 +1 @@
+"""Unit tests for core bundles module."""
diff --git a/tests/unit/pipelex/core/bundles/test_data_pipe_sorter.py b/tests/unit/pipelex/core/bundles/test_data_pipe_sorter.py
new file mode 100644
index 000000000..7a45b885e
--- /dev/null
+++ b/tests/unit/pipelex/core/bundles/test_data_pipe_sorter.py
@@ -0,0 +1,372 @@
+"""Test data for pipe dependency sorting."""
+
+from typing import ClassVar
+
+from pipelex.core.bundles.pipelex_bundle_blueprint import PipeBlueprintUnion
+from pipelex.exceptions import PipeDefinitionError
+from pipelex.pipe_controllers.batch.pipe_batch_blueprint import PipeBatchBlueprint
+from pipelex.pipe_controllers.condition.pipe_condition_blueprint import PipeConditionBlueprint
+from pipelex.pipe_controllers.parallel.pipe_parallel_blueprint import PipeParallelBlueprint
+from pipelex.pipe_controllers.sequence.pipe_sequence_blueprint import PipeSequenceBlueprint
+from pipelex.pipe_controllers.sub_pipe_blueprint import SubPipeBlueprint
+from pipelex.pipe_operators.img_gen.pipe_img_gen_blueprint import PipeImgGenBlueprint
+from pipelex.pipe_operators.llm.pipe_llm_blueprint import PipeLLMBlueprint
+
+
+class PipeSorterTestCases:
+ """Test cases for pipe dependency sorting with various scenarios."""
+
+ # Test case 1: No dependencies - all operators
+ NO_DEPENDENCIES_PIPES: ClassVar[dict[str, PipeBlueprintUnion]] = {
+ "pipe_c": PipeLLMBlueprint(type="PipeLLM", category="PipeOperator", description="C", inputs={}, output="Text"),
+ "pipe_a": PipeLLMBlueprint(type="PipeLLM", category="PipeOperator", description="A", inputs={}, output="Text"),
+ "pipe_b": PipeLLMBlueprint(type="PipeLLM", category="PipeOperator", description="B", inputs={}, output="Text"),
+ }
+ NO_DEPENDENCIES_EXPECTED: ClassVar[list[str]] = ["pipe_a", "pipe_b", "pipe_c"] # Alphabetical order (all are roots)
+
+ # Test case 2: Simple chain A -> B -> C
+ SIMPLE_CHAIN_PIPES: ClassVar[dict[str, PipeBlueprintUnion]] = {
+ "pipe_c": PipeSequenceBlueprint(
+ type="PipeSequence",
+ category="PipeController",
+ description="C depends on B",
+ inputs={},
+ output="Text",
+ steps=[SubPipeBlueprint(pipe="pipe_b", result="result_b")],
+ ),
+ "pipe_a": PipeLLMBlueprint(type="PipeLLM", category="PipeOperator", description="A no deps", inputs={}, output="Text"),
+ "pipe_b": PipeSequenceBlueprint(
+ type="PipeSequence",
+ category="PipeController",
+ description="B depends on A",
+ inputs={},
+ output="Text",
+ steps=[SubPipeBlueprint(pipe="pipe_a", result="result_a")],
+ ),
+ }
+ SIMPLE_CHAIN_EXPECTED: ClassVar[list[str]] = ["pipe_c", "pipe_b", "pipe_a"]
+
+ # Test case 3: Diamond pattern
+ # A
+ # / \
+ # B C
+ # \ /
+ # D
+ DIAMOND_PIPES: ClassVar[dict[str, PipeBlueprintUnion]] = {
+ "pipe_d": PipeParallelBlueprint(
+ type="PipeParallel",
+ category="PipeController",
+ description="D depends on B and C",
+ inputs={},
+ output="Text",
+ parallels=[
+ SubPipeBlueprint(pipe="pipe_b", result="result_b"),
+ SubPipeBlueprint(pipe="pipe_c", result="result_c"),
+ ],
+ add_each_output=True,
+ ),
+ "pipe_a": PipeLLMBlueprint(type="PipeLLM", category="PipeOperator", description="A", inputs={}, output="Text"),
+ "pipe_c": PipeSequenceBlueprint(
+ type="PipeSequence",
+ category="PipeController",
+ description="C depends on A",
+ inputs={},
+ output="Text",
+ steps=[SubPipeBlueprint(pipe="pipe_a", result="result_a")],
+ ),
+ "pipe_b": PipeSequenceBlueprint(
+ type="PipeSequence",
+ category="PipeController",
+ description="B depends on A",
+ inputs={},
+ output="Text",
+ steps=[SubPipeBlueprint(pipe="pipe_a", result="result_a")],
+ ),
+ }
+ # D is root, visits B first (alphabetically), then A (B's dependency), then C (already visited A)
+ # Don't verify exact order for diamond pattern due to shared dependencies
+ DIAMOND_EXPECTED: ClassVar[list[str] | None] = None
+
+ # Test case 4: Multiple independent chains
+ MULTIPLE_CHAINS_PIPES: ClassVar[dict[str, PipeBlueprintUnion]] = {
+ # Chain 1: A -> B
+ "pipe_b": PipeSequenceBlueprint(
+ type="PipeSequence",
+ category="PipeController",
+ description="B depends on A",
+ inputs={},
+ output="Text",
+ steps=[SubPipeBlueprint(pipe="pipe_a", result="result_a")],
+ ),
+ "pipe_a": PipeLLMBlueprint(type="PipeLLM", category="PipeOperator", description="A", inputs={}, output="Text"),
+ # Chain 2: X -> Y
+ "pipe_y": PipeSequenceBlueprint(
+ type="PipeSequence",
+ category="PipeController",
+ description="Y depends on X",
+ inputs={},
+ output="Text",
+ steps=[SubPipeBlueprint(pipe="pipe_x", result="result_x")],
+ ),
+ "pipe_x": PipeLLMBlueprint(type="PipeLLM", category="PipeOperator", description="X", inputs={}, output="Text"),
+ }
+ # Depth-first from roots (pipe_b, pipe_y alphabetically): pipe_b -> pipe_a, then pipe_y -> pipe_x
+ MULTIPLE_CHAINS_EXPECTED: ClassVar[list[str]] = ["pipe_b", "pipe_a", "pipe_y", "pipe_x"]
+
+ # Test case 5: PipeBatch dependency
+ PIPE_BATCH_PIPES: ClassVar[dict[str, PipeBlueprintUnion]] = {
+ "batch_pipe": PipeBatchBlueprint(
+ type="PipeBatch",
+ category="PipeController",
+ description="Batch depends on process",
+ inputs={},
+ output="Text",
+ branch_pipe_code="process_item",
+ ),
+ "process_item": PipeLLMBlueprint(type="PipeLLM", category="PipeOperator", description="Process", inputs={}, output="Text"),
+ }
+ PIPE_BATCH_EXPECTED: ClassVar[list[str]] = ["batch_pipe", "process_item"]
+
+ # Test case 6: PipeCondition with multiple branches
+ PIPE_CONDITION_PIPES: ClassVar[dict[str, PipeBlueprintUnion]] = {
+ "router": PipeConditionBlueprint(
+ type="PipeCondition",
+ category="PipeController",
+ description="Routes to different pipes",
+ inputs={},
+ output="Text",
+ expression="category",
+ pipe_map={
+ "small": "process_small",
+ "large": "process_large",
+ },
+ default_pipe_code="process_default",
+ ),
+ "process_large": PipeLLMBlueprint(type="PipeLLM", category="PipeOperator", description="Large", inputs={}, output="Text"),
+ "process_small": PipeLLMBlueprint(type="PipeLLM", category="PipeOperator", description="Small", inputs={}, output="Text"),
+ "process_default": PipeLLMBlueprint(type="PipeLLM", category="PipeOperator", description="Default", inputs={}, output="Text"),
+ }
+ # Router first, then its dependencies in alphabetical order
+ PIPE_CONDITION_EXPECTED: ClassVar[list[str]] = ["router", "process_default", "process_large", "process_small"]
+
+ # Test case 7: Circular dependency (should raise error)
+ CIRCULAR_PIPES: ClassVar[dict[str, PipeBlueprintUnion]] = {
+ "pipe_a": PipeSequenceBlueprint(
+ type="PipeSequence",
+ category="PipeController",
+ description="A depends on C (circular!)",
+ inputs={},
+ output="Text",
+ steps=[SubPipeBlueprint(pipe="pipe_c", result="result_c")],
+ ),
+ "pipe_b": PipeSequenceBlueprint(
+ type="PipeSequence",
+ category="PipeController",
+ description="B depends on A",
+ inputs={},
+ output="Text",
+ steps=[SubPipeBlueprint(pipe="pipe_a", result="result_a")],
+ ),
+ "pipe_c": PipeSequenceBlueprint(
+ type="PipeSequence",
+ category="PipeController",
+ description="C depends on B",
+ inputs={},
+ output="Text",
+ steps=[SubPipeBlueprint(pipe="pipe_b", result="result_b")],
+ ),
+ }
+
+ # Test case 8: Reference to non-existent pipe (should be ignored in sorting)
+ MISSING_DEPENDENCY_PIPES: ClassVar[dict[str, PipeBlueprintUnion]] = {
+ "pipe_b": PipeSequenceBlueprint(
+ type="PipeSequence",
+ category="PipeController",
+ description="B depends on A and Z (Z doesn't exist)",
+ inputs={},
+ output="Text",
+ steps=[
+ SubPipeBlueprint(pipe="pipe_a", result="result_a"),
+ SubPipeBlueprint(pipe="pipe_z", result="result_z"), # Z doesn't exist in this bundle
+ ],
+ ),
+ "pipe_a": PipeLLMBlueprint(type="PipeLLM", category="PipeOperator", description="A", inputs={}, output="Text"),
+ }
+ MISSING_DEPENDENCY_EXPECTED: ClassVar[list[str]] = ["pipe_b", "pipe_a"] # Z is ignored as it's not in the bundle
+
+ # Test case 9: Image inversion pipeline - realistic multi-step sequence
+ IMAGE_INVERSION_PIPES: ClassVar[dict[str, PipeBlueprintUnion]] = {
+ "analyze_image_content": PipeLLMBlueprint(
+ type="PipeLLM",
+ category="PipeOperator",
+ description="Analyzes the input photo to understand visual elements, composition, mood, etc.",
+ inputs={"input_photo": "Image"},
+ output="ImageAnalysis",
+ ),
+ "define_opposite_concept": PipeLLMBlueprint(
+ type="PipeLLM",
+ category="PipeOperator",
+ description="Determines what constitutes the opposite for the analyzed image",
+ inputs={"image_analysis": "ImageAnalysis"},
+ output="OppositeDefinition",
+ ),
+ "generate_image_prompt": PipeLLMBlueprint(
+ type="PipeLLM",
+ category="PipeOperator",
+ description="Crafts a detailed image generation prompt from the opposite definition",
+ inputs={"opposite_definition": "OppositeDefinition"},
+ output="ImagePrompt",
+ ),
+ "generate_opposite_image": PipeImgGenBlueprint(
+ type="PipeImgGen",
+ category="PipeOperator",
+ description="Generates the opposite image using AI",
+ inputs={"generation_prompt": "ImagePrompt"},
+ output="Image",
+ ),
+ "photo_opposite_pipeline": PipeSequenceBlueprint(
+ type="PipeSequence",
+ category="PipeController",
+ description="Main pipeline that generates the opposite of an input photo",
+ inputs={"input_photo": "Image"},
+ output="Image",
+ steps=[
+ SubPipeBlueprint(pipe="analyze_image_content", result="image_analysis"),
+ SubPipeBlueprint(pipe="define_opposite_concept", result="opposite_concept"),
+ SubPipeBlueprint(pipe="generate_image_prompt", result="generation_prompt"),
+ SubPipeBlueprint(pipe="generate_opposite_image", result="opposite_image"),
+ ],
+ ),
+ }
+ # Controller first, then dependencies in step order
+ IMAGE_INVERSION_EXPECTED: ClassVar[list[str]] = [
+ "photo_opposite_pipeline",
+ "analyze_image_content",
+ "define_opposite_concept",
+ "generate_image_prompt",
+ "generate_opposite_image",
+ ]
+
+ # Test case 10: Complex nested pipeline with PipeSequence containing another PipeSequence and PipeCondition
+ COMPLEX_NESTED_PIPES: ClassVar[dict[str, PipeBlueprintUnion]] = {
+ # Main sequence with nested controllers
+ "main_pipeline": PipeSequenceBlueprint(
+ type="PipeSequence",
+ category="PipeController",
+ description="Main pipeline with nested controllers",
+ inputs={"input": "Text"},
+ output="Text",
+ steps=[
+ SubPipeBlueprint(pipe="nested_sequence", result="prepared"),
+ SubPipeBlueprint(pipe="router", result="processed"),
+ SubPipeBlueprint(pipe="final_process", result="final"),
+ ],
+ ),
+ # Nested sequence
+ "nested_sequence": PipeSequenceBlueprint(
+ type="PipeSequence",
+ category="PipeController",
+ description="Nested preparation sequence",
+ inputs={"input": "Text"},
+ output="Text",
+ steps=[
+ SubPipeBlueprint(pipe="prepare_data", result="prepared_data"),
+ SubPipeBlueprint(pipe="validate_data", result="validated_data"),
+ ],
+ ),
+ # Condition router
+ "router": PipeConditionBlueprint(
+ type="PipeCondition",
+ category="PipeController",
+ description="Routes based on size",
+ inputs={"prepared": "Text"},
+ output="Text",
+ expression="size",
+ pipe_map={
+ "small": "process_small",
+ "large": "process_large",
+ },
+ default_pipe_code="process_default",
+ ),
+ # LLM operators
+ "prepare_data": PipeLLMBlueprint(
+ type="PipeLLM",
+ category="PipeOperator",
+ description="Prepare the data",
+ inputs={"input": "Text"},
+ output="Text",
+ ),
+ "validate_data": PipeLLMBlueprint(
+ type="PipeLLM",
+ category="PipeOperator",
+ description="Validate the prepared data",
+ inputs={"prepared_data": "Text"},
+ output="Text",
+ ),
+ "process_small": PipeLLMBlueprint(
+ type="PipeLLM",
+ category="PipeOperator",
+ description="Process small items",
+ inputs={"item": "Text"},
+ output="Text",
+ ),
+ "process_large": PipeLLMBlueprint(
+ type="PipeLLM",
+ category="PipeOperator",
+ description="Process large items",
+ inputs={"item": "Text"},
+ output="Text",
+ ),
+ "process_default": PipeLLMBlueprint(
+ type="PipeLLM",
+ category="PipeOperator",
+ description="Default processing",
+ inputs={"item": "Text"},
+ output="Text",
+ ),
+ "final_process": PipeLLMBlueprint(
+ type="PipeLLM",
+ category="PipeOperator",
+ description="Final processing step",
+ inputs={"processed": "Text"},
+ output="Text",
+ ),
+ }
+ # Expected order: main_pipeline first, then following step order
+ # Step 1: nested_sequence and its steps (prepare_data, validate_data)
+ # Step 2: router and its dependencies (alphabetically: process_default, process_large, process_small)
+ # Step 3: final_process
+ COMPLEX_NESTED_EXPECTED: ClassVar[list[str]] = [
+ "main_pipeline",
+ "nested_sequence",
+ "prepare_data",
+ "validate_data",
+ "router",
+ "process_default",
+ "process_large",
+ "process_small",
+ "final_process",
+ ]
+
+ # Aggregate all test cases
+ TEST_CASES: ClassVar[
+ list[
+ tuple[
+ str, # test_name
+ dict[str, PipeBlueprintUnion], # pipes
+ list[str] | None, # expected_order (None if should raise error)
+ type[Exception] | None, # expected_exception (None if should succeed)
+ ]
+ ]
+ ] = [
+ ("no_dependencies", NO_DEPENDENCIES_PIPES, NO_DEPENDENCIES_EXPECTED, None),
+ ("simple_chain", SIMPLE_CHAIN_PIPES, SIMPLE_CHAIN_EXPECTED, None),
+ ("diamond_pattern", DIAMOND_PIPES, DIAMOND_EXPECTED, None),
+ ("multiple_chains", MULTIPLE_CHAINS_PIPES, MULTIPLE_CHAINS_EXPECTED, None),
+ ("pipe_batch", PIPE_BATCH_PIPES, PIPE_BATCH_EXPECTED, None),
+ ("pipe_condition", PIPE_CONDITION_PIPES, PIPE_CONDITION_EXPECTED, None),
+ ("circular_dependency", CIRCULAR_PIPES, None, PipeDefinitionError),
+ ("missing_dependency", MISSING_DEPENDENCY_PIPES, MISSING_DEPENDENCY_EXPECTED, None),
+ ("image_inversion", IMAGE_INVERSION_PIPES, IMAGE_INVERSION_EXPECTED, None),
+ ("complex_nested", COMPLEX_NESTED_PIPES, COMPLEX_NESTED_EXPECTED, None),
+ ]
diff --git a/tests/unit/pipelex/core/bundles/test_pipe_sorter.py b/tests/unit/pipelex/core/bundles/test_pipe_sorter.py
new file mode 100644
index 000000000..66e0a1868
--- /dev/null
+++ b/tests/unit/pipelex/core/bundles/test_pipe_sorter.py
@@ -0,0 +1,66 @@
+"""Unit tests for pipe dependency sorting."""
+
+import pytest
+
+from pipelex.core.bundles.pipe_sorter import sort_pipes_by_dependencies
+from pipelex.core.bundles.pipelex_bundle_blueprint import PipeBlueprintUnion
+
+from .test_data_pipe_sorter import PipeSorterTestCases
+
+
+class TestSortPipesByDependencies:
+ """Test suite for the sort_pipes_by_dependencies function."""
+
+ @pytest.mark.parametrize(
+ ("test_name", "pipes", "expected_order", "expected_exception"),
+ PipeSorterTestCases.TEST_CASES,
+ )
+ def test_sort_pipes_by_dependencies(
+ self,
+ test_name: str,
+ pipes: dict[str, PipeBlueprintUnion],
+ expected_order: list[str] | None,
+ expected_exception: type[Exception] | None,
+ ):
+ """Test pipe sorting with various dependency patterns.
+
+ Args:
+ test_name: Name of the test case for identification
+ pipes: Dictionary of pipe_code to PipeBlueprintUnion
+ expected_order: Expected order of pipe codes after sorting (None if should raise)
+ expected_exception: Expected exception type (None if should succeed)
+ """
+ if expected_exception:
+ # Test that circular dependencies raise the correct exception
+ with pytest.raises(expected_exception):
+ sort_pipes_by_dependencies(pipes)
+ else:
+ # Test successful sorting
+ sorted_pipes = sort_pipes_by_dependencies(pipes)
+ sorted_codes = [code for code, _ in sorted_pipes]
+
+ # Verify all pipes are present
+ assert len(sorted_codes) == len(pipes), f"Test '{test_name}': Not all pipes were included in sorted result"
+ assert set(sorted_codes) == set(pipes.keys()), f"Test '{test_name}': Pipe codes don't match"
+
+ # Verify the order respects depth-first pre-order dependencies
+ # In depth-first pre-order, each pipe comes before its dependencies
+ # (unless the dependency was already visited through another path)
+ for code, blueprint in sorted_pipes:
+ dependencies = blueprint.pipe_dependencies
+ code_index = sorted_codes.index(code)
+ for dep_code in dependencies:
+ # Only check dependencies that exist in this bundle
+ if dep_code in pipes:
+ dep_index = sorted_codes.index(dep_code)
+ # Skip if dependency was visited earlier (shared dependency)
+ if dep_index < code_index:
+ continue
+ assert code_index < dep_index, (
+ f"Test '{test_name}': In depth-first pre-order, pipe '{code}' "
+ f"should come before its dependency '{dep_code}' in {sorted_codes}"
+ )
+
+ # For test cases with a specific expected order, verify exact match
+ if expected_order is not None:
+ assert sorted_codes == expected_order, f"Test '{test_name}': Expected {expected_order}, got {sorted_codes}"
diff --git a/tests/unit/pipelex/libraries/pipelines/builder/pipe/pipe_operators/pipe_llm/test_data.py b/tests/unit/pipelex/libraries/pipelines/builder/pipe/pipe_operators/pipe_llm/test_data.py
index 08ba5eb13..b8bac296f 100644
--- a/tests/unit/pipelex/libraries/pipelines/builder/pipe/pipe_operators/pipe_llm/test_data.py
+++ b/tests/unit/pipelex/libraries/pipelines/builder/pipe/pipe_operators/pipe_llm/test_data.py
@@ -84,7 +84,7 @@ class PipeLLMTestCases:
type="PipeLLM",
description="Generate with settings",
llm=LLMSetting(
- llm_handle="gpt-5-mini",
+ llm_handle="claude-4-sonnet",
temperature=0.7,
max_tokens=None, # "auto" is handled at conversion to core
),
@@ -135,7 +135,7 @@ class PipeLLMTestCases:
nb_output=None,
output="Item",
prompt_template="Generate items",
- llm="gpt-5-mini",
+ llm="claude-4-sonnet",
),
)
@@ -158,7 +158,7 @@ class PipeLLMTestCases:
multiple_output=None,
output="Item",
prompt_template="Generate items",
- llm="gpt-5-mini",
+ llm="claude-4-sonnet",
),
)