agents.rag.utils.structured_output_enhancer

Structured Output Enhancer for RAG Agents.

from typing import Any, Dict This utility enables any agent to be enhanced with structured output by appending a SimpleAgent with the appropriate prompt template and Pydantic model. This follows the pattern of keeping prompts focused on generation while parsers handle structure.

Classes

RAGEnhancementFactory

Factory for creating enhanced RAG agents with structured output.

StructuredOutputEnhancer

Utility for enhancing any agent with structured output capabilities.

Functions

create_fusion_enhancer()

Create an enhancer for Fusion RAG structured output.

create_hyde_enhancer()

Create an enhancer for HyDE structured output.

create_memory_enhancer()

Create an enhancer for Memory-aware RAG structured output.

create_speculative_enhancer()

Create an enhancer for Speculative RAG structured output.

demonstrate_enhancement_patterns()

Demonstrate various enhancement patterns.

Module Contents

class agents.rag.utils.structured_output_enhancer.RAGEnhancementFactory

Factory for creating enhanced RAG agents with structured output.

static enhance_simple_rag(llm_config, enhancement_type='hyde')

Create a simple RAG with structured output enhancement.

Parameters:
  • llm_config (haive.core.models.llm.base.LLMConfig) – LLM configuration

  • enhancement_type (str) – Type of enhancement (hyde, fusion, speculative, memory)

Returns:

List of agents forming an enhanced RAG pipeline

Return type:

list[haive.agents.simple.agent.SimpleAgent]

class agents.rag.utils.structured_output_enhancer.StructuredOutputEnhancer(output_model, prompt_style=PromptStyle.DESCRIPTIVE, structured_output_version='v1')

Utility for enhancing any agent with structured output capabilities.

This class provides a clean pattern for appending structured output processing to any existing agent, following the principle that prompts focus on generation while parsers handle structure.

Example

>>> from haive.agents.rag.models import HyDEResult
>>>
>>> # Enhance any agent with HyDE structured output
>>> enhancer = StructuredOutputEnhancer(HyDEResult)
>>> structured_agent = enhancer.create_enhancement_agent(
...     llm_config=llm_config,
...     context_prompt="Based on the retrieved documents, generate a hypothetical document analysis"
... )
>>>
>>> # Or enhance an existing agent pipeline
>>> enhanced_pipeline = enhancer.enhance_agent_sequence([base_agent, retrieval_agent], llm_config)

Initialize the enhancer with a Pydantic output model.

Parameters:
  • output_model (type[pydantic.BaseModel]) – Pydantic model for structured output

  • prompt_style (haive.core.utils.pydantic_utils.base_model_to_prompt.PromptStyle) – Style for generating format instructions

  • structured_output_version (str) – v1 (parser-based) or v2 (tool-based)

create_enhancement_agent(llm_config, context_prompt, agent_name=None, include_state_context=True, **engine_kwargs)

Create a SimpleAgent for structured output enhancement.

Parameters:
  • llm_config (haive.core.models.llm.base.LLMConfig) – LLM configuration

  • context_prompt (str) – Main instruction for what to generate

  • agent_name (str | None) – Name for the enhancement agent

  • include_state_context (bool) – Whether to include previous state context

  • **engine_kwargs – Additional arguments for AugLLMConfig

Returns:

SimpleAgent configured for structured output

Return type:

haive.agents.simple.agent.SimpleAgent

create_enhancement_prompt(context_prompt, include_state_context=True)

Create a prompt template for structured output enhancement.

Parameters:
  • context_prompt (str) – The main instruction for what to generate

  • include_state_context (bool) – Whether to include previous state context

Returns:

ChatPromptTemplate configured for structured output

Return type:

langchain_core.prompts.ChatPromptTemplate

create_format_instructions()

Generate format instructions for the output model.

Return type:

str

enhance_agent_sequence(agents, llm_config, context_prompt=None, **kwargs)

Enhance a sequence of agents by appending structured output processing.

Parameters:
  • agents (list[Any]) – List of existing agents

  • llm_config (haive.core.models.llm.base.LLMConfig) – LLM configuration

  • context_prompt (str | None) – Custom context prompt (auto-generated if None)

  • **kwargs – Additional arguments for the enhancement agent

Returns:

List of agents with structured output enhancement appended

Return type:

list[Any]

agents.rag.utils.structured_output_enhancer.create_fusion_enhancer()

Create an enhancer for Fusion RAG structured output.

Return type:

StructuredOutputEnhancer

agents.rag.utils.structured_output_enhancer.create_hyde_enhancer()

Create an enhancer for HyDE structured output.

Return type:

StructuredOutputEnhancer

agents.rag.utils.structured_output_enhancer.create_memory_enhancer()

Create an enhancer for Memory-aware RAG structured output.

Return type:

StructuredOutputEnhancer

agents.rag.utils.structured_output_enhancer.create_speculative_enhancer()

Create an enhancer for Speculative RAG structured output.

Return type:

StructuredOutputEnhancer

agents.rag.utils.structured_output_enhancer.demonstrate_enhancement_patterns()

Demonstrate various enhancement patterns.

Return type:

dict[str, Any]