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.

Refactor/Adapt ClassRegistry from kajson, remove sandbox manager (#103)

Louis Choquel08bbe33dae59
diff --git a/docs/pages/configuration/config-practical/logging-config.md b/docs/pages/configuration/config-practical/logging-config.md
index 971a983f0..c8b23553d 100644
--- a/docs/pages/configuration/config-practical/logging-config.md
+++ b/docs/pages/configuration/config-practical/logging-config.md
@@ -79,7 +79,7 @@ silenced_problem_ids = ["azure_openai_no_stream_options"]
### Poor Loggers
```toml
-generic_poor_logger = "#sandbox"
+generic_poor_logger = "#poor-log"
poor_loggers = [
"kajson.decoder.sandbox",
"kajson.encoder.sandbox",
@@ -150,7 +150,7 @@ is_caller_info_enabled = true
caller_info_template = "file_line_func"
silenced_problem_ids = []
-generic_poor_logger = "#sandbox"
+generic_poor_logger = "#poor-log"
poor_loggers = []
[pipelex.log_config.package_log_levels]
diff --git a/pipelex/cogt/content_generation/assignment_models.py b/pipelex/cogt/content_generation/assignment_models.py
index c69a61579..082b8efe3 100644
--- a/pipelex/cogt/content_generation/assignment_models.py
+++ b/pipelex/cogt/content_generation/assignment_models.py
@@ -1,6 +1,5 @@
from typing import Any, Dict, Optional, Type
-from kajson.class_registry import class_registry
from pydantic import BaseModel
from typing_extensions import override
@@ -16,6 +15,7 @@ from pipelex.cogt.llm.llm_prompt_factory_abstract import LLMPromptFactoryAbstrac
from pipelex.cogt.ocr.ocr_handle import OcrHandle
from pipelex.cogt.ocr.ocr_input import OcrInput
from pipelex.cogt.ocr.ocr_job_components import OcrJobConfig, OcrJobParams
+from pipelex.hub import get_class_registry
from pipelex.pipeline.job_metadata import JobMetadata
from pipelex.tools.templating.jinja2_template_category import Jinja2TemplateCategory
from pipelex.tools.templating.templating_models import PromptingStyle
@@ -80,7 +80,7 @@ class ObjectAssignment(BaseModel):
def __init__(self, **kwargs: Any):
super().__init__(**kwargs)
- if not class_registry.has_class(name=self.object_class_name):
+ if not get_class_registry().has_class(name=self.object_class_name):
error_msg = f"Could not create ObjectAssignment for class '{self.object_class_name}' because it is not in the class registry."
raise LLMAssignmentError(error_msg)
@@ -90,7 +90,7 @@ class ObjectAssignment(BaseModel):
llm_assignment: LLMAssignment,
) -> "ObjectAssignment":
object_class_name = object_class.__name__
- class_registry.register_class(
+ get_class_registry().register_class(
class_type=object_class,
name=object_class_name,
should_warn_if_already_registered=False,
diff --git a/pipelex/cogt/content_generation/llm_generate.py b/pipelex/cogt/content_generation/llm_generate.py
index 918da4f8c..95a169781 100644
--- a/pipelex/cogt/content_generation/llm_generate.py
+++ b/pipelex/cogt/content_generation/llm_generate.py
@@ -1,12 +1,11 @@
from typing import List
-from kajson.class_registry import class_registry
from pydantic import BaseModel
from pipelex import log
from pipelex.cogt.content_generation.assignment_models import LLMAssignment, ObjectAssignment
from pipelex.cogt.llm.llm_job_factory import LLMJobFactory
-from pipelex.hub import get_llm_worker
+from pipelex.hub import get_class_registry, get_llm_worker
async def llm_gen_text(llm_assignment: LLMAssignment) -> str:
@@ -31,7 +30,7 @@ async def llm_gen_object(object_assignment: ObjectAssignment) -> BaseModel:
llm_job_params=llm_assignment.llm_job_params,
)
content_class_name = object_assignment.object_class_name
- content_class = class_registry.get_required_base_model(name=content_class_name)
+ content_class = get_class_registry().get_required_base_model(name=content_class_name)
generated_object: BaseModel = await llm_worker.gen_object(
llm_job=llm_job,
schema=content_class,
@@ -49,7 +48,7 @@ async def llm_gen_object_list(object_assignment: ObjectAssignment) -> List[BaseM
llm_job_params=llm_assignment.llm_job_params,
)
item_class_name = object_assignment.object_class_name
- item_class = class_registry.get_required_class(name=item_class_name)
+ item_class = get_class_registry().get_required_class(name=item_class_name)
class ListSchema(BaseModel):
items: List[item_class] # type: ignore
diff --git a/pipelex/core/concept.py b/pipelex/core/concept.py
index cf74c6fd3..a213a0e0c 100644
--- a/pipelex/core/concept.py
+++ b/pipelex/core/concept.py
@@ -1,7 +1,7 @@
import re
from typing import List, Tuple
-from kajson.class_registry import class_registry
+from kajson.kajson_manager import KajsonManager
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
from typing_extensions import Self
@@ -91,10 +91,12 @@ class Concept(BaseModel):
@classmethod
def is_valid_structure_class(cls, structure_class_name: str) -> bool:
- if class_registry.has_subclass(name=structure_class_name, base_class=StuffContent):
+ # we get_class_registry directly from KajsonManager instead of pipelex hub to avoid circular import
+ if KajsonManager.get_class_registry().has_subclass(name=structure_class_name, base_class=StuffContent):
return True
else:
- if class_registry.has_class(name=structure_class_name):
+ # we get_class_registry directly from KajsonManager instead of pipelex hub to avoid circular import
+ if KajsonManager.get_class_registry().has_class(name=structure_class_name):
log.warning(f"Concept class '{structure_class_name}' is registered but it's not a subclass of StuffContent")
return False
diff --git a/pipelex/core/concept_factory.py b/pipelex/core/concept_factory.py
index de37aebc9..e078cf885 100644
--- a/pipelex/core/concept_factory.py
+++ b/pipelex/core/concept_factory.py
@@ -1,7 +1,6 @@
from inspect import getsource
from typing import Any, Dict, List, Optional, Type, Union
-from kajson.class_registry import class_registry
from pydantic import BaseModel, ConfigDict, Field, ValidationError
from pipelex.core.concept import Concept
@@ -10,6 +9,7 @@ from pipelex.core.concept_native import NativeConcept
from pipelex.core.domain import SpecialDomain
from pipelex.core.stuff_content import TextContent
from pipelex.exceptions import ConceptFactoryError, StructureClassError
+from pipelex.hub import get_class_registry
class ConceptBlueprint(BaseModel):
@@ -180,8 +180,8 @@ class ConceptFactory:
@classmethod
def get_concept_class_source_code(cls, concept_name: str, base_class: Type[Any]) -> str:
- if not class_registry.has_class(concept_name):
+ if not get_class_registry().has_class(concept_name):
raise RuntimeError(f"Class '{concept_name}' not found in registry")
- cls = class_registry.get_required_subclass(name=concept_name, base_class=base_class)
+ cls = get_class_registry().get_required_subclass(name=concept_name, base_class=base_class)
return getsource(cls)
diff --git a/pipelex/core/stuff_factory.py b/pipelex/core/stuff_factory.py
index 1ce5d6866..b01c9dbd5 100644
--- a/pipelex/core/stuff_factory.py
+++ b/pipelex/core/stuff_factory.py
@@ -1,7 +1,6 @@
from typing import Any, Dict, List, Optional, Tuple
import shortuuid
-from kajson.class_registry import class_registry
from pydantic import BaseModel, Field
from pipelex.config import get_config
@@ -11,7 +10,7 @@ from pipelex.core.concept_native import NativeConcept
from pipelex.core.stuff import Stuff, StuffCreationRecord
from pipelex.core.stuff_content import StuffContent, StuffContentInitableFromStr
from pipelex.exceptions import ConceptError, PipelexError
-from pipelex.hub import get_required_concept
+from pipelex.hub import get_class_registry, get_required_concept
class StuffFactoryError(PipelexError):
@@ -111,7 +110,7 @@ class StuffFactory:
raise StuffFactoryError(f"Concept '{concept_str}' does not contain a domain, could not make stuff '{stuff_ref}'")
the_concept = get_required_concept(concept_code=concept_code)
the_subclass_name = the_concept.structure_class_name
- the_subclass = class_registry.get_class(name=the_subclass_name) or eval(the_subclass_name)
+ the_subclass = get_class_registry().get_class(name=the_subclass_name) or eval(the_subclass_name)
if not issubclass(the_subclass, StuffContentInitableFromStr):
raise StuffFactoryError(f"Concept '{concept_code}', subclass '{the_subclass}' is not InitableFromStr")
stuff_content: StuffContent = the_subclass.make_from_str(str_value)
@@ -155,7 +154,7 @@ class StuffFactory:
"""
the_concept = get_required_concept(concept_code=concept_code)
the_subclass_name = the_concept.structure_class_name
- the_subclass = class_registry.get_required_subclass(name=the_subclass_name, base_class=StuffContent)
+ the_subclass = get_class_registry().get_required_subclass(name=the_subclass_name, base_class=StuffContent)
the_stuff_content = the_subclass.model_validate(obj=stuff_contents)
return cls.make_stuff(
concept_str=concept_code,
diff --git a/pipelex/hub.py b/pipelex/hub.py
index 124d470e4..8a704b6ca 100644
--- a/pipelex/hub.py
+++ b/pipelex/hub.py
@@ -1,6 +1,8 @@
from collections import defaultdict
from typing import ClassVar, Dict, List, Optional, Type
+from kajson.class_registry_abstract import ClassRegistryAbstract
+
from pipelex import log
from pipelex.cogt.content_generation.content_generator_protocol import ContentGeneratorProtocol
from pipelex.cogt.imgg.imgg_worker_abstract import ImggWorkerAbstract
@@ -43,6 +45,7 @@ class PipelexHub:
self._config: Optional[ConfigRoot] = None
self._secrets_provider: Optional[SecretsProviderAbstract] = None
self._template_provider: Optional[TemplateProviderAbstract] = None
+ self._class_registry: Optional[ClassRegistryAbstract] = None
# cogt
self._llm_models_provider: Optional[LLMModelProviderAbstract] = None
self._llm_deck_provider: Optional[LLMDeckAbstract] = None
@@ -112,6 +115,9 @@ class PipelexHub:
def set_template_provider(self, template_provider: TemplateProviderAbstract):
self._template_provider = template_provider
+ def set_class_registry(self, class_registry: ClassRegistryAbstract):
+ self._class_registry = class_registry
+
# cogt
def set_llm_models_provider(self, llm_models_provider: LLMModelProviderAbstract):
@@ -187,6 +193,11 @@ class PipelexHub:
raise RuntimeError("Template provider is not set. You must initialize Pipelex first.")
return self._template_provider
+ def get_required_class_registry(self) -> ClassRegistryAbstract:
+ if self._class_registry is None:
+ raise RuntimeError("ClassRegistry is not initialized")
+ return self._class_registry
+
# cogt
def get_required_llm_models_provider(self) -> LLMModelProviderAbstract:
@@ -294,6 +305,10 @@ def get_template(template_name: str) -> str:
return get_template_provider().get_template(template_name=template_name)
+def get_class_registry() -> ClassRegistryAbstract:
+ return get_pipelex_hub().get_required_class_registry()
+
+
# cogt
diff --git a/pipelex/libraries/library_manager.py b/pipelex/libraries/library_manager.py
index 4226d38e7..f5be69c69 100644
--- a/pipelex/libraries/library_manager.py
+++ b/pipelex/libraries/library_manager.py
@@ -2,8 +2,8 @@ import os
from pathlib import Path
from typing import Any, ClassVar, Dict, List, Optional, Type
-from kajson.class_registry import class_registry
from kajson.exceptions import ClassRegistryInheritanceError, ClassRegistryNotFoundError
+from kajson.kajson_manager import KajsonManager
from pydantic import ValidationError
from pipelex import log
@@ -74,13 +74,13 @@ class LibraryManager:
def load_libraries(self):
log.debug("LibraryManager loading separate libraries")
- class_registry.register_classes_in_folder(
+ KajsonManager.get_class_registry().register_classes_in_folder(
folder_path=LibraryConfig.loaded_pipelines_path,
)
library_paths = [LibraryConfig.loaded_pipelines_path]
if runtime_manager.is_unit_testing:
log.debug("Registering test pipeline structures for unit testing")
- class_registry.register_classes_in_folder(
+ KajsonManager.get_class_registry().register_classes_in_folder(
folder_path=LibraryConfig.test_pipelines_path,
)
library_paths += [LibraryConfig.test_pipelines_path]
@@ -283,7 +283,7 @@ class LibraryManager:
# the factory class name for that specific type of Pipe is the pipe class name with "Factory" suffix
factory_class_name = f"{pipe_class_name}Factory"
try:
- pipe_factory: Type[PipeSpecificFactoryProtocol[Any, Any]] = class_registry.get_required_subclass(
+ pipe_factory: Type[PipeSpecificFactoryProtocol[Any, Any]] = KajsonManager.get_class_registry().get_required_subclass(
name=factory_class_name,
base_class=PipeSpecificFactoryProtocol,
)
diff --git a/pipelex/pipe_operators/pipe_llm.py b/pipelex/pipe_operators/pipe_llm.py
index 1fb1d74b0..15d58ce41 100644
--- a/pipelex/pipe_operators/pipe_llm.py
+++ b/pipelex/pipe_operators/pipe_llm.py
@@ -1,6 +1,5 @@
from typing import List, Optional, Set, Type, cast
-from kajson.class_registry import class_registry
from pydantic import model_validator
from typing_extensions import Self, override
@@ -34,6 +33,7 @@ from pipelex.exceptions import (
StaticValidationErrorType,
)
from pipelex.hub import (
+ get_class_registry,
get_concept_provider,
get_content_generator,
get_llm_deck,
@@ -118,7 +118,7 @@ class PipeLLM(PipeOperator):
# let's check at least that the input is a structured concept
input_concept = concept_provider.get_required_concept(concept_code=concept_code_of_declared_input)
input_concept_class_name = input_concept.structure_class_name
- input_concept_class = class_registry.get_required_subclass(name=input_concept_class_name, base_class=StuffContent)
+ input_concept_class = get_class_registry().get_required_subclass(name=input_concept_class_name, base_class=StuffContent)
if issubclass(input_concept_class, StructuredContent):
continue
explanation = "The input provided for LLM Vision must be an image or a concept that refines image"
@@ -393,7 +393,7 @@ class PipeLLM(PipeOperator):
llm_prompt_2_factory: Optional[LLMPromptFactoryAbstract],
content_generator: ContentGeneratorProtocol,
) -> StuffContent:
- content_class: Type[StuffContent] = class_registry.get_required_subclass(name=output_class_name, base_class=StuffContent)
+ content_class: Type[StuffContent] = get_class_registry().get_required_subclass(name=output_class_name, base_class=StuffContent)
task_desc: str
the_content: StuffContent
diff --git a/pipelex/pipe_operators/pipe_llm_prompt.py b/pipelex/pipe_operators/pipe_llm_prompt.py
index c3066a416..d538b9a42 100644
--- a/pipelex/pipe_operators/pipe_llm_prompt.py
+++ b/pipelex/pipe_operators/pipe_llm_prompt.py
@@ -1,6 +1,5 @@
from typing import ClassVar, List, Optional, Set, cast
-from kajson.class_registry import class_registry
from pydantic import model_validator
from typing_extensions import Self, override
@@ -22,7 +21,7 @@ from pipelex.exceptions import (
PipeRunParamsError,
WorkingMemoryVariableError,
)
-from pipelex.hub import get_template
+from pipelex.hub import get_class_registry, get_template
from pipelex.pipe_operators.pipe_jinja2 import PipeJinja2, PipeJinja2Output
from pipelex.pipe_operators.pipe_operator import PipeOperator
from pipelex.pipeline.job_metadata import JobCategory, JobMetadata
@@ -219,7 +218,7 @@ class PipeLLMPrompt(PipeOperator):
@staticmethod
def get_output_structure_prompt(output_concept: str) -> str:
class_name = Concept.extract_concept_name_from_str(concept_str=output_concept)
- output_class = class_registry.get_class(class_name)
+ output_class = get_class_registry().get_class(class_name)
if not output_class:
return ""
diff --git a/pipelex/pipelex.py b/pipelex/pipelex.py
index a0c75361a..ea1e86e08 100644
--- a/pipelex/pipelex.py
+++ b/pipelex/pipelex.py
@@ -2,7 +2,9 @@ from importlib.metadata import metadata
from typing import Any, ClassVar, List, Optional, Type
from dotenv import load_dotenv
-from kajson.class_registry import class_registry
+from kajson.class_registry import ClassRegistry
+from kajson.class_registry_abstract import ClassRegistryAbstract
+from kajson.kajson_manager import KajsonManager
from pydantic import ValidationError
from rich import print
from typing_extensions import Self
@@ -50,6 +52,7 @@ class Pipelex:
pipelex_hub: Optional[PipelexHub] = None,
config_cls: Optional[Type[ConfigRoot]] = None,
ready_made_config: Optional[ConfigRoot] = None,
+ class_registry: Optional[ClassRegistryAbstract] = None,
template_provider: Optional[TemplateLibrary] = None,
llm_model_provider: Optional[LLMModelLibrary] = None,
plugin_manager: Optional[PluginManager] = None,
@@ -77,6 +80,7 @@ class Pipelex:
pipelex_hub: Optional[PipelexHub] = None,
config_cls: Optional[Type[ConfigRoot]] = None,
ready_made_config: Optional[ConfigRoot] = None,
+ class_registry: Optional[ClassRegistryAbstract] = None,
template_provider: Optional[TemplateLibrary] = None,
llm_model_provider: Optional[LLMModelLibrary] = None,
plugin_manager: Optional[PluginManager] = None,
@@ -113,6 +117,9 @@ class Pipelex:
# tools
self.template_provider = template_provider or TemplateLibrary()
self.pipelex_hub.set_template_provider(self.template_provider)
+ self.class_registry = class_registry or ClassRegistry()
+ self.pipelex_hub.set_class_registry(self.class_registry)
+ self.kajson_manager = KajsonManager(class_registry=self.class_registry)
# cogt
self.llm_model_provider = llm_model_provider or LLMModelLibrary()
@@ -172,14 +179,14 @@ class Pipelex:
# cogt
self.pipelex_hub.set_content_generator(content_generator or ContentGenerator())
self.reporting_delegate.setup()
- class_registry.register_classes(PipelexRegistryModels.get_all_models())
+ self.class_registry.register_classes(PipelexRegistryModels.get_all_models())
if runtime_manager.is_unit_testing:
log.debug("Registering test models for unit testing")
- class_registry.register_classes(PipelexTestModels.get_all_models())
+ self.class_registry.register_classes(PipelexTestModels.get_all_models())
self.activity_manager.setup()
if structure_classes:
- class_registry.register_classes(structure_classes)
+ self.class_registry.register_classes(structure_classes)
self.pipelex_hub.set_pipe_router(pipe_router or PipeRouter())
@@ -226,7 +233,8 @@ class Pipelex:
self.llm_model_provider.teardown()
# tools
- class_registry.reset()
+ self.kajson_manager.teardown()
+ self.class_registry.teardown()
func_registry.teardown()
Pipelex._pipelex_instance = None
diff --git a/pipelex/pipelex.toml b/pipelex/pipelex.toml
index 345f6f142..978485b4f 100644
--- a/pipelex/pipelex.toml
+++ b/pipelex/pipelex.toml
@@ -32,12 +32,10 @@ silenced_problem_ids = ["azure_openai_no_stream_options"]
caller_info_template = "file_line"
is_caller_info_enabled = false
-poor_loggers = [
- "kajson.decoder.sandbox",
- "kajson.encoder.sandbox",
- "class_registry.sandbox",
-]
-generic_poor_logger = "#sandbox"
+log_mode = "rich"
+
+poor_loggers = []
+generic_poor_logger = "#poor-log"
[pipelex.log_config.package_log_levels]
# for logger names, the dots "." have been replaced by "-" to avoid toml parsing issues
diff --git a/pipelex/tools/exceptions.py b/pipelex/tools/exceptions.py
index 85e4e958d..054477cfc 100644
--- a/pipelex/tools/exceptions.py
+++ b/pipelex/tools/exceptions.py
@@ -1,6 +1,7 @@
import logging
+from typing import ClassVar
-from kajson.sandbox_manager import sandbox_manager
+from pipelex.types import StrEnum
class RootException(Exception):
@@ -13,17 +14,25 @@ class ToolException(RootException):
pass
+class TracebackMessageErrorMode(StrEnum):
+ ERROR = "error"
+ EXCEPTION = "exception"
+
+
class TracebackMessageError(RootException):
+ error_mode: ClassVar[TracebackMessageErrorMode] = TracebackMessageErrorMode.EXCEPTION
+
def __init__(self, message: str):
super().__init__(message)
logger_name = __name__
- if sandbox_manager.is_in_sandbox():
- generic_poor_logger = "#sandbox"
- logger = logging.getLogger(generic_poor_logger)
- logger.error(message)
- else:
- self.logger = logging.getLogger(logger_name)
- self.logger.exception(message)
+ match self.__class__.error_mode:
+ case TracebackMessageErrorMode.ERROR:
+ generic_poor_logger = "#poor-log"
+ logger = logging.getLogger(generic_poor_logger)
+ logger.error(message)
+ case TracebackMessageErrorMode.EXCEPTION:
+ self.logger = logging.getLogger(logger_name)
+ self.logger.exception(message)
class FatalError(TracebackMessageError):
diff --git a/pipelex/tools/func_registry.py b/pipelex/tools/func_registry.py
index 142446cb5..645c31c0c 100644
--- a/pipelex/tools/func_registry.py
+++ b/pipelex/tools/func_registry.py
@@ -1,14 +1,12 @@
import logging
from typing import Any, Callable, Dict, List, Optional, TypeVar
-from kajson.sandbox_manager import sandbox_manager
from pydantic import Field, PrivateAttr, RootModel
from pipelex.tools.exceptions import ToolException
from pipelex.tools.log.log_levels import LOGGING_LEVEL_VERBOSE
FUNC_REGISTRY_LOGGER_CHANNEL_NAME = "func_registry"
-FUNC_REGISTRY_LOGGER_CHANNEL_NAME_IN_SANDBOX = "func_registry.sandbox"
# Type variable for generic function types
T = TypeVar("T")
@@ -24,11 +22,10 @@ class FuncRegistry(RootModel[FuncRegistryDict]):
_logger: logging.Logger = PrivateAttr(logging.getLogger(FUNC_REGISTRY_LOGGER_CHANNEL_NAME))
def log(self, message: str) -> None:
- if sandbox_manager.is_in_sandbox():
- logger = logging.getLogger(FUNC_REGISTRY_LOGGER_CHANNEL_NAME_IN_SANDBOX)
- logger.debug(message)
- else:
- self._logger.debug(message)
+ self._logger.debug(message)
+
+ def set_logger(self, logger: logging.Logger) -> None:
+ self._logger = logger
def teardown(self) -> None:
"""Resets the registry to an empty state."""
diff --git a/pipelex/tools/log/log.py b/pipelex/tools/log/log.py
index c3bc51ff2..05e11673f 100644
--- a/pipelex/tools/log/log.py
+++ b/pipelex/tools/log/log.py
@@ -1,7 +1,7 @@
import logging
from typing import Any, Dict, Optional, Union
-from pipelex.tools.log.log_config import LogConfig
+from pipelex.tools.log.log_config import LogConfig, LogMode
from pipelex.tools.log.log_dispatch import LogDispatch
from pipelex.tools.log.log_formatter import EmojiLogFormatter, LevelAndEmojiLogFormatter
from pipelex.tools.log.log_levels import LOGGING_LEVEL_DEV, LOGGING_LEVEL_OFF, LOGGING_LEVEL_VERBOSE, LogLevel
@@ -26,6 +26,9 @@ class Log:
self.poor_handler: Optional[logging.Handler] = None
self.log_dispatch: LogDispatch = LogDispatch()
+ def set_log_mode(self, mode: LogMode):
+ self.log_dispatch.set_log_mode(mode=mode)
+
@property
def _log_config(self) -> LogConfig:
"""
diff --git a/pipelex/tools/log/log_config.py b/pipelex/tools/log/log_config.py
index a795e0a8e..2d180bc73 100644
--- a/pipelex/tools/log/log_config.py
+++ b/pipelex/tools/log/log_config.py
@@ -10,6 +10,11 @@ from pipelex.tools.log.log_levels import LogLevel
from pipelex.types import StrEnum
+class LogMode(StrEnum):
+ RICH = "rich"
+ POOR = "poor"
+
+
class HighlighterName(StrEnum):
JSON = "json"
REPR = "repr"
@@ -79,6 +84,7 @@ class RichLogConfig(ConfigModel):
class LogConfig(ConfigModel):
default_log_level: LogLevel = Field(strict=False)
package_log_levels: Dict[str, LogLevel]
+ log_mode: LogMode = Field(strict=False)
is_console_logging_enabled: bool
diff --git a/pipelex/tools/log/log_dispatch.py b/pipelex/tools/log/log_dispatch.py
index f5031925e..c9a7f2e5b 100644
--- a/pipelex/tools/log/log_dispatch.py
+++ b/pipelex/tools/log/log_dispatch.py
@@ -4,12 +4,7 @@ import os
import traceback
from typing import Any, List, Optional, Union
-from kajson.sandbox_manager import sandbox_manager
-
-from pipelex.tools.log.log_config import (
- CallerInfoTemplate,
- LogConfig,
-)
+from pipelex.tools.log.log_config import CallerInfoTemplate, LogConfig, LogMode
from pipelex.tools.misc.json_utils import purify_json, purify_json_dict, purify_json_list
@@ -25,6 +20,10 @@ class LogDispatch:
def __init__(self):
self.project_name: Optional[str] = None
self._log_config_instance: Optional[LogConfig] = None
+ self.log_mode: LogMode = LogMode.RICH
+
+ def set_log_mode(self, mode: LogMode):
+ self.log_mode = mode
def reset(self):
"""
@@ -67,6 +66,7 @@ class LogDispatch:
raise RuntimeError("LogConfig is already set. You can only call log.configure() once.")
self._log_config_instance = log_config
self.project_name = project_name
+ self.log_mode = log_config.log_mode
########################################################
# Private methods
@@ -249,10 +249,12 @@ class LogDispatch:
if not self._log_config.is_console_logging_enabled:
return
- if sandbox_manager.is_in_sandbox():
- logger = logging.getLogger(self._log_config.generic_poor_logger)
- logger.log(level=severity, msg=message, stacklevel=6)
- return
+ match self.log_mode:
+ case LogMode.RICH:
+ pass
+ case LogMode.POOR:
+ logger = logging.getLogger(self._log_config.generic_poor_logger)
+ logger.log(level=severity, msg=message, stacklevel=6)
stack = inspect.stack()
try:
diff --git a/pipelex/tools/log/log_formatter.py b/pipelex/tools/log/log_formatter.py
index 1a8af6e08..ac4de0704 100644
--- a/pipelex/tools/log/log_formatter.py
+++ b/pipelex/tools/log/log_formatter.py
@@ -28,7 +28,7 @@ def emoji_for_channel(channel_name: str) -> Optional[str]:
elif channel_name.startswith("kajson"):
# space added to make it look better
return "*️⃣ "
- elif channel_name.startswith("#sandbox"):
+ elif channel_name.startswith("#poor-log"):
# space added to make it look better
return "🧿 "
elif channel_name.startswith("pipelex"):
diff --git a/pipelex/tools/misc/pretty.py b/pipelex/tools/misc/pretty.py
index 08c514ba2..d3f48f16c 100644
--- a/pipelex/tools/misc/pretty.py
+++ b/pipelex/tools/misc/pretty.py
@@ -1,7 +1,6 @@
import shutil
-from typing import Any, List, Optional, Union
+from typing import Any, ClassVar, List, Optional, Union
-from kajson.sandbox_manager import sandbox_manager
from rich import print as rich_print
from rich.panel import Panel
from rich.pretty import Pretty
@@ -10,6 +9,7 @@ from rich.table import Table
from rich.text import Text, TextType
from pipelex.tools.misc.terminal_utils import BOLD_FONT, RESET_FONT, TerminalColor
+from pipelex.types import StrEnum
TEXT_COLOR = TerminalColor.WHITE
TITLE_COLOR = TerminalColor.CYAN
@@ -22,100 +22,122 @@ def pretty_print(
subtitle: Optional[TextType] = None,
border_style: Optional[StyleType] = None,
):
- if sandbox_manager.is_in_sandbox():
- # we are in a sandbox, so we can't use rich
- pretty_print_in_sandbox(content=content, title=title, subtitle=subtitle)
- else:
- pretty_print_using_rich(content=content, title=title, subtitle=subtitle, border_style=border_style)
+ PrettyPrinter.pretty_print(content=content, title=title, subtitle=subtitle, border_style=border_style)
-def pretty_print_using_rich(
- content: Union[str, Any],
- title: Optional[TextType] = None,
- subtitle: Optional[TextType] = None,
- border_style: Optional[StyleType] = None,
-):
- if isinstance(content, str):
- if content.startswith(("http://", "https://")):
- content = Text(content, style="link " + content, no_wrap=True)
+class PrettyPrintMode(StrEnum):
+ RICH = "rich"
+ POOR = "poor"
+
+
+class PrettyPrinter:
+ mode: ClassVar[PrettyPrintMode] = PrettyPrintMode.RICH
+
+ @classmethod
+ def pretty_print(
+ cls,
+ content: Union[str, Any],
+ title: Optional[TextType] = None,
+ subtitle: Optional[TextType] = None,
+ border_style: Optional[StyleType] = None,
+ ):
+ match cls.mode:
+ case PrettyPrintMode.RICH:
+ cls.pretty_print_using_rich(content=content, title=title, subtitle=subtitle, border_style=border_style)
+ case PrettyPrintMode.POOR:
+ cls.pretty_print_without_rich(content=content, title=title, subtitle=subtitle)
+
+ @classmethod
+ def pretty_print_using_rich(
+ cls,
+ content: Union[str, Any],
+ title: Optional[TextType] = None,
+ subtitle: Optional[TextType] = None,
+ border_style: Optional[StyleType] = None,
+ ):
+ if isinstance(content, str):
+ if content.startswith(("http://", "https://")):
+ content = Text(content, style="link " + content, no_wrap=True)
+ else:
+ content = Text(str(content)) # Treat all other strings as plain text
+ elif isinstance(content, Table):
+ # For tables, print directly without a panel
+ if title:
+ rich_print(f"\n[bold blue]{title}[/]\n")
+ rich_print(content)
+ rich_print("\n")
+ if subtitle:
+ rich_print(f"\n[dim]{subtitle}[/]")
+ return
else:
- content = Text(str(content)) # Treat all other strings as plain text
- elif isinstance(content, Table):
- # For tables, print directly without a panel
+ content = Pretty(content)
+ panel = Panel(
+ content,
+ title=title,
+ subtitle=subtitle,
+ expand=False,
+ title_align="left",
+ subtitle_align="right",
+ padding=(0, 1),
+ border_style=border_style or "",
+ )
+ rich_print(panel)
+
+ @classmethod
+ def pretty_print_without_rich(
+ cls,
+ content: Union[str, Any],
+ title: Optional[TextType] = None,
+ subtitle: Optional[TextType] = None,
+ ):
+ if isinstance(content, str) and content.startswith(("http://", "https://")):
+ cls.pretty_print_url_without_rich(content=content, title=title, subtitle=subtitle)
+ return
+ title = title or ""
+ if subtitle:
+ title += f" ({subtitle})"
+ terminal_width = shutil.get_terminal_size().columns
+ content_str = f"{content}"
+ max_content_width = terminal_width - len(title) - 8 # Accounting for frame and padding
+ wrapped_lines: List[str] = []
+ for line in content_str.splitlines():
+ while len(line) > max_content_width:
+ wrapped_lines.append(line[:max_content_width])
+ line = line[max_content_width:]
+ wrapped_lines.append(line)
+
+ if not wrapped_lines:
+ wrapped_lines.append("")
+
+ frame_width = max(len(title) + 6, max(len(line) for line in wrapped_lines) + 6)
+ top_border = "╭" + "─" * (frame_width - 2) + "╮"
+ bottom_border = "╰" + "─" * (frame_width - 2) + "╯"
+
+ print(f"{BORDER_COLOR}{top_border}{RESET_FONT}")
if title:
- rich_print(f"\n[bold blue]{title}[/]\n")
- rich_print(content)
- rich_print("\n")
+ print(f"{BORDER_COLOR}│ {BOLD_FONT}{TITLE_COLOR}{title}{RESET_FONT}:{' ' * (frame_width - len(title) - 4)}{BORDER_COLOR}│{RESET_FONT}")
+ for line in wrapped_lines:
+ padding = " " * (frame_width - len(line) - 3)
+ print(f"{BORDER_COLOR}│ {TEXT_COLOR}{line}{RESET_FONT}{padding}{BORDER_COLOR}│{RESET_FONT}")
+ print(f"{BORDER_COLOR}{bottom_border}{RESET_FONT}")
+
+ @classmethod
+ def pretty_print_url_without_rich(
+ cls,
+ content: Union[str, Any],
+ title: Optional[TextType] = None,
+ subtitle: Optional[TextType] = None,
+ ):
+ title = title or ""
if subtitle:
- rich_print(f"\n[dim]{subtitle}[/]")
- return
- else:
- content = Pretty(content)
- panel = Panel(
- content,
- title=title,
- subtitle=subtitle,
- expand=False,
- title_align="left",
- subtitle_align="right",
- padding=(0, 1),
- border_style=border_style or "",
- )
- rich_print(panel)
-
-
-def pretty_print_in_sandbox(
- content: Union[str, Any],
- title: Optional[TextType] = None,
- subtitle: Optional[TextType] = None,
-):
- if isinstance(content, str) and content.startswith(("http://", "https://")):
- pretty_print_url_in_sandbox(content=content, title=title, subtitle=subtitle)
- return
- title = title or ""
- if subtitle:
- title += f" ({subtitle})"
- terminal_width = shutil.get_terminal_size().columns
- content_str = f"{content}"
- max_content_width = terminal_width - len(title) - 8 # Accounting for frame and padding
- wrapped_lines: List[str] = []
- for line in content_str.splitlines():
- while len(line) > max_content_width:
- wrapped_lines.append(line[:max_content_width])
- line = line[max_content_width:]
- wrapped_lines.append(line)
-
- if not wrapped_lines:
- wrapped_lines.append("")
-
- frame_width = max(len(title) + 6, max(len(line) for line in wrapped_lines) + 6)
- top_border = "╭" + "─" * (frame_width - 2) + "╮"
- bottom_border = "╰" + "─" * (frame_width - 2) + "╯"
-
- print(f"{BORDER_COLOR}{top_border}{RESET_FONT}")
- if title:
- print(f"{BORDER_COLOR}│ {BOLD_FONT}{TITLE_COLOR}{title}{RESET_FONT}:{' ' * (frame_width - len(title) - 4)}{BORDER_COLOR}│{RESET_FONT}")
- for line in wrapped_lines:
- padding = " " * (frame_width - len(line) - 3)
- print(f"{BORDER_COLOR}│ {TEXT_COLOR}{line}{RESET_FONT}{padding}{BORDER_COLOR}│{RESET_FONT}")
- print(f"{BORDER_COLOR}{bottom_border}{RESET_FONT}")
-
-
-def pretty_print_url_in_sandbox(
- content: Union[str, Any],
- title: Optional[TextType] = None,
- subtitle: Optional[TextType] = None,
-):
- title = title or ""
- if subtitle:
- title += f" ({subtitle})"
- terminal_width = shutil.get_terminal_size().columns
- frame_width = terminal_width - 2
- top_border = "╭" + "─" * (frame_width - 2) + "╮"
- bottom_border = "╰" + "─" * (frame_width - 2) + "╯"
-
- print(f"{BORDER_COLOR}{top_border}{RESET_FONT}")
- if title:
- print(f"{BORDER_COLOR}│ {BOLD_FONT}{TITLE_COLOR}{title}{RESET_FONT}:{' ' * (frame_width - len(title) - 4)}{BORDER_COLOR}│{RESET_FONT}")
- print(f"{TEXT_COLOR}{content}{RESET_FONT}")
- print(f"{BORDER_COLOR}{bottom_border}{RESET_FONT}")
+ title += f" ({subtitle})"
+ terminal_width = shutil.get_terminal_size().columns
+ frame_width = terminal_width - 2
+ top_border = "╭" + "─" * (frame_width - 2) + "╮"
+ bottom_border = "╰" + "─" * (frame_width - 2) + "╯"
+
+ print(f"{BORDER_COLOR}{top_border}{RESET_FONT}")
+ if title:
+ print(f"{BORDER_COLOR}│ {BOLD_FONT}{TITLE_COLOR}{title}{RESET_FONT}:{' ' * (frame_width - len(title) - 4)}{BORDER_COLOR}│{RESET_FONT}")
+ print(f"{TEXT_COLOR}{content}{RESET_FONT}")
+ print(f"{BORDER_COLOR}{bottom_border}{RESET_FONT}")
diff --git a/pipelex/tools/typing/pydantic_utils.py b/pipelex/tools/typing/pydantic_utils.py
index f550ccfa3..fdcb995cb 100644
--- a/pipelex/tools/typing/pydantic_utils.py
+++ b/pipelex/tools/typing/pydantic_utils.py
@@ -109,7 +109,7 @@ def serialize_model(
# Identify which fields should be excluded
fields_to_exclude: Set[str] = set()
- for field_name, field_info in obj.model_fields.items():
+ for field_name, field_info in obj.__class__.model_fields.items():
json_schema_extra = field_info.json_schema_extra
is_hidden = json_schema_extra and isinstance(json_schema_extra, dict) and json_schema_extra.get(ExtraFieldAttribute.IS_HIDDEN) is True
match field_visibility:
@@ -124,7 +124,7 @@ def serialize_model(
# Build a dict, omitting hidden fields. Recursively handle nested models.
data: Dict[str, Any] = {}
- for field_name, _ in obj.model_fields.items():
+ for field_name, _ in obj.__class__.model_fields.items():
if field_name in fields_to_exclude:
continue # Skip hidden fields
diff --git a/pyproject.toml b/pyproject.toml
index 9efaeba8d..a5f4eb9ae 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -24,7 +24,7 @@ dependencies = [
"instructor>=1.8.3",
"jinja2>=3.1.4",
"json2html>=1.3.0",
- "kajson==0.1.5",
+ "kajson==0.1.6",
"markdown>=3.6",
"networkx>=3.4.2",
"openai>=1.60.1",
diff --git a/tests/pipelex/pipelex_asynch/test_pipe_llm_prompt.py b/tests/pipelex/pipelex_asynch/test_pipe_llm_prompt.py
index 780515b49..a86d46cd3 100644
--- a/tests/pipelex/pipelex_asynch/test_pipe_llm_prompt.py
+++ b/tests/pipelex/pipelex_asynch/test_pipe_llm_prompt.py
@@ -1,8 +1,11 @@
+from typing import List, Tuple, Type
+
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.stuff_content import StructuredContent
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
@@ -76,7 +79,7 @@ class TestPipeLLMPrompt:
async def test_prompt_with_output_structure(self, pipe_run_mode: PipeRunMode):
"""Test prompt with output structure for different content types."""
- test_cases = [
+ test_cases: List[Tuple[Type[StructuredContent], str, str]] = [
(SimpleTextContent, "Empty class inheritance", "test.SimpleTextContent"),
(SimpleStructuredContent, "Primitive types", "test.SimpleStructuredContent"),
(DocumentTypeContent, "Enum fields", "test.DocumentTypeContent"),
diff --git a/tests/tools/class_registry/test_class_registry.py b/tests/tools/class_registry/test_class_registry.py
index 780810abf..3ade36f4e 100644
--- a/tests/tools/class_registry/test_class_registry.py
+++ b/tests/tools/class_registry/test_class_registry.py
@@ -1,9 +1,9 @@
from typing import List
import pytest
-from kajson.class_registry import class_registry
from pipelex.core.stuff_content import StuffContent
+from pipelex.hub import get_class_registry
from tests.tools.test_data import ClassRegistryTestCases
@@ -25,6 +25,7 @@ class TestClassRegistry:
classes_to_register: List[str],
classes_not_to_register: List[str],
) -> None:
+ class_registry = get_class_registry()
class_registry.register_classes_in_folder(folder_path=folder, base_class=StuffContent)
for class_name in classes_to_register:
diff --git a/tests/tools/misc/test_pretty.py b/tests/tools/misc/test_pretty.py
index 35b76aca4..12a269093 100644
--- a/tests/tools/misc/test_pretty.py
+++ b/tests/tools/misc/test_pretty.py
@@ -6,7 +6,7 @@ import pytest
from pydantic import BaseModel, Field
from pytest import CaptureFixture
-from pipelex.tools.misc.pretty import pretty_print, pretty_print_in_sandbox
+from pipelex.tools.misc.pretty import PrettyPrinter, pretty_print
class Address(BaseModel):
@@ -199,7 +199,7 @@ class TestPrettyPrint:
class TestPrettyPrintInSandbox:
def test_empty_content(self, capsys: CaptureFixture[str]):
- pretty_print_in_sandbox(content="")
+ PrettyPrinter.pretty_print_without_rich(content="")
captured = capsys.readouterr()
output = remove_ansi_escape_codes(captured.out)
@@ -213,7 +213,7 @@ class TestPrettyPrintInSandbox:
assert output == expected_output, f"Output did not match expected:\n{output}"
def test_none_content(self, capsys: CaptureFixture[str]):
- pretty_print_in_sandbox(content=None, title="title", subtitle="subtitle")
+ PrettyPrinter.pretty_print_without_rich(content=None, title="title", subtitle="subtitle")
captured = capsys.readouterr()
output = remove_ansi_escape_codes(captured.out)
diff --git a/uv.lock b/uv.lock
index 7b67198be..7ae501dea 100644
--- a/uv.lock
+++ b/uv.lock
@@ -889,14 +889,14 @@ wheels = [
[[package]]
name = "kajson"
-version = "0.1.5"
+version = "0.1.6"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "pydantic" },
]
-sdist = { url = "https://files.pythonhosted.org/packages/51/38/300e06d9e5c98d2bcb922e3d2813836f74754a117bcf5c9ff019a20eea0b/kajson-0.1.5.tar.gz", hash = "sha256:cf166e417642127e4a4bca3d31788b64fcd1969f6bc103df2fa59e069c27b8a8", size = 15061, upload-time = "2025-06-02T15:28:52.403Z" }
+sdist = { url = "https://files.pythonhosted.org/packages/40/cf/eb2cc86fc0f56b982692d2a2401d3464e8143dfc7e6549afa0eaf3613053/kajson-0.1.6.tar.gz", hash = "sha256:163abcfe8c376adb351d4e7acdc82744e2ddcb7ae4082803284318cc4d240f06", size = 15236, upload-time = "2025-06-19T10:18:00.314Z" }
wheels = [
- { url = "https://files.pythonhosted.org/packages/b5/7c/12f2167e1b1056e73224b7c4f8062807af47f0bc6a26ef773e3f8219c773/kajson-0.1.5-py3-none-any.whl", hash = "sha256:5fbc4065fa85f56dccf93d644a9d3ee1bccb504d7f27f030a1beacc6ba21e1d6", size = 21090, upload-time = "2025-06-02T15:28:50.765Z" },
+ { url = "https://files.pythonhosted.org/packages/22/75/53cc71d915c4269c624f8e76f10e0aff9f1112d6b568ac1b987d1b5df4b1/kajson-0.1.6-py3-none-any.whl", hash = "sha256:93ddca59dd225a79d8ffb9b54170e98b4b6c8a1f999bcf2d5981d919e132538d", size = 21594, upload-time = "2025-06-19T10:17:58.919Z" },
]
[[package]]
@@ -1619,7 +1619,7 @@ wheels = [
[[package]]
name = "pipelex"
-version = "0.4.2"
+version = "0.4.3"
source = { editable = "." }
dependencies = [
{ name = "aiofiles" },
@@ -1712,7 +1712,7 @@ requires-dist = [
{ name = "instructor", specifier = ">=1.8.3" },
{ name = "jinja2", specifier = ">=3.1.4" },
{ name = "json2html", specifier = ">=1.3.0" },
- { name = "kajson", specifier = "==0.1.5" },
+ { name = "kajson", specifier = "==0.1.6" },
{ name = "markdown", specifier = ">=3.6" },
{ name = "mistralai", marker = "extra == 'mistralai'", specifier = "==1.5.2" },
{ name = "mkdocs", marker = "extra == 'docs'", specifier = "==1.6.1" },