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.

dev to release/v0.3.2 (#77)

Thomas Hebrard8fe5561922f3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 541d2e980..89240c8c8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog
+## [v0.3.2] - 2025-06-12
+
+- Added a better printing of class structure (corresponding to the `output_concept_code`) at the end of the prompts. (Coming with UT)
+- The ReportingManager now reports all `pipeline_run_id` when none is provided
+- The `make_from_str` method from the `StuffFactory` now has a default value for `concept_code`: `native.Text`
+
## [v0.3.1] - 2025-06-10
### Added
diff --git a/README.md b/README.md
index c22966a75..3a163a8ef 100644
--- a/README.md
+++ b/README.md
@@ -26,7 +26,15 @@
</div>
<div align="center">
- <a href="https://www.pipelex.com/demo"><strong>Checkout our demo!</strong></a>
+ <h2 align="center">πŸš€ See Pipelex in Action</h2>
+ <p align="center">
+ <a href="https://www.pipelex.com/demo"><strong>Checkout our Demo</strong></a>
+ </p>
+
+ <h2 align="center">πŸ“œ Discover Our Vision</h2>
+ <p align="center">
+ <a href="https://www.pipelex.com/post/the-knowledge-pipeline-manifesto"><strong>Read our Manifesto</strong></a>
+ </p>
</div>
# πŸ“‘ Table of Contents
diff --git a/pipelex/cogt/llm/llm_worker_abstract.py b/pipelex/cogt/llm/llm_worker_abstract.py
index 23b097aba..1de792eca 100644
--- a/pipelex/cogt/llm/llm_worker_abstract.py
+++ b/pipelex/cogt/llm/llm_worker_abstract.py
@@ -131,7 +131,9 @@ class LLMWorkerAbstract(InferenceWorkerAbstract, ABC):
result = await self._gen_object(llm_job=llm_job, schema=schema)
except InstructorRetryException as exc:
raise LLMCompletionError(
- f"LLM Worker error: Instructor failed after retry with llm '{self.llm_engine.tag}': {exc}\nLLMPrompt: {llm_job.llm_prompt.desc}"
+ f"""Instructor failed to generate object: {schema} after retry with llm '{self.llm_engine.tag}'
+ Reason: {exc}
+ LLMPrompt: {llm_job.llm_prompt.desc}"""
) from exc
# Cleanup result
diff --git a/pipelex/core/stuff.py b/pipelex/core/stuff.py
index ae6b3b9c1..4d4e211d8 100644
--- a/pipelex/core/stuff.py
+++ b/pipelex/core/stuff.py
@@ -136,7 +136,7 @@ class Stuff(BaseModel):
# Validate all items are of the expected type
for i, item in enumerate(list_content.items):
if not isinstance(item, item_type):
- raise TypeError(f"Item {i} in list is of type {type(item)}, not {item_type}")
+ raise TypeError(f"Item {i} in list is of type {type(item)}, not {item_type}, in {self.stuff_name=} and {self.concept_code=}")
return list_content
diff --git a/pipelex/core/stuff_factory.py b/pipelex/core/stuff_factory.py
index a64b2e8bb..412eb28d8 100644
--- a/pipelex/core/stuff_factory.py
+++ b/pipelex/core/stuff_factory.py
@@ -95,9 +95,9 @@ class StuffFactory:
@classmethod
def make_from_str(
cls,
- concept_code: str,
str_value: str,
name: Optional[str] = None,
+ concept_code: str = NativeConcept.TEXT.code,
pipelex_session_id: Optional[str] = None,
) -> Stuff:
if not Concept.concept_str_contains_domain(concept_code):
diff --git a/pipelex/pipe_operators/pipe_llm.py b/pipelex/pipe_operators/pipe_llm.py
index 272ac266e..1a25c16da 100644
--- a/pipelex/pipe_operators/pipe_llm.py
+++ b/pipelex/pipe_operators/pipe_llm.py
@@ -219,11 +219,14 @@ class PipeLLM(PipeOperator):
# TODO: This DYNAMIC_OUTPUT_CONCEPT should not be a field in the params attribute of PipeRunParams.
# It should be an attribute of PipeRunParams.
output_concept_code = pipe_run_params.dynamic_output_concept_code or pipe_run_params.params.get(PipeRunParamKey.DYNAMIC_OUTPUT_CONCEPT)
+
if not output_concept_code:
raise RuntimeError(f"No output concept code provided for dynamic output pipe '{self.code}'")
else:
output_concept_code = self.output_concept_code
+ self.pipe_llm_prompt.output_concept_code = output_concept_code
+
applied_output_multiplicity, is_multiple_output, fixed_nb_output = output_multiplicity_to_apply(
output_multiplicity_base=self.output_multiplicity,
output_multiplicity_override=pipe_run_params.output_multiplicity,
@@ -318,6 +321,7 @@ class PipeLLM(PipeOperator):
domain=self.domain,
user_pipe_jinja2=user_pipe_jinja2,
system_prompt=system_prompt,
+ output_concept_code=output_concept_code,
)
llm_prompt_2_factory = PipedLLMPromptFactory(
pipe_llm_prompt=pipe_llm_prompt_2,
@@ -340,6 +344,7 @@ class PipeLLM(PipeOperator):
domain=self.domain,
user_pipe_jinja2=user_pipe_jinja2,
system_prompt=system_prompt,
+ output_concept_code=output_concept_code,
)
llm_prompt_2_factory = PipedLLMPromptFactory(
pipe_llm_prompt=pipe_llm_prompt_2,
diff --git a/pipelex/pipe_operators/pipe_llm_prompt.py b/pipelex/pipe_operators/pipe_llm_prompt.py
index 34cd6cd5f..bcbd228e1 100644
--- a/pipelex/pipe_operators/pipe_llm_prompt.py
+++ b/pipelex/pipe_operators/pipe_llm_prompt.py
@@ -174,6 +174,10 @@ class PipeLLMPrompt(PipeOperator):
# Append output structure prompt if needed
if pipe_run_params.dynamic_output_concept_code:
user_text += PipeLLMPrompt.get_output_structure_prompt(output_concept=pipe_run_params.dynamic_output_concept_code)
+ else:
+ user_text += PipeLLMPrompt.get_output_structure_prompt(output_concept=self.output_concept_code)
+
+ log.verbose(f"User text with {self.output_concept_code=}:\n {user_text}")
############################################################
# System text
@@ -219,16 +223,18 @@ class PipeLLMPrompt(PipeOperator):
if not output_class:
return ""
- fields = get_type_structure(output_class, base_class=StuffContent)
+ class_structure = get_type_structure(output_class, base_class=StuffContent)
- if not fields:
+ if not class_structure:
return ""
output_structure_prompt = (
- f"\n\n---\nRequested output format: The output should contain the following fields:\n"
- f"{chr(10).join(fields)}\n"
+ f"\n\n---\nRequested output format: The output should be the following class: {class_name}\n"
+ f"{chr(10).join(class_structure)}\n"
"You do NOT need to output a formatted JSON object, another LLM will take care of that. "
+ "If you cannot find a value that is Optional, output None for that field."
"However, you MUST clearly output the values for each of these fields in your response.\n---\n"
+ "DO NOT create information. If the information is not present, output None."
)
return output_structure_prompt
diff --git a/pipelex/reporting/reporting_manager.py b/pipelex/reporting/reporting_manager.py
index 761e5582a..708a4f416 100644
--- a/pipelex/reporting/reporting_manager.py
+++ b/pipelex/reporting/reporting_manager.py
@@ -94,7 +94,6 @@ class ReportingManager(ReportingProtocol):
@override
def generate_report(self, pipeline_run_id: Optional[str] = None):
- pipeline_run_id = pipeline_run_id or SpecialPipelineId.UNTITLED
cost_report_file_path: Optional[str] = None
if self._reporting_config.is_generate_cost_report_file_enabled:
ensure_path(self._reporting_config.cost_report_dir_path)
@@ -104,13 +103,19 @@ class ReportingManager(ReportingProtocol):
extension=self._reporting_config.cost_report_extension,
)
- registry = self._get_registry(pipeline_run_id)
- CostRegistry.generate_report(
- pipeline_run_id=pipeline_run_id,
- llm_tokens_usages=registry.get_current_tokens_usage(),
- unit_scale=self._reporting_config.cost_report_unit_scale,
- cost_report_file_path=cost_report_file_path,
- )
+ registries_to_process: Dict[str, UsageRegistry] = {}
+ if pipeline_run_id:
+ registries_to_process = {pipeline_run_id: self._get_registry(pipeline_run_id)}
+ else:
+ registries_to_process = self._usage_registries
+
+ for run_id, registry in registries_to_process.items():
+ CostRegistry.generate_report(
+ pipeline_run_id=run_id,
+ llm_tokens_usages=registry.get_current_tokens_usage(),
+ unit_scale=self._reporting_config.cost_report_unit_scale,
+ cost_report_file_path=cost_report_file_path,
+ )
@override
def close_registry(self, pipeline_run_id: str):
diff --git a/pipelex/tools/typing/type_inspector.py b/pipelex/tools/typing/type_inspector.py
index 0ebbf10b7..84a866320 100644
--- a/pipelex/tools/typing/type_inspector.py
+++ b/pipelex/tools/typing/type_inspector.py
@@ -2,6 +2,7 @@ from enum import Enum
from typing import Any, Dict, List, Optional, Set, Type, Union, get_type_hints
from pydantic import BaseModel
+from typing_extensions import get_args, get_origin
from pipelex.types import StrEnum
@@ -26,9 +27,12 @@ def pretty_type(tp: object) -> str:
values: List[str] = []
for arg in args:
if isinstance(arg, Enum) or isinstance(arg, StrEnum):
- values.append(f"'{arg.value}'")
+ values.append(f'"{arg.value}"')
else:
values.append(repr(arg))
+ # Return multi-line format for Literal fields
+ if len(values) > 1:
+ return "Literal[\n " + ",\n ".join(values) + ",\n ]"
return f"Literal[{', '.join(values)}]"
if (origin is list or origin is List) and args:
@@ -39,49 +43,123 @@ def pretty_type(tp: object) -> str:
def get_type_structure(
- tp: object,
+ tp: Type[Any],
seen_types: Optional[Set[str]] = None,
- collected: Optional[List[Type[Any]]] = None,
+ collected_types: Optional[Dict[str, Type[Any]]] = None,
+ collected_enums: Optional[Dict[str, Type[Enum]]] = None,
base_class: Type[Any] = BaseModel,
) -> List[str]:
"""
- Get the structure of a type, listing referenced subclasses of base_class after the parent class.
+ Get the structure of a type, listing referenced subclasses of base_class and enums.
Args:
tp: The type to analyze
seen_types: Set of already seen type names to avoid cycles
- collected: List of collected types to analyze
+ collected_types: Dictionary of collected types to analyze
+ collected_enums: Dictionary of collected enums
base_class: The base class to check for inheritance (defaults to BaseModel)
"""
if seen_types is None:
seen_types = set()
- if collected is None:
- collected = []
+ if collected_types is None:
+ collected_types = {}
+ if collected_enums is None:
+ collected_enums = {}
- def collect_types(tp: object):
- origin = getattr(tp, "__origin__", None)
- args = getattr(tp, "__args__", None)
- if origin and args:
+ def format_type(tp: Any) -> str:
+ """Format a type annotation nicely"""
+ origin = get_origin(tp)
+ if origin is None:
+ if isinstance(tp, type):
+ return tp.__name__
+ return str(tp)
+
+ args = get_args(tp)
+ if origin is Union:
+ non_none = [a for a in args if a is not type(None)]
+ if len(non_none) == 1:
+ return f"Optional[{format_type(non_none[0])}]"
+ return f"Union[{', '.join(format_type(a) for a in non_none)}]"
+
+ if str(origin).endswith("Literal") and args: # Handle both typing.Literal and typing_extensions.Literal
+ # For enum values, just get their values
+ values: List[str] = []
+ enum_type = None
+ for arg in args:
+ if isinstance(arg, Enum) or isinstance(arg, StrEnum):
+ values.append(arg.value)
+ if enum_type is None:
+ enum_type = type(arg)
+ else:
+ values.append(str(arg))
+ # Add enum type to collected_enums if found
+ if enum_type is not None:
+ collected_enums[enum_type.__name__] = enum_type
+ # Return multi-line format for Literal fields
+ if len(values) > 1:
+ lines: List[str] = []
+ for value in values:
+ lines.append(f'"{value}"')
+ return "Literal[\n " + ",\n ".join(lines) + ",\n ]"
+ return f"Literal[{', '.join(values)}]"
+
+ if origin in (list, List):
+ return f"List[{format_type(args[0])}]"
+ if origin in (dict, Dict):
+ return f"Dict[{format_type(args[0])}, {format_type(args[1])}]"
+ return str(tp)
+
+ def collect_types(tp: Type[Any]) -> None:
+ """Recursively collect types and enums"""
+ origin = get_origin(tp)
+ args = get_args(tp)
+
+ if origin:
if origin is Union:
non_none = [a for a in args if a is not type(None)]
- if len(non_none) == 1:
- collect_types(non_none[0])
- else:
- for arg in non_none:
+ for arg in non_none:
+ if isinstance(arg, type):
+ collect_types(arg)
+ elif hasattr(arg, "__origin__"): # Handle nested generics
collect_types(arg)
elif origin in (list, List):
- collect_types(args[0])
+ if isinstance(args[0], type):
+ collect_types(args[0])
+ elif hasattr(args[0], "__origin__"): # Handle nested generics
+ collect_types(args[0])
elif origin in (dict, Dict):
- collect_types(args[0])
- collect_types(args[1])
+ for arg in args:
+ if isinstance(arg, type):
+ collect_types(arg)
+ elif hasattr(arg, "__origin__"): # Handle nested generics
+ collect_types(arg)
return
- if isinstance(tp, type):
- if issubclass(tp, base_class) and tp.__name__ not in seen_types:
- seen_types.add(tp.__name__)
- collected.append(tp)
+ # Collect enums
+ if issubclass(tp, Enum) and tp not in collected_enums.values():
+ collected_enums[tp.__name__] = tp
+ return
+
+ # Collect model classes
+ if issubclass(tp, base_class) and tp.__name__ not in seen_types:
+ seen_types.add(tp.__name__)
+ collected_types[tp.__name__] = tp
+
+ # Only collect immediate parent class if it's a custom class
+ for base in tp.__bases__:
+ if (
+ issubclass(base, BaseModel)
+ and base is not BaseModel
+ and base.__module__ != "pydantic.main"
+ and base.__module__ != "abc"
+ and not base.__module__.startswith("pipelex.core")
+ ):
+ collect_types(base)
+
+ try:
type_hints = get_type_hints(tp)
- model_fields: Dict[str, Any] = getattr(tp, "model_fields", {})
+ model_fields = getattr(tp, "model_fields", {})
+
if model_fields:
for fname, _ in model_fields.items():
ftype = type_hints[fname]
@@ -89,31 +167,161 @@ def get_type_structure(
elif hasattr(tp, "__annotations__"):
for fname, ftype in type_hints.items():
collect_types(ftype)
+ except (TypeError, AttributeError):
+ # Handle cases where type hints cannot be retrieved
+ pass
+ # Start collection
collect_types(tp)
+
+ # Generate output
output: List[str] = []
- for idx, ctp in enumerate(collected):
- if idx > 0:
+
+ # First output the main class and its dependencies
+ for class_name, class_type in collected_types.items():
+ if output:
output.append("")
- output.extend(pretty_print_class_structure(ctp))
- return output
+ # Get class docstring
+ doc = class_type.__doc__ and class_type.__doc__.strip()
+ base_class_name = class_type.__bases__[0].__name__
+
+ # Get generic parameters if any
+ type_args = get_args(class_type)
+ if type_args:
+ base_class_name = f"{base_class_name}[{', '.join(arg.__name__ for arg in type_args)}]"
+
+ # Class definition with docstring
+ output.append(f"class {class_name}({base_class_name}):")
+ if doc:
+ # Split docstring into lines and format each line
+ doc_lines = [line.rstrip() for line in doc.split("\n")]
-def pretty_print_class_structure(tp: Type[Any]) -> List[str]:
- lines: List[str] = []
- lines.append(f"Class '{tp.__name__}':")
- if tp.__doc__:
- lines.append(f"{tp.__doc__}")
- type_hints = get_type_hints(tp)
- model_fields: Dict[str, Any] = getattr(tp, "model_fields", {})
- if model_fields:
- for fname, f in model_fields.items():
- ftype = type_hints[fname]
- line = f"- {fname} ({pretty_type(ftype)})"
- if hasattr(f, "description") and getattr(f, "description", None):
- line += f": {f.description}"
- lines.append(line)
- elif hasattr(tp, "__annotations__"):
- for fname, ftype in type_hints.items():
- lines.append(f"- {fname} ({str(ftype)})")
- return lines
+ # Remove empty lines from start and end
+ while doc_lines and not doc_lines[0].strip():
+ doc_lines.pop(0)
+ while doc_lines and not doc_lines[-1].strip():
+ doc_lines.pop()
+
+ if len(doc_lines) == 1:
+ # Single line docstring
+ output.append(f' """{doc_lines[0]}"""')
+ else:
+ # Multi-line docstring
+ output.append(f' """{doc_lines[0]}')
+
+ # Add empty line after first line if there's content
+ if doc_lines[1].strip():
+ output.append("")
+
+ # Add remaining lines with proper indentation
+ for line in doc_lines[1:]:
+ if line.strip():
+ output.append(f" {line.strip()}")
+ else:
+ output.append("")
+
+ # Close the docstring
+ output.append(' """')
+
+ # Handle empty classes or classes that only inherit fields
+ try:
+ type_hints = get_type_hints(class_type)
+ model_fields = getattr(class_type, "model_fields", {})
+
+ # Get and sort fields
+ if model_fields:
+ fields = model_fields.items()
+ else:
+ fields = [(k, type_hints[k]) for k in sorted(type_hints.keys())]
+
+ # Check if all fields are inherited
+ parent_fields: Set[str] = set()
+ for base in class_type.__bases__:
+ try:
+ parent_fields.update(get_type_hints(base).keys())
+ except (TypeError, AttributeError):
+ continue
+
+ current_fields = set(dict(fields).keys())
+ non_inherited_fields = current_fields - parent_fields
+
+ # Output fields
+ for fname, ftype in fields:
+ if fname in non_inherited_fields or (fname == "items" and "List" in base_class_name):
+ # Initialize is_optional to False by default
+ is_optional = False
+
+ if isinstance(ftype, type) and issubclass(ftype, BaseModel):
+ ftype_str = ftype.__name__
+ else:
+ field_type = type_hints[fname]
+ ftype_str = format_type(field_type)
+ # Check if field is Optional
+ field_origin = get_origin(field_type)
+ field_args = get_args(field_type)
+ is_optional = field_origin is Union and type(None) in field_args
+
+ # Handle default values
+ field_default = None
+ field_description = None
+ if model_fields:
+ field_info = model_fields.get(fname)
+ if field_info:
+ if hasattr(field_info, "default") and field_info.default is not None:
+ # Skip PydanticUndefined default values
+ if str(field_info.default) != "PydanticUndefined":
+ field_default = field_info.default
+ if hasattr(field_info, "description"):
+ field_description = field_info.description
+
+ # Build the field line
+ field_line = f" {fname}: {ftype_str}"
+ if is_optional:
+ field_line += " = None"
+ elif field_default is not None:
+ if isinstance(field_default, bool):
+ field_line += f" = {str(field_default)}"
+ elif not isinstance(field_default, (BaseModel, list, dict)):
+ field_line += f" = {repr(field_default)}"
+
+ # Add description as a comment if available
+ # First check if there's a direct field description from model_fields
+ if field_description:
+ field_line += f" # {field_description}"
+ # Then check if the field type itself has model_fields and a description
+ # This handles nested content types that have field descriptions
+ elif hasattr(ftype, "model_fields") and fname in ftype.model_fields and hasattr(ftype.model_fields[fname], "description"): # type: ignore
+ field_line += f" # {ftype.model_fields[fname].description}" # type: ignore
+
+ # Split multi-line field lines
+ if "\n" in field_line:
+ lines = field_line.split("\n")
+ output.extend(lines)
+ else:
+ output.append(field_line)
+ continue
+
+ # If no fields were output, show inheritance comment
+ if len(output) == (2 if doc else 1):
+ output.append(f" # Inherits from {base_class_name}")
+ output.append(" # No additional fields")
+ except (TypeError, AttributeError):
+ # If we can't get type hints, show inheritance comment
+ output.append(f" # Inherits from {base_class_name}")
+ output.append(" # No additional fields")
+
+ # Then output all enum classes
+ for enum_name, enum_type in collected_enums.items():
+ if output:
+ output.append("")
+ output.append(f"class {enum_name}({enum_type.__bases__[0].__name__}):")
+ # Add enum docstring if available, but skip Python's default "An enumeration." docstring
+ # This ensures we only include meaningful custom docstrings
+ if enum_type.__doc__ and enum_type.__doc__.strip() != "An enumeration.":
+ doc = enum_type.__doc__.strip()
+ output.append(f' """{doc}"""')
+ for member in enum_type:
+ output.append(f' {member.name} = "{member.value}"')
+
+ return output
diff --git a/pyproject.toml b/pyproject.toml
index 1130c1845..f6cddd67f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[project]
name = "pipelex"
-version = "0.3.1"
+version = "0.3.2"
description = "Pipelex is an open-source dev tool based on a simple declarative language that lets you define replicable, structured, composable LLM pipelines."
authors = [{ name = "Evotis S.A.S.", email = "[email protected]" }]
maintainers = [{ name = "Pipelex staff", email = "[email protected]" }]
diff --git a/tests/pipelex/pipelex_asynch/test_pipe_llm_prompt.py b/tests/pipelex/pipelex_asynch/test_pipe_llm_prompt.py
new file mode 100644
index 000000000..780515b49
--- /dev/null
+++ b/tests/pipelex/pipelex_asynch/test_pipe_llm_prompt.py
@@ -0,0 +1,184 @@
+import pytest
+
+from pipelex.core.concept_native import NativeConcept
+from pipelex.core.pipe_run_params import PipeRunMode
+from pipelex.core.pipe_run_params_factory import PipeRunParamsFactory
+from pipelex.core.working_memory_factory import WorkingMemoryFactory
+from pipelex.hub import get_pipe_router
+from pipelex.pipe_operators.pipe_llm_prompt import PipeLLMPrompt, PipeLLMPromptOutput
+from pipelex.pipe_works.pipe_job_factory import PipeJobFactory
+from pipelex.tools.templating.templating_models import PromptingStyle, TagStyle, TextFormat
+from tests.pipelex.test_data import PipeTestCases
+from tests.test_pipelines.pipe_llm_prompt import (
+ ComplexListContent,
+ DocumentTypeContent,
+ MusicCategoryContent,
+ PersonContent,
+ SimpleStructuredContent,
+ SimpleTextContent,
+)
+
+
[email protected](loop_scope="class")
+class TestPipeLLMPrompt:
+ async def test_simple_prompt(self, pipe_run_mode: PipeRunMode):
+ """Test basic prompt with system and user text."""
+ pipe_llm_prompt = PipeLLMPrompt(
+ code="test_simple_prompt",
+ domain="generic",
+ system_prompt=PipeTestCases.SYSTEM_PROMPT,
+ user_text=PipeTestCases.USER_PROMPT,
+ output_concept_code=NativeConcept.TEXT.code,
+ )
+
+ pipe_output: PipeLLMPromptOutput = await get_pipe_router().run_pipe_job(
+ pipe_job=PipeJobFactory.make_pipe_job(
+ pipe=pipe_llm_prompt,
+ pipe_run_params=PipeRunParamsFactory.make_run_params(pipe_run_mode=pipe_run_mode),
+ )
+ )
+
+ assert pipe_output is not None
+ assert pipe_output.working_memory is not None
+ assert pipe_output.main_stuff is not None
+ assert pipe_output.llm_prompt is not None
+ assert pipe_output.llm_prompt.user_text is not None
+ assert pipe_output.llm_prompt.user_text.endswith(PipeLLMPrompt.get_output_structure_prompt(NativeConcept.TEXT.code))
+
+ async def test_prompt_with_images(self, pipe_run_mode: PipeRunMode):
+ """Test prompt with image inputs."""
+ stuff_name = "image"
+ working_memory = WorkingMemoryFactory.make_from_single_stuff(stuff=PipeTestCases.SIMPLE_STUFF_IMAGE)
+
+ pipe_llm_prompt = PipeLLMPrompt(
+ code="test_prompt_with_images",
+ domain="generic",
+ system_prompt=PipeTestCases.SYSTEM_PROMPT,
+ user_text=PipeTestCases.MULTI_IMG_DESC_PROMPT,
+ user_images=[stuff_name], # Just use the stuff name, the content will be extracted as ImageContent
+ output_concept_code=NativeConcept.TEXT.code,
+ )
+
+ pipe_output: PipeLLMPromptOutput = await get_pipe_router().run_pipe_job(
+ pipe_job=PipeJobFactory.make_pipe_job(
+ pipe=pipe_llm_prompt,
+ pipe_run_params=PipeRunParamsFactory.make_run_params(pipe_run_mode=pipe_run_mode),
+ working_memory=working_memory,
+ )
+ )
+
+ assert pipe_output is not None
+ assert pipe_output.working_memory is not None
+ assert pipe_output.main_stuff is not None
+
+ async def test_prompt_with_output_structure(self, pipe_run_mode: PipeRunMode):
+ """Test prompt with output structure for different content types."""
+ test_cases = [
+ (SimpleTextContent, "Empty class inheritance", "test.SimpleTextContent"),
+ (SimpleStructuredContent, "Primitive types", "test.SimpleStructuredContent"),
+ (DocumentTypeContent, "Enum fields", "test.DocumentTypeContent"),
+ (PersonContent, "Nested content", "test.PersonContent"),
+ (ComplexListContent, "List content", "test.ComplexListContent"),
+ (MusicCategoryContent, "Music genre", "test.MusicCategoryContent"),
+ ]
+
+ for content_class, description, concept_code in test_cases:
+ pipe_llm_prompt = PipeLLMPrompt(
+ code=f"test_output_structure_{content_class.__name__}",
+ domain="generic",
+ system_prompt=PipeTestCases.SYSTEM_PROMPT,
+ user_text=f"Generate content for {description}",
+ output_concept_code=concept_code,
+ )
+
+ pipe_output: PipeLLMPromptOutput = await get_pipe_router().run_pipe_job(
+ pipe_job=PipeJobFactory.make_pipe_job(
+ pipe=pipe_llm_prompt,
+ pipe_run_params=PipeRunParamsFactory.make_run_params(pipe_run_mode=pipe_run_mode),
+ )
+ )
+
+ assert pipe_output is not None
+ assert pipe_output.working_memory is not None
+ assert pipe_output.main_stuff is not None
+
+ # Verify output structure is appended
+ output_structure = pipe_llm_prompt.get_output_structure_prompt(concept_code)
+
+ assert pipe_output.llm_prompt is not None
+ assert pipe_output.llm_prompt.user_text is not None
+ assert output_structure in pipe_output.llm_prompt.user_text
+
+ # Verify docstrings and field descriptions are included
+ if content_class.__doc__:
+ assert content_class.__doc__ in output_structure
+
+ # Verify field descriptions
+ for _, field in content_class.model_fields.items():
+ if field.description:
+ assert field.description in output_structure
+
+ async def test_prompt_with_prompting_style(self, pipe_run_mode: PipeRunMode):
+ """Test prompt with different prompting styles."""
+ prompting_style = PromptingStyle(
+ tag_style=TagStyle.NO_TAG,
+ text_format=TextFormat.PLAIN,
+ )
+ pipe_llm_prompt = PipeLLMPrompt(
+ code="test_prompting_style",
+ domain="generic",
+ system_prompt=PipeTestCases.SYSTEM_PROMPT,
+ user_text=PipeTestCases.USER_PROMPT,
+ prompting_style=prompting_style,
+ output_concept_code=NativeConcept.TEXT.code,
+ )
+
+ pipe_output: PipeLLMPromptOutput = await get_pipe_router().run_pipe_job(
+ pipe_job=PipeJobFactory.make_pipe_job(
+ pipe=pipe_llm_prompt,
+ pipe_run_params=PipeRunParamsFactory.make_run_params(pipe_run_mode=pipe_run_mode),
+ )
+ )
+
+ assert pipe_output is not None
+ assert pipe_output.working_memory is not None
+ assert pipe_output.main_stuff is not None
+
+ async def test_prompt_validation_errors(self):
+ """Test validation errors for invalid prompt configurations."""
+ # Test missing user text
+ with pytest.raises(Exception) as exc_info:
+ PipeLLMPrompt(
+ code="test_validation_error",
+ domain="generic",
+ system_prompt=PipeTestCases.SYSTEM_PROMPT,
+ output_concept_code=NativeConcept.TEXT.code,
+ )
+ assert "must have exactly one of user_text" in str(exc_info.value)
+
+ # Test multiple user text sources
+ with pytest.raises(Exception) as exc_info:
+ PipeLLMPrompt(
+ code="test_validation_error",
+ domain="generic",
+ system_prompt=PipeTestCases.SYSTEM_PROMPT,
+ user_text=PipeTestCases.USER_PROMPT,
+ user_prompt_verbatim_name="some_template",
+ output_concept_code=NativeConcept.TEXT.code,
+ )
+ assert "must have exactly one of user_text" in str(exc_info.value)
+
+ # Test multiple system prompts
+ with pytest.raises(Exception) as exc_info:
+ PipeLLMPrompt(
+ code="test_validation_error",
+ domain="generic",
+ system_prompt=PipeTestCases.SYSTEM_PROMPT,
+ system_prompt_verbatim_name="some_template",
+ user_text=PipeTestCases.USER_PROMPT,
+ output_concept_code=NativeConcept.TEXT.code,
+ )
+ assert "got more than one of system_prompt" in str(exc_info.value)
diff --git a/tests/test_pipelines/pipe_llm_prompt.py b/tests/test_pipelines/pipe_llm_prompt.py
new file mode 100644
index 000000000..d920a22a6
--- /dev/null
+++ b/tests/test_pipelines/pipe_llm_prompt.py
@@ -0,0 +1,64 @@
+from typing import List, Literal
+
+from pydantic import Field
+
+from pipelex.core.stuff_content import StructuredContent
+from pipelex.types import StrEnum
+
+
+class MusicGenre(StrEnum):
+ """Available music genres."""
+
+ CLASSICAL = "classical"
+ JAZZ = "jazz"
+ ROCK = "rock"
+ ELECTRONIC = "electronic"
+ WORLD = "world"
+
+
+class MusicCategoryContent(StructuredContent):
+ """A content class with a Literal field for music genres."""
+
+ category: Literal[
+ MusicGenre.CLASSICAL,
+ MusicGenre.JAZZ,
+ MusicGenre.ROCK,
+ MusicGenre.ELECTRONIC,
+ MusicGenre.WORLD,
+ ] = Field(description="The genre of music")
+
+
+class SimpleTextContent(StructuredContent):
+ """A simple text content class for testing."""
+
+ text: str = Field(description="The text content")
+
+
+class SimpleStructuredContent(StructuredContent):
+ """A structured content class with primitive types."""
+
+ name: str = Field(description="The name of the content")
+ value: int = Field(description="The numeric value")
+ is_active: bool = Field(description="Whether the content is active")
+
+
+class DocumentTypeContent(StructuredContent):
+ """A content class with enum fields."""
+
+ title: str = Field(description="The document title")
+ status: str = Field(description="The document status")
+
+
+class PersonContent(StructuredContent):
+ """A complex nested content with various types."""
+
+ name: str = Field(description="The person's name")
+ age: int = Field(description="The person's age")
+ address: str = Field(description="The person's address")
+
+
+class ComplexListContent(StructuredContent):
+ """A content class with list fields."""
+
+ items: List[str] = Field(description="List of items")
+ tags: List[str] = Field(description="List of tags")
diff --git a/tests/tools/typing/test_type_inspector.py b/tests/tools/typing/test_type_inspector.py
index 8bafed80f..399c195d9 100644
--- a/tests/tools/typing/test_type_inspector.py
+++ b/tests/tools/typing/test_type_inspector.py
@@ -1,27 +1,324 @@
-from pydantic import BaseModel
+from datetime import datetime
+from typing import List, Literal, Optional
-from pipelex import pretty_print
+from pydantic import BaseModel, Field, field_validator
+from pytest import FixtureRequest
+
+from pipelex.core.stuff_content import ListContent, StructuredContent, TextContent
from pipelex.tools.typing.type_inspector import get_type_structure
+from pipelex.types import StrEnum
+
+
+# Test Enums
+class DocumentType(StrEnum):
+ INVOICE = "INVOICE"
+ RECEIPT = "RECEIPT"
+
+
+class Priority(StrEnum):
+ HIGH = "HIGH"
+ LOW = "LOW"
+
+
+class MusicGenre(StrEnum):
+ """Available music genres."""
+
+ CLASSICAL = "classical"
+ JAZZ = "jazz"
+ ROCK = "rock"
+ ELECTRONIC = "electronic"
+ WORLD = "world"
+
+
+# Simple Content Classes
+class SimpleTextContent(TextContent):
+ """A simple text content class"""
+
+ pass
+
+
+class MusicCategoryContent(StructuredContent):
+ """A content class with a Literal field for music genres."""
+
+ category: Literal[
+ MusicGenre.CLASSICAL,
+ MusicGenre.JAZZ,
+ MusicGenre.ROCK,
+ MusicGenre.ELECTRONIC,
+ MusicGenre.WORLD,
+ ] = Field(description="The genre of music")
+
+
+class SimpleStructuredContent(StructuredContent):
+ """A simple structured content with primitive types"""
+
+ name: str
+ age: int
+ active: bool
+
+
+# Enum Content Classes
+class DocumentTypeContent(StructuredContent):
+ """Content with enum type"""
+
+ document_type: DocumentType
+
+
+# Nested Content Classes
+class AddressContent(StructuredContent):
+ """Nested address content"""
+
+ street: str
+ city: str
+ country: str
+
+
+class PersonContent(StructuredContent):
+ """Complex nested content with various types"""
+
+ name: str
+ age: int
+ address: AddressContent = Field(description="Address of the person")
+ documents: List[DocumentTypeContent]
+ priority: Optional[Priority] = None
+
+
+class ComplexListContent(ListContent[PersonContent]):
+ """List content with complex items"""
+
+ items: List[PersonContent]
+
+
+class GanttTaskDetails(StructuredContent):
+ """Do not include timezone in the dates."""
+
+ name: str
+ start_date: Optional[datetime] = None
+ end_date: Optional[datetime] = None
+
+ @field_validator("start_date", "end_date")
+ @classmethod
+ def remove_tzinfo(cls, v: Optional[datetime]) -> Optional[datetime]:
+ if v is not None:
+ return v.replace(tzinfo=None)
+ return v
+
+
+class Milestone(StructuredContent):
+ name: str
+ date: Optional[datetime]
+
+ @field_validator("date")
+ @classmethod
+ def remove_tzinfo(cls, v: Optional[datetime]) -> Optional[datetime]:
+ if v is not None:
+ return v.replace(tzinfo=None)
+ return v
+
+
+class GanttChart(StructuredContent):
+ tasks: Optional[List[GanttTaskDetails]] = None
+ milestones: Optional[List[Milestone]] = None
+
+
+class TestTypeInspector:
+ """Tests for the type inspector functionality"""
+
+ def test_simple_text_content(self, request: FixtureRequest):
+ """Test structure of simple text content"""
+ result = get_type_structure(SimpleTextContent)
+ expected = [
+ "class SimpleTextContent(TextContent):",
+ ' """A simple text content class"""',
+ " # Inherits from TextContent",
+ " # No additional fields",
+ ]
+ assert result == expected, f"Expected:\n{''.join(expected)}\n\nGot:\n{''.join(result)}"
+
+ def test_simple_structured_content(self, request: FixtureRequest):
+ """Test structure of simple structured content"""
+ result = get_type_structure(SimpleStructuredContent)
+ expected = [
+ "class SimpleStructuredContent(StructuredContent):",
+ ' """A simple structured content with primitive types"""',
+ " name: str",
+ " age: int",
+ " active: bool",
+ ]
+ assert result == expected, f"Expected:\n{''.join(expected)}\n\nGot:\n{''.join(result)}"
+
+ def test_enum_content(self, request: FixtureRequest):
+ """Test structure of content with enum"""
+ result = get_type_structure(DocumentTypeContent)
+ expected = [
+ "class DocumentTypeContent(StructuredContent):",
+ ' """Content with enum type"""',
+ " document_type: DocumentType",
+ "",
+ "class DocumentType(StrEnum):",
+ ' INVOICE = "INVOICE"',
+ ' RECEIPT = "RECEIPT"',
+ ]
+ assert result == expected, f"Expected:\n{''.join(expected)}\n\nGot:\n{''.join(result)}"
+
+ def test_nested_content(self, request: FixtureRequest):
+ """Test structure of nested content"""
+ result = get_type_structure(PersonContent)
+ expected = [
+ "class PersonContent(StructuredContent):",
+ ' """Complex nested content with various types"""',
+ " name: str",
+ " age: int",
+ " address: AddressContent # Address of the person",
+ " documents: List[DocumentTypeContent]",
+ " priority: Optional[Priority] = None",
+ "",
+ "class AddressContent(StructuredContent):",
+ ' """Nested address content"""',
+ " street: str",
+ " city: str",
+ " country: str",
+ "",
+ "class DocumentTypeContent(StructuredContent):",
+ ' """Content with enum type"""',
+ " document_type: DocumentType",
+ "",
+ "class DocumentType(StrEnum):",
+ ' INVOICE = "INVOICE"',
+ ' RECEIPT = "RECEIPT"',
+ "",
+ "class Priority(StrEnum):",
+ ' HIGH = "HIGH"',
+ ' LOW = "LOW"',
+ ]
+ assert result == expected, f"Expected:\n{''.join(expected)}\n\nGot:\n{''.join(result)}"
+
+ def test_list_content(self, request: FixtureRequest):
+ """Test structure of list content"""
+ result = get_type_structure(ComplexListContent)
+ expected = [
+ "class ComplexListContent(ListContent[PersonContent]):",
+ ' """List content with complex items"""',
+ " items: List[PersonContent]",
+ "",
+ "class PersonContent(StructuredContent):",
+ ' """Complex nested content with various types"""',
+ " name: str",
+ " age: int",
+ " address: AddressContent # Address of the person",
+ " documents: List[DocumentTypeContent]",
+ " priority: Optional[Priority] = None",
+ "",
+ "class AddressContent(StructuredContent):",
+ ' """Nested address content"""',
+ " street: str",
+ " city: str",
+ " country: str",
+ "",
+ "class DocumentTypeContent(StructuredContent):",
+ ' """Content with enum type"""',
+ " document_type: DocumentType",
+ "",
+ "class DocumentType(StrEnum):",
+ ' INVOICE = "INVOICE"',
+ ' RECEIPT = "RECEIPT"',
+ "",
+ "class Priority(StrEnum):",
+ ' HIGH = "HIGH"',
+ ' LOW = "LOW"',
+ ]
+ assert result == expected, f"Expected:\n{''.join(expected)}\n\nGot:\n{''.join(result)}"
+
+ def test_model_with_field_description(self, request: FixtureRequest):
+ """Test structure of a model with field descriptions"""
+
+ class Person(BaseModel):
+ name: str
+ age: int
+
+ class Employee(Person):
+ job: str = Field(description="Job title, must be lowercase")
+
+ result = get_type_structure(Employee)
+ expected = [
+ "class Employee(Person):",
+ " job: str # Job title, must be lowercase",
+ "",
+ "class Person(BaseModel):",
+ " name: str",
+ " age: int",
+ ]
+ assert result == expected, f"Expected:\n{''.join(expected)}\n\nGot:\n{''.join(result)}"
+
+ def test_model_with_docstring_and_field_description(self, request: FixtureRequest):
+ """Test structure of a model with both docstring and field descriptions"""
+ class TaskContent(StructuredContent):
+ """A task content model that represents a single task.
-class Simple(BaseModel):
- """A simple class that references itself."""
+ This model is used to store task information including its title,
+ description, and status.
+ """
- field_1: int
- field_2: "Simple"
+ title: str = Field(description="The title of the task")
+ description: str = Field(description="Detailed description of what needs to be done")
+ is_completed: bool = Field(False, description="Whether the task is completed")
+ result = get_type_structure(TaskContent)
+ expected = [
+ "class TaskContent(StructuredContent):",
+ ' """A task content model that represents a single task.',
+ "",
+ " This model is used to store task information including its title,",
+ " description, and status.",
+ ' """',
+ " title: str # The title of the task",
+ " description: str # Detailed description of what needs to be done",
+ " is_completed: bool = False # Whether the task is completed",
+ ]
+ assert result == expected, f"Expected:\n{''.join(expected)}\n\nGot:\n{''.join(result)}"
-def test_self_referencing_class_no_loop():
- """Test that get_type_structure handles self-referencing classes without infinite loops."""
- # Prepare test data
- TestClass = Simple
- TestClass.model_rebuild()
+ def test_literal_field_content(self, request: FixtureRequest):
+ """Test structure of content with Literal field"""
+ result = get_type_structure(MusicCategoryContent)
+ expected = [
+ "class MusicCategoryContent(StructuredContent):",
+ ' """A content class with a Literal field for music genres."""',
+ " category: Literal[",
+ ' "classical",',
+ ' "jazz",',
+ ' "rock",',
+ ' "electronic",',
+ ' "world",',
+ " ] # The genre of music",
+ "",
+ "class MusicGenre(StrEnum):",
+ ' """Available music genres."""',
+ ' CLASSICAL = "classical"',
+ ' JAZZ = "jazz"',
+ ' ROCK = "rock"',
+ ' ELECTRONIC = "electronic"',
+ ' WORLD = "world"',
+ ]
+ assert result == expected, f"Expected:\n{''.join(expected)}\n\nGot:\n{''.join(result)}"
- # Call the function
- structure = get_type_structure(TestClass, base_class=BaseModel)
- pretty_print(structure, title="Structure")
+ def test_gantt_chart_content(self, request: FixtureRequest):
+ """Test structure of Gantt chart content with datetime validators"""
- # Assert the result
- assert len(structure) > 0, "Structure should not be empty"
- assert structure.count("Class 'Simple':") == 1, "Class should only appear once"
- assert any("field_2" in line and "Simple" in line for line in structure), "Structure should show next field with self-reference"
+ result = get_type_structure(GanttChart, base_class=StructuredContent)
+ expected = [
+ "class GanttChart(StructuredContent):",
+ " tasks: Optional[List[GanttTaskDetails]] = None",
+ " milestones: Optional[List[Milestone]] = None",
+ "",
+ "class GanttTaskDetails(StructuredContent):",
+ ' """Do not include timezone in the dates."""',
+ " name: str",
+ " start_date: Optional[datetime] = None",
+ " end_date: Optional[datetime] = None",
+ "",
+ "class Milestone(StructuredContent):",
+ " name: str",
+ " date: Optional[datetime] = None",
+ ]
+ assert result == expected, f"Expected:\n{''.join(expected)}\n\nGot:\n{''.join(result)}"