orka.orchestrator.prompt_rendering module
Prompt Rendering Module
This module provides Jinja2-based template rendering capabilities for dynamic prompt construction in the OrKa orchestration framework. It handles the rendering of agent prompts with dynamic context data and provides utilities for processing agent responses.
The PromptRenderer
class is integrated into the main orchestrator through
multiple inheritance composition, providing seamless template processing capabilities
throughout the workflow execution.
Key Features
- Dynamic Template Rendering
Uses Jinja2 templating engine for flexible prompt construction
- Context Integration
Automatically injects previous agent outputs and workflow state into templates
- Response Processing
Handles complex agent response structures and extracts relevant data
- Error Resilience
Gracefully handles template rendering failures to prevent workflow interruption
Usage Example
from orka.orchestrator.prompt_rendering import PromptRenderer
renderer = PromptRenderer()
# Render a template with context
result = renderer.render_prompt(
"Answer this question: {{ input }} using {{ previous_outputs.retriever }}",
{
"input": "What is Python?",
"previous_outputs": {"retriever": "Python is a programming language"}
}
)
- class orka.orchestrator.prompt_rendering.PromptRenderer[source]
Bases:
object
Handles prompt rendering and template processing using Jinja2.
This class provides methods for rendering dynamic prompts with context data, processing agent responses, and managing template-related operations within the orchestrator workflow.
The renderer supports complex template structures and provides robust error handling to ensure that template failures don’t interrupt workflow execution.
- render_prompt(template_str, payload)[source]
Render a Jinja2 template string with the given payload.
This method is the core template rendering functionality, taking a template string and context payload to produce a rendered prompt for agent execution.
- Parameters:
template_str (str) – The Jinja2 template string to render
payload (dict) – Context data for template variable substitution
- Returns:
The rendered template with variables substituted
- Return type:
str
- Raises:
ValueError – If template_str is not a string
jinja2.TemplateError – If template syntax is invalid
Example
template = "Hello {{ name }}, you have {{ count }} messages" context = {"name": "Alice", "count": 5} result = renderer.render_prompt(template, context) # Returns: "Hello Alice, you have 5 messages"
- static normalize_bool(value)[source]
Normalize a value to boolean with support for complex agent responses.
This utility method handles the conversion of various data types to boolean values, with special support for complex agent response structures that may contain nested results.
- Parameters:
value – The value to normalize (bool, str, dict, or other)
- Returns:
The normalized boolean value
- Return type:
bool
- Supported Input Types:
bool: Returned as-is
str: ‘true’, ‘yes’ (case-insensitive) → True, others → False
dict: Extracts from ‘result’ or ‘response’ keys with recursive processing
other: Defaults to False
Example
# Simple cases assert PromptRenderer.normalize_bool(True) == True assert PromptRenderer.normalize_bool("yes") == True assert PromptRenderer.normalize_bool("false") == False # Complex agent response response = {"result": {"response": "true"}} assert PromptRenderer.normalize_bool(response) == True