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.

Try and validate pipe after creation

Louis Choquelc6d5c190ef54
diff --git a/pipelex/cli/commands/build_cmd.py b/pipelex/cli/commands/build_cmd.py
index 51e860ac7..b0aca2346 100644
--- a/pipelex/cli/commands/build_cmd.py
+++ b/pipelex/cli/commands/build_cmd.py
@@ -1,17 +1,21 @@
from __future__ import annotations
import asyncio
-from typing import Annotated, Optional
+from typing import Annotated, Any, Dict, Optional, cast
import typer
+from pydantic import ValidationError
from pipelex import pretty_print
-from pipelex.exceptions import PipelexCLIError
+from pipelex.exceptions import PipeDefinitionError, PipelexCLIError
+from pipelex.hub import get_library_manager
+from pipelex.libraries.library_manager import LibraryManager
from pipelex.libraries.pipeline_blueprint import PipelineLibraryBlueprint
from pipelex.pipe_works.pipe_dry import dry_run_pipe_codes
from pipelex.pipelex import Pipelex
from pipelex.pipeline.execute import execute_pipeline
from pipelex.tools.misc.file_utils import load_text_from_path
+from pipelex.tools.typing.pydantic_utils import format_pydantic_validation_error
def _load_requirements_text(requirements: Optional[str], requirements_file: Optional[str]) -> str:
@@ -60,12 +64,48 @@ def do_build_blueprint(
typer.echo(f"✅ Blueprint saved to '{output_path}'")
if validate:
+ try:
+ _load_pipes_from_generated_blueprint(blueprint=blueprint)
+ except PipeDefinitionError as exc:
+ raise PipelexCLIError(f"Failed to load pipes from generated blueprint at '{output_path}': {exc}") from exc
+
generated_pipe_codes = list(blueprint.pipe.keys())
if not generated_pipe_codes:
raise PipelexCLIError("No pipe found in generated blueprint to validate")
asyncio.run(dry_run_pipe_codes(pipe_codes=generated_pipe_codes))
+def _load_pipes_from_generated_blueprint(blueprint: PipelineLibraryBlueprint) -> None:
+ """Instantiate and register all pipes from a generated PipelineLibraryBlueprint.
+
+ This constructs the appropriate typed PipeBlueprint for each pipe entry and
+ uses LibraryManager.load_pipe_from_blueprint to create the Pipe, then adds it
+ to the in-memory PipeLibrary so that validation can run against them.
+ """
+ library_manager = cast(LibraryManager, get_library_manager())
+ domain_code = blueprint.domain
+
+ for pipe_code, details in blueprint.pipe.items():
+ details_dict: Dict[str, Any] = details.copy()
+
+ # Build the concrete PipeBlueprint via LibraryManager helper
+ try:
+ pipe_blueprint = LibraryManager.make_pipe_blueprint_from_details(
+ domain_code=domain_code,
+ details_dict=details_dict,
+ )
+ except ValidationError as exc:
+ error_msg = format_pydantic_validation_error(exc=exc)
+ raise PipeDefinitionError(f"Failed to build pipe blueprint for pipe '{pipe_code}': {error_msg}") from exc
+
+ # Create pipe and register in library
+ pipe = LibraryManager.load_pipe_from_blueprint(
+ pipe_code=pipe_code,
+ pipe_blueprint=pipe_blueprint,
+ )
+ library_manager.pipe_library.add_new_pipe(pipe=pipe)
+
+
# Typer group for build commands
build_app = typer.Typer(help="Build artifacts like pipeline blueprints")
@@ -97,14 +137,22 @@ def build_blueprint_cmd(
),
] = "./pipelex_libraries",
) -> None:
- try:
- do_build_blueprint(
- relative_config_folder_path=relative_config_folder_path,
- requirements=requirements,
- requirements_file=requirements_file,
- output_path=output_path,
- validate=validate,
- )
- except Exception as exc:
- typer.echo(f"❌ Error: {exc}")
- raise typer.Exit(1)
+ # try:
+ # do_build_blueprint(
+ # relative_config_folder_path=relative_config_folder_path,
+ # requirements=requirements,
+ # requirements_file=requirements_file,
+ # output_path=output_path,
+ # validate=validate,
+ # )
+ # except Exception as exc:
+ # typer.echo(f"❌ Error: {exc}")
+ # raise typer.Exit(1)
+
+ do_build_blueprint(
+ relative_config_folder_path=relative_config_folder_path,
+ requirements=requirements,
+ requirements_file=requirements_file,
+ output_path=output_path,
+ validate=validate,
+ )
diff --git a/pipelex/libraries/library_manager.py b/pipelex/libraries/library_manager.py
index 1b1039eed..1730c9ed2 100644
--- a/pipelex/libraries/library_manager.py
+++ b/pipelex/libraries/library_manager.py
@@ -1,3 +1,4 @@
+import importlib
import os
from pathlib import Path
from typing import Any, ClassVar, Dict, List, Optional, Type
@@ -15,7 +16,7 @@ from pipelex.core.concept_library import ConceptLibrary
from pipelex.core.domain import Domain
from pipelex.core.domain_library import DomainLibrary
from pipelex.core.pipe_abstract import PipeAbstract
-from pipelex.core.pipe_blueprint import PipeSpecificFactoryProtocol
+from pipelex.core.pipe_blueprint import PipeBlueprint, PipeSpecificFactoryProtocol
from pipelex.core.pipe_library import PipeLibrary
from pipelex.exceptions import (
ConceptLibraryError,
@@ -63,7 +64,7 @@ class LibraryManager(LibraryManagerAbstract):
"domain",
"definition",
"system_prompt",
- "system_prompt_to_structure",
+ "system_prompt_jto_structure",
"prompt_template_to_structure",
]
domain_library: DomainLibrary
@@ -398,3 +399,100 @@ Old syntax will be removed in v0.3.0.
pipe_code=pipe_code,
details_dict=details_dict,
)
+
+ @classmethod
+ def load_pipe_from_blueprint(
+ cls,
+ pipe_code: str,
+ pipe_blueprint: PipeBlueprint,
+ ) -> PipeAbstract:
+ """Create a Pipe from a concrete PipeBlueprint instance.
+
+ This resolves the corresponding factory by converting the blueprint class name
+ (e.g. "PipeLLMBlueprint") to its factory name ("PipeLLMFactory").
+ """
+ # The blueprint must be a concrete subclass like PipeLLMBlueprint, PipeOcrBlueprint, etc.
+ blueprint_class_name = type(pipe_blueprint).__name__
+ if blueprint_class_name == "PipeBlueprint":
+ raise PipeFactoryError("Cannot load pipe from base PipeBlueprint. Please provide a specific blueprint subclass (e.g. PipeLLMBlueprint).")
+
+ # Derive factory name: PipeLLMBlueprint -> PipeLLMFactory
+ factory_class_name = blueprint_class_name.replace("Blueprint", "Factory")
+
+ try:
+ pipe_factory: Type[PipeSpecificFactoryProtocol[Any, Any]] = KajsonManager.get_class_registry().get_required_subclass(
+ name=factory_class_name,
+ base_class=PipeSpecificFactoryProtocol,
+ )
+ except ClassRegistryNotFoundError as factory_not_found_error:
+ raise PipeFactoryError(
+ f"Pipe '{pipe_code}' couldn't be created: factory '{factory_class_name}' not found: {factory_not_found_error}"
+ ) from factory_not_found_error
+ except ClassRegistryInheritanceError as factory_inheritance_error:
+ raise PipeFactoryError(
+ f"Pipe '{pipe_code}' couldn't be created: factory '{factory_class_name}' is not a subclass of {type(PipeSpecificFactoryProtocol)}."
+ ) from factory_inheritance_error
+
+ # Domain is part of the blueprint
+ domain_code = pipe_blueprint.domain
+
+ # Let the specific factory build the concrete Pipe instance
+ pipe_from_blueprint: PipeAbstract = pipe_factory.make_pipe_from_blueprint(
+ domain_code=domain_code,
+ pipe_code=pipe_code,
+ pipe_blueprint=pipe_blueprint, # type: ignore[arg-type]
+ )
+ return pipe_from_blueprint
+
+ @classmethod
+ def make_pipe_blueprint_from_details(
+ cls,
+ domain_code: str,
+ details_dict: Dict[str, Any],
+ ) -> PipeBlueprint:
+ """Create a concrete PipeBlueprint subclass instance from details.
+
+ This resolves the factory based on the provided details (supports both
+ new and legacy formats), locates the corresponding <PipeClassName>Blueprint
+ in the factory module, and validates the blueprint.
+ """
+ # Determine pipe class name from details
+ if "type" in details_dict and "definition" in details_dict:
+ pipe_class_name = details_dict["type"]
+ normalized_details = details_dict.copy()
+ else:
+ try:
+ pipe_class_name, legacy_definition = next(iter(details_dict.items()))
+ normalized_details = {"definition": legacy_definition}
+ except StopIteration as empty_err:
+ raise PipeFactoryError("Pipe details are empty; cannot determine pipe type") from empty_err
+
+ factory_class_name = f"{pipe_class_name}Factory"
+
+ # Resolve factory to locate its module (where the Blueprint class is defined)
+ try:
+ pipe_factory: Type[PipeSpecificFactoryProtocol[Any, Any]] = KajsonManager.get_class_registry().get_required_subclass(
+ name=factory_class_name,
+ base_class=PipeSpecificFactoryProtocol,
+ )
+ except ClassRegistryNotFoundError as factory_not_found_error:
+ raise PipeFactoryError(
+ f"Factory '{factory_class_name}' not found for pipe type '{pipe_class_name}': {factory_not_found_error}"
+ ) from factory_not_found_error
+ except ClassRegistryInheritanceError as factory_inheritance_error:
+ raise PipeFactoryError(
+ f"Factory '{factory_class_name}' is not a subclass of {type(PipeSpecificFactoryProtocol)}."
+ ) from factory_inheritance_error
+
+ factory_module = importlib.import_module(pipe_factory.__module__)
+ blueprint_class_name = f"{pipe_class_name}Blueprint"
+
+ try:
+ blueprint_cls: Type[PipeBlueprint] = getattr(factory_module, blueprint_class_name)
+ except AttributeError as exc:
+ raise PipeFactoryError(f"Cannot find blueprint class '{blueprint_class_name}' in module '{pipe_factory.__module__}'") from exc
+
+ details_with_domain = normalized_details.copy()
+ details_with_domain["domain"] = domain_code
+
+ return blueprint_cls.model_validate(details_with_domain)