haive.core.contracts.prompt_config¶

Prompt configuration system with contracts and composition.

This module provides focused prompt management extracted from AugLLMConfig, reducing complexity while adding explicit contracts and composition patterns.

Classes¶

PromptConfig

Focused prompt configuration with contracts.

PromptContract

Contract defining prompt's requirements and behavior.

PromptVariable

Definition of a prompt variable with validation.

Module Contents¶

class haive.core.contracts.prompt_config.PromptConfig(/, **data)[source]¶

Bases: pydantic.BaseModel

Focused prompt configuration with contracts.

This replaces scattered prompt management in AugLLMConfig (~150 lines) with a focused, contract-based approach.

Parameters:

data (Any)

prompt_template¶

Main prompt template.

system_message¶

System message for the prompt.

contracts¶

Prompt contracts by name.

partial_variables¶

Partial variables to inject.

format_instructions¶

Format instructions to include.

composition_mode¶

How to compose multiple prompts.

fallback_prompts¶

Fallback prompts if main fails.

include_examples¶

Whether to include examples.

max_prompt_length¶

Maximum prompt length in characters.

Examples

Basic prompt configuration:
>>> config = PromptConfig(
...     prompt_template=ChatPromptTemplate.from_template("Hello {name}"),
...     system_message="You are a helpful assistant"
... )
With contracts:
>>> config = PromptConfig(
...     prompt_template=analysis_prompt,
...     contracts={
...         "analysis": PromptContract(
...             name="analysis",
...             description="Analyze data",
...             variables=[
...                 PromptVariable(name="data", type="object")
...             ]
...         )
...     }
... )

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

add_example(input_vars, expected_output)[source]¶

Add an example to the configuration.

Parameters:
  • input_vars (Dict[str, Any]) – Example input variables.

  • expected_output (str) – Expected output.

Returns:

Self for chaining.

Return type:

PromptConfig

apply_partial_variables()[source]¶

Apply partial variables to the prompt template.

Returns:

Self for chaining.

Return type:

PromptConfig

compose_with(other, mode=None)[source]¶

Compose with another prompt configuration.

Parameters:
  • other (PromptConfig) – Other prompt configuration.

  • mode (Optional[Literal['before', 'after', 'replace']]) – Composition mode.

Returns:

New composed configuration.

Return type:

PromptConfig

get_required_variables()[source]¶

Get all required variables from contracts.

Returns:

Set of required variable names.

Return type:

Set[str]

to_dict()[source]¶

Convert to dictionary for serialization.

Returns:

Configuration as dictionary.

Return type:

Dict[str, Any]

validate_variables(provided)[source]¶

Validate provided variables against contracts.

Parameters:

provided (Dict[str, Any]) – Provided variables.

Returns:

Dictionary of validation errors (empty if valid).

Return type:

Dict[str, str]

class haive.core.contracts.prompt_config.PromptContract(/, **data)[source]¶

Bases: pydantic.BaseModel

Contract defining prompt’s requirements and behavior.

Parameters:

data (Any)

name¶

Prompt identifier.

description¶

What the prompt does.

variables¶

Required variables.

output_format¶

Expected output format.

max_tokens¶

Maximum expected tokens.

temperature_range¶

Recommended temperature range.

examples¶

Example inputs and outputs.

constraints¶

Behavioral constraints.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

classmethod validate_temperature_range(v)[source]¶

Validate temperature range.

Parameters:

v (tuple[float, float]) – Temperature range tuple.

Returns:

Validated temperature range.

Raises:

ValueError – If range is invalid.

Return type:

tuple[float, float]

class haive.core.contracts.prompt_config.PromptVariable(/, **data)[source]¶

Bases: pydantic.BaseModel

Definition of a prompt variable with validation.

Parameters:

data (Any)

name¶

Variable name.

type¶

Expected type of the variable.

required¶

Whether variable is required.

default¶

Default value if not provided.

description¶

Human-readable description.

validation_regex¶

Optional regex for validation.

allowed_values¶

Optional list of allowed values.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.