haive.core.common.mixins.prompt_template_mixin¶
PromptTemplateMixin: Advanced prompt template integration for engine classes.
This module provides the PromptTemplateMixin class, which adds sophisticated prompt template management capabilities to any engine class. The mixin enables automatic input schema derivation, prompt template validation, and seamless composition with existing engine functionality.
The PromptTemplateMixin is designed to integrate with Haive’s engine architecture, particularly AugLLMConfig, to provide dynamic schema generation based on prompt template requirements while preserving existing engine behaviors.
- Key Features:
Automatic conversion of prompt templates to InvokableEngines
Dynamic input schema derivation with intelligent composition
Prompt template validation and preprocessing
Schema composition with existing engine schemas
Field-level validation integration via Pydantic validators
Support for both override and composition approaches
- Architecture:
The mixin uses method override patterns to integrate with engine classes:
Overrides derive_input_schema() to incorporate prompt template variables
Provides field validators for prompt template preprocessing
Offers helper methods for prompt formatting and variable management
- Integration Patterns:
Method Override: derive_input_schema() method is overridden to check for prompt templates and compose schemas when present
Field Validation: @field_validator decorators preprocess prompt templates
Composition: Existing schemas are preserved and extended, not replaced
Examples
Basic integration with an engine class:
from haive.core.common.mixins.prompt_template_mixin import PromptTemplateMixin
from haive.core.engine.base import InvokableEngine
class MyEngine(PromptTemplateMixin, InvokableEngine):
prompt_template: Optional[BasePromptTemplate] = None
# The mixin automatically enhances input schema derivation
pass
# Usage
engine = MyEngine(prompt_template=my_template)
schema = engine.derive_input_schema() # Includes prompt variables
Advanced usage with schema composition:
# Engine with existing input schema
class AdvancedEngine(PromptTemplateMixin, InvokableEngine):
def get_base_input_schema(self):
return MyExistingSchema
# The mixin will compose prompt variables with existing schema
engine = AdvancedEngine(prompt_template=chat_template)
combined_schema = engine.derive_input_schema()
- Classes:
PromptTemplateMixin: Main mixin class for prompt template integration
- Dependencies:
langchain_core: For prompt template functionality and message types
pydantic: For schema generation, validation, and field validation
typing: For type hints and optional typing support
- Author:
Haive Core Team
- Version:
1.0.0
See also
haive.core.engine.prompt_template.PromptTemplateEngine: Standalone engine
haive.core.engine.aug_llm.config.AugLLMConfig: Primary integration target
haive.core.schema.schema_composer.SchemaComposer: Schema composition utilities
Classes¶
Advanced mixin for integrating prompt template functionality into engine classes. |
Module Contents¶
- class haive.core.common.mixins.prompt_template_mixin.PromptTemplateMixin[source]¶
Advanced mixin for integrating prompt template functionality into engine classes.
This mixin provides comprehensive prompt template management capabilities, enabling any engine class to automatically derive input schemas from prompt templates while preserving existing functionality through intelligent composition.
The mixin is designed to be non-invasive and compatible with existing engine architectures. It overrides key methods like derive_input_schema() to enhance functionality rather than replace it, ensuring backward compatibility.
- Key Capabilities:
Automatic prompt template to engine conversion
Dynamic input schema derivation with type inference
Intelligent schema composition (prompt + existing schemas)
Prompt template validation and preprocessing
Field-level integration via Pydantic validators
Configurable behavior (enable/disable prompt schema usage)
- Required Fields:
Classes using this mixin must define: - prompt_template: Optional[BasePromptTemplate] = None
- Optional Configuration:
_use_prompt_for_input_schema: bool = True (control schema derivation)
_prompt_engine: Internal cache for prompt template engine
- Method Override Pattern:
The mixin overrides derive_input_schema() using a safe pattern: 1. Attempts to call parent class implementation 2. Checks if prompt template schema derivation is enabled 3. Derives schema from prompt template if present 4. Composes schemas intelligently (prompt + parent) 5. Falls back gracefully on any errors
Examples
Basic usage:
class MyEngine(PromptTemplateMixin, InvokableEngine): prompt_template: Optional[BasePromptTemplate] = None engine = MyEngine(prompt_template=template) schema = engine.derive_input_schema() # Enhanced with prompt fields
With existing schema:
class ComplexEngine(PromptTemplateMixin, SomeOtherMixin, InvokableEngine): # Existing schema logic preserved and enhanced pass
- Integration Notes:
Safe to use with multiple inheritance
Preserves existing derive_input_schema() behavior
Graceful error handling prevents disruption
Can be enabled/disabled at runtime
See also
PromptTemplateEngine: Standalone engine implementation
AugLLMConfig: Primary usage example with full integration
- compose_with_prompt_schema(base_schema)[source]¶
Compose a base schema with the prompt template’s input schema.
- derive_input_schema()[source]¶
Override derive_input_schema to intelligently compose prompt template schemas.
This method enhances the standard input schema derivation process by incorporating prompt template variables when a prompt template is present. It uses a safe override pattern that preserves existing functionality while adding prompt template awareness.
- Returns:
- A Pydantic model class that includes:
All fields from the parent class schema (if any)
Additional fields derived from prompt template variables
Proper field types, defaults, and validation rules
- Return type:
Optional[Type[BaseModel]]
- Process:
Attempts to derive schema from parent class (preserves existing behavior)
Checks if prompt template schema derivation is enabled
Derives schema from prompt template variables if present
Intelligently composes parent + prompt schemas when both exist
Falls back gracefully on any errors to ensure stability
Examples
With prompt template only:
# Engine with prompt template engine.prompt_template = PromptTemplate.from_template("Hello {name}") schema = engine.derive_input_schema() # schema includes 'name' field
With existing schema + prompt template:
# Engine with both existing schema and prompt template # Result combines both sets of fields intelligently combined_schema = engine.derive_input_schema()
Note
Respects _use_prompt_for_input_schema configuration flag
Preserves all existing field properties (defaults, validation, etc.)
Prompt template fields take precedence in case of name conflicts
Error handling ensures method never fails completely
- derive_prompt_input_schema()[source]¶
Derive input schema from the prompt template.
- Return type:
type[pydantic.BaseModel] | None
- derive_prompt_output_schema()[source]¶
Derive output schema from the prompt template.
- Return type:
type[pydantic.BaseModel] | None
- enable_prompt_schema_derivation(enabled=True)[source]¶
Enable or disable prompt template schema derivation.
- Parameters:
enabled (bool)
- get_effective_input_schema()[source]¶
Get the effective input schema, considering all factors.
- Return type:
type[pydantic.BaseModel] | None
- get_prompt_aware_input_fields()[source]¶
Get input fields considering prompt template requirements.
- get_prompt_engine()[source]¶
Get or create a cached PromptTemplateEngine for the current prompt template.
This method provides lazy initialization of a PromptTemplateEngine wrapper around the current prompt template. The engine is cached to avoid recreation on multiple calls, improving performance.
- Returns:
- A PromptTemplateEngine instance wrapping
the current prompt template, or None if no prompt template is set.
- Return type:
Optional[PromptTemplateEngine]
Note
The engine is cached in _prompt_engine for reuse
Engine name is automatically generated from the parent object’s name
Returns None if no prompt template is configured
Examples
# Get the prompt engine (creates if first time) engine = self.get_prompt_engine() if engine: schema = engine.derive_input_schema() result = engine.invoke(input_data)
- set_base_input_schema(schema)[source]¶
Set the base input schema to use for composition.
- Parameters:
schema (type[pydantic.BaseModel] | None)
- update_prompt_partials(**partials)[source]¶
Update partial variables in the prompt template.
- Return type:
- validate_prompt_inputs(input_data)[source]¶
Validate that input data satisfies prompt template requirements.