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.
feature/catch-pipe-factory-error (#513)
Thomas Hebrardae3732178b6f
diff --git a/pipelex/builder/builder_loop.py b/pipelex/builder/builder_loop.pyindex cefaf90a9..222c40eba 100644--- a/pipelex/builder/builder_loop.py+++ b/pipelex/builder/builder_loop.py@@ -6,10 +6,11 @@ from pipelex.builder.builder import (PipeSpecUnion,reconstruct_bundle_with_pipe_fixes,)+from pipelex.builder.concept.concept_spec import ConceptSpecfrom pipelex.builder.pipe.pipe_sequence_spec import PipeSequenceSpecfrom pipelex.client.protocol import PipelineInputsfrom pipelex.config import get_config-from pipelex.core.pipes.exceptions import PipeValidationErrorType+from pipelex.core.pipes.exceptions import PipeFactoryErrorType, PipeValidationErrorTypefrom pipelex.core.pipes.pipe_blueprint import PipeCategoryfrom pipelex.core.pipes.variable_multiplicity import format_concept_with_multiplicityfrom pipelex.hub import get_required_pipe@@ -90,7 +91,36 @@ class BuilderLoop:is_save_second_iteration_enabled: bool,) -> PipelexBundleSpec:fixed_pipes: list[PipeSpecUnion] = []+ added_concepts: list[str] = []++ # Handle pipe factory errors (e.g., missing output concepts)+ for factory_error in bundle_error.pipe_factory_errors:+ match factory_error.error_type:+ case PipeFactoryErrorType.UNKNOWN_CONCEPT:+ # Fix unknown concept by adding a new concept that refines Text to the bundle+ unknown_concept_code = factory_error.missing_concept_code+ if not unknown_concept_code:+ continue++ # Create a simple concept that refines Text+ new_concept = ConceptSpec(+ the_concept_code=unknown_concept_code,+ description=unknown_concept_code,+ refines="Text",+ )+ # Add the concept to the bundle+ if pipelex_bundle_spec.concept is None:+ pipelex_bundle_spec.concept = {}++ pipelex_bundle_spec.concept[unknown_concept_code] = new_concept+ added_concepts.append(unknown_concept_code)+ log.info(f"🔧 Added unknown concept '{unknown_concept_code}' (refines Text) to bundle for pipe '{factory_error.pipe_code}'")++ case PipeFactoryErrorType.UNKNOWN_FACTORY_ERROR:+ continue++ # Handle pipe validation errorsfor val_error in bundle_error.pipe_validation_error_data:if not val_error.pipe_code or not pipelex_bundle_spec.pipe:continue@@ -196,16 +226,18 @@ class BuilderLoop:):continue- # Reconstruct bundle if we made changes+ # Reconstruct bundle if we made pipe changesif fixed_pipes:pipelex_bundle_spec = reconstruct_bundle_with_pipe_fixes(pipelex_bundle_spec=pipelex_bundle_spec, fixed_pipes=fixed_pipes)- if is_save_second_iteration_enabled:- plx_content = PlxFactory.make_plx_content(blueprint=pipelex_bundle_spec.to_blueprint())- second_iteration_path = get_incremental_file_path(- base_path="results",- base_name="generated_pipeline_2nd_iteration",- extension="plx",- )- save_text_to_path(text=plx_content, path=second_iteration_path)++ # Save second iteration if we made any changes (pipes or concepts)+ if (fixed_pipes or added_concepts) and is_save_second_iteration_enabled:+ plx_content = PlxFactory.make_plx_content(blueprint=pipelex_bundle_spec.to_blueprint())+ second_iteration_path = get_incremental_file_path(+ base_path="results",+ base_name="generated_pipeline_2nd_iteration",+ extension="plx",+ )+ save_text_to_path(text=plx_content, path=second_iteration_path)return pipelex_bundle_specdiff --git a/pipelex/core/exceptions.py b/pipelex/core/exceptions.pyindex 17d7c49e0..51a1af338 100644--- a/pipelex/core/exceptions.py+++ b/pipelex/core/exceptions.py@@ -1,6 +1,30 @@from pydantic import BaseModel, Field-from pipelex.core.pipes.exceptions import PipeValidationErrorType+from pipelex.core.pipes.exceptions import PipeFactoryErrorType, PipeValidationErrorType+++class PipeFactoryErrorData(BaseModel):+ """Structured error data for Pipe factory errors.++ This model captures errors raised during pipe creation from blueprints,+ particularly missing concept errors that can be auto-fixed by the builder loop.+ """++ # === Error Classification ===+ error_type: PipeFactoryErrorType = Field(+ description="Type of pipe factory error",+ )++ # === Source Context ===+ domain: str | None = Field(None, description="Domain where error occurred")++ # === Entity Context (what failed) ===+ pipe_code: str | None = Field(None, description="Pipe code that failed to be created")+ missing_concept_code: str | None = Field(None, description="The concept code that is missing")+ declared_concepts: list[str] = Field(default_factory=list, description="List of concepts declared in the domain")++ # === Error Details ===+ message: str = Field(description="Human-readable error message")class PipesAndConceptValidationErrorData(BaseModel):diff --git a/pipelex/core/pipes/exceptions.py b/pipelex/core/pipes/exceptions.pyindex 9b4d55227..b8c4a8f1b 100644--- a/pipelex/core/pipes/exceptions.py+++ b/pipelex/core/pipes/exceptions.py@@ -8,8 +8,50 @@ from pipelex.cogt.model_backends.model_type import ModelTypefrom pipelex.types import StrEnum+class PipeFactoryErrorType(StrEnum):+ """Types of pipe factory errors.++ These error types are raised during pipe creation from blueprints.+ Some are auto-fixed in the builder loop (marked below).+ """++ # Errors that are auto-fixed in builder_loop.py+ UNKNOWN_CONCEPT = "unknown_concept" # AUTO-FIXED: concept not declared in domain++ # Generic fallback for unexpected factory errors+ UNKNOWN_FACTORY_ERROR = "unknown_factory_error"++class PipeFactoryError(PipelexError):- pass+ """Raised when a pipe cannot be created from a blueprint.++ This error includes structured data about the failure, particularly useful+ for missing concept errors that can be auto-fixed by the builder loop.++ Attributes:+ message: Human-readable error message+ error_type: The type of factory error+ pipe_code: The pipe code that failed to be created+ domain: The domain of the pipe+ missing_concept_code: The concept code that is missing (for MISSING_OUTPUT_CONCEPT errors)+ declared_concepts: List of concepts declared in the domain+ """++ def __init__(+ self,+ message: str,+ error_type: PipeFactoryErrorType = PipeFactoryErrorType.UNKNOWN_FACTORY_ERROR,+ pipe_code: str | None = None,+ domain: str | None = None,+ missing_concept_code: str | None = None,+ declared_concepts: list[str] | None = None,+ ):+ self.error_type = error_type+ self.pipe_code = pipe_code+ self.domain = domain+ self.missing_concept_code = missing_concept_code+ self.declared_concepts = declared_concepts or []+ super().__init__(message)class PipeVariableMultiplicityError(ValueError):diff --git a/pipelex/core/pipes/handle_pipe_errors.py b/pipelex/core/pipes/handle_pipe_errors.pyindex 0a4993230..9a4be7b35 100644--- a/pipelex/core/pipes/handle_pipe_errors.py+++ b/pipelex/core/pipes/handle_pipe_errors.py@@ -1,9 +1,9 @@from pydantic import ValidationErrorfrom pydantic_core import ErrorDetails-from pipelex.core.exceptions import PipesAndConceptValidationErrorData+from pipelex.core.exceptions import PipeFactoryErrorData, PipesAndConceptValidationErrorDatafrom pipelex.core.interpreter.validation_error_categorizer import ErrorCatKey-from pipelex.core.pipes.exceptions import PipeValidationError, PipeValidationErrorType+from pipelex.core.pipes.exceptions import PipeFactoryError, PipeValidationError, PipeValidationErrorTypefrom pipelex.types import StrEnum@@ -162,3 +162,26 @@ def categorize_pipe_validation_with_libraries_error(field_path=pipe_error.file_path or "",variable_names=pipe_error.variable_names,)+++def categorize_pipe_factory_error(+ factory_error: PipeFactoryError,+) -> PipeFactoryErrorData:+ """Categorize a PipeFactoryError and create structured error data.++ This function handles errors from PipeFactory, particularly missing concept errors.++ Args:+ factory_error: PipeFactoryError with structured error information++ Returns:+ PipeFactoryErrorData with all relevant fields populated+ """+ return PipeFactoryErrorData(+ error_type=factory_error.error_type,+ domain=factory_error.domain,+ pipe_code=factory_error.pipe_code,+ missing_concept_code=factory_error.missing_concept_code,+ declared_concepts=factory_error.declared_concepts,+ message=factory_error.message,+ )diff --git a/pipelex/core/pipes/pipe_factory.py b/pipelex/core/pipes/pipe_factory.pyindex 9cda5ca69..c57e8b89b 100644--- a/pipelex/core/pipes/pipe_factory.py+++ b/pipelex/core/pipes/pipe_factory.py@@ -9,7 +9,7 @@ from pipelex.core.concepts.concept_factory import ConceptFactoryfrom pipelex.core.concepts.exceptions import ConceptFactoryErrorfrom pipelex.core.concepts.helpers import strip_multiplicity_from_concept_string_or_codefrom pipelex.core.concepts.native.concept_native import NativeConceptCode-from pipelex.core.pipes.exceptions import PipeFactoryError, PipeVariableMultiplicityError+from pipelex.core.pipes.exceptions import PipeFactoryError, PipeFactoryErrorType, PipeVariableMultiplicityErrorfrom pipelex.core.pipes.inputs.input_requirements import InputRequirementsfrom pipelex.core.pipes.inputs.input_requirements_factory import InputRequirementsFactoryfrom pipelex.core.pipes.pipe_blueprint import PipeBlueprint, PipeType@@ -82,7 +82,14 @@ class PipeFactory(Generic[PipeAbstractType]):f"The concept must be either native, declared in domain '{domain_code}', or fully qualified with a domain prefix. "f"Declared concepts are: '{concept_codes_from_the_same_domain}'")- raise PipeFactoryError(msg)+ raise PipeFactoryError(+ message=msg,+ error_type=PipeFactoryErrorType.UNKNOWN_CONCEPT,+ pipe_code=pipe_code,+ domain=domain_code,+ missing_concept_code=stripped_output_concept_string_or_code,+ declared_concepts=concept_codes_from_the_same_domain,+ )# Parse common attributesparsed_output = cls._parse_output_concept_string(domain=domain_code, pipe_code=pipe_code, output_string=blueprint.output)diff --git a/pipelex/pipeline/validate_bundle.py b/pipelex/pipeline/validate_bundle.pyindex 7d7bc17cc..f9fb54268 100644--- a/pipelex/pipeline/validate_bundle.py+++ b/pipelex/pipeline/validate_bundle.py@@ -5,11 +5,12 @@ from pydantic import BaseModel, ValidationErrorfrom pipelex.base_exceptions import PipelexErrorfrom pipelex.core.bundles.exceptions import PipelexBundleBlueprintValidationErrorDatafrom pipelex.core.bundles.pipelex_bundle_blueprint import PipelexBundleBlueprint-from pipelex.core.exceptions import PipesAndConceptValidationErrorData+from pipelex.core.exceptions import PipeFactoryErrorData, PipesAndConceptValidationErrorDatafrom pipelex.core.interpreter.exceptions import PipelexInterpreterErrorfrom pipelex.core.interpreter.interpreter import PipelexInterpreter-from pipelex.core.pipes.exceptions import PipeValidationError+from pipelex.core.pipes.exceptions import PipeFactoryError, PipeValidationErrorfrom pipelex.core.pipes.handle_pipe_errors import (+ categorize_pipe_factory_error,categorize_pipe_validation_error,categorize_pipe_validation_with_libraries_error,)@@ -25,6 +26,7 @@ class ValidateBundleError(PipelexError):This error aggregates validation errors from different stages:- Blueprint validation errors (from interpreter)+ - Pipe factory errors (from PipeFactoryError exceptions, e.g., missing concepts)- Pipe validation errors (from PipeValidationError exceptions)- Pipe/Concept instantiation errors (from Pydantic ValidationError during factory instantiation)- Dry run errors@@ -36,6 +38,7 @@ class ValidateBundleError(PipelexError):self,message: str,pipelex_bundle_blueprint_validation_errors: list[PipelexBundleBlueprintValidationErrorData] | None = None,+ pipe_factory_errors: list[PipeFactoryErrorData] | None = None,pipe_validation_errors: list[PipesAndConceptValidationErrorData] | None = None,pipe_concept_instantiation_errors: list[PipesAndConceptValidationErrorData] | None = None,dry_run_error_message: str | None = None,@@ -43,6 +46,9 @@ class ValidateBundleError(PipelexError):# Blueprint validation errors (e.g., PIPE_SEQUENCE_OUTPUT_MISMATCH)self.pipelex_bundle_blueprint_validation_errors = pipelex_bundle_blueprint_validation_errors or []+ # Pipe factory errors (e.g., MISSING_OUTPUT_CONCEPT)+ self.pipe_factory_errors = pipe_factory_errors or []+# Pipe validation errors from PipeValidationError exceptions# (e.g., MISSING_INPUT_VARIABLE, EXTRANEOUS_INPUT_VARIABLE, INPUT_REQUIREMENT_MISMATCH, INADEQUATE_OUTPUT_CONCEPT)self.pipe_validation_errors = pipe_validation_errors or []@@ -111,6 +117,12 @@ async def validate_bundle(message=interpreter_error.message,pipelex_bundle_blueprint_validation_errors=interpreter_error.validation_errors,) from interpreter_error+ except PipeFactoryError as factory_error:+ factory_error_data = categorize_pipe_factory_error(factory_error=factory_error)+ raise ValidateBundleError(+ message=f"Pipe factory error: {factory_error}",+ pipe_factory_errors=[factory_error_data],+ ) from factory_errorexcept PipeValidationError as pipe_error:pipe_error_data = categorize_pipe_validation_with_libraries_error(pipe_error=pipe_error)raise ValidateBundleError(@@ -153,6 +165,12 @@ async def validate_bundles_from_directory(directory: Path) -> ValidateBundleResumessage=interpreter_error.message,pipelex_bundle_blueprint_validation_errors=interpreter_error.validation_errors,) from interpreter_error+ except PipeFactoryError as factory_error:+ factory_error_data = categorize_pipe_factory_error(factory_error=factory_error)+ raise ValidateBundleError(+ message=f"Pipe factory error: {factory_error}",+ pipe_factory_errors=[factory_error_data],+ ) from factory_errorexcept PipeValidationError as pipe_error:pipe_error_data = categorize_pipe_validation_with_libraries_error(pipe_error=pipe_error)raise ValidateBundleError(