Skip to content

API Reference parrot.outputs.formatter

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.outputs.formatter

View source

Alias AbstractClient

Re-exported from parrot.clients.base.AbstractClient.

Attribute DEFAULT_RETRY_PROMPTS

Class OutputFormatter

Formatter for AI responses supporting multiple output modes.

Supports LLM-based retry for fixing malformed outputs (e.g., invalid JSON). When a rendering fails due to parsing errors, the formatter can use an LLM client to attempt to fix the output automatically.

Parameter Type Default Description
template_engine Optional[TemplateEngine] None Optional TemplateEngine instance for template-based rendering. If not provided, a new one will be created when needed.
llm_client Optional['AbstractClient'] None Optional LLM client instance for retry functionality. Required for format_with_retry() to work.
retry_config Optional[OutputRetryConfig] None Optional retry configuration. If not provided, defaults will be used when retry is attempted.

Attributes:

Attribute Type Description
llm_client Optional['AbstractClient'] Get the current LLM client.
retry_config OutputRetryConfig Get the current retry configuration.

Method OutputFormatter.get_system_prompt

Get the system prompt for a given output mode.

get_system_prompt(mode: OutputMode) -> Optional[str]

Parameter Type Default Description
mode OutputMode OutputMode enum value

Returns: Optional[str] — System prompt string or None if mode has no specific prompt

Method OutputFormatter.has_system_prompt

Check if an output mode has a registered system prompt.

has_system_prompt(mode: OutputMode) -> bool

Parameter Type Default Description
mode OutputMode OutputMode enum value

Returns: bool — True if mode has a system prompt

Method OutputFormatter.format

Format output based on mode

format(mode: OutputMode, data: Any, **kwargs) -> Tuple[str, Optional[str]]

Parameter Type Default Description
mode OutputMode
data Any
**kwargs {}

Returns: Tuple[str, Optional[str]] — Tuple[str, Optional[str]]: (content, wrapped_content) - content: main formatted output - wrapped_content: optional wrapped version (e.g., HTML)

Method OutputFormatter.extract_data

Extract data from response using Table extraction logic.

extract_data(data: Any) -> Optional[List[Dict[str, Any]]]

Parameter Type Default Description
data Any The response data to extract from

Returns: Optional[List[Dict[str, Any]]] — List of dictionaries representing the data, or None if extraction failed/empty

Method OutputFormatter.add_template

Add an in-memory template for use with TEMPLATE_REPORT mode.

add_template(name: str, content: str) -> None

Parameter Type Default Description
name str Template name (e.g., 'report.html', 'summary.md')
content str Jinja2 template content

Method OutputFormatter.set_llm_client

Set or update the LLM client used for retry operations.

set_llm_client(client: 'AbstractClient') -> None

Parameter Type Default Description
client 'AbstractClient' An AbstractClient instance (Claude, GPT, etc.)

Method OutputFormatter.set_retry_config

Set or update the retry configuration.

set_retry_config(config: OutputRetryConfig) -> None

Parameter Type Default Description
config OutputRetryConfig OutputRetryConfig instance

Method OutputFormatter.format_with_retry

Format output with automatic LLM-based retry on parsing failures.

This method attempts to format the output normally. If parsing fails, it uses the configured LLM client to request a fix for the malformed output, then retries formatting.

format_with_retry(mode: OutputMode, data: Any, original_prompt: Optional[str] = None, llm_client: Optional['AbstractClient'] = None, retry_config: Optional[OutputRetryConfig] = None, **kwargs) -> OutputRetryResult

Parameter Type Default Description
mode OutputMode OutputMode enum value
data Any The data to format (typically an AIMessage response)
original_prompt Optional[str] None Optional original user prompt that generated the data. This provides context to the LLM for better fixes.
llm_client Optional['AbstractClient'] None Optional LLM client to use for this call (overrides default)
retry_config Optional[OutputRetryConfig] None Optional retry config for this call (overrides default)
**kwargs {} Additional arguments passed to the renderer

Returns: OutputRetryResult — OutputRetryResult with success status, content, and retry information

Alias OutputMode

Re-exported from parrot.models.outputs.OutputMode.

Class OutputRetryConfig

Configuration for LLM-based output retry on parsing failures.

When output parsing fails (e.g., malformed JSON for ECharts), this config controls how the system will use an LLM to attempt to fix the output.

Parameter Type Default Description
max_retries int 2
retry_on_parse_error bool True
retry_model Optional[str] None
retry_temperature float 0.1
retry_max_tokens int 4096
include_original_prompt bool True
custom_retry_prompts dict dict()

Fields:

Field Type Default Description
max_retries int 2 Maximum number of retry attempts (default: 2)
retry_on_parse_error bool True Whether to retry on parsing/validation errors
retry_model Optional[str] None Optional specific model to use for retries (uses client default if None)
retry_temperature float 0.1 Temperature for retry requests (lower = more deterministic)
retry_max_tokens int 4096 Max tokens for retry response
include_original_prompt bool True Whether to include the original user prompt in retry
custom_retry_prompts dict field(default_factory=dict) Optional dict mapping OutputMode to custom retry prompts

Method OutputRetryConfig.get_retry_prompt

Get custom retry prompt for a specific output mode.

get_retry_prompt(mode: OutputMode) -> Optional[str]

Parameter Type Default Description
mode OutputMode

Returns: Optional[str]

Class OutputRetryResult

Result from an output retry attempt.

Parameter Type Default Description
success bool
content Any
wrapped_content Optional[Any]
retry_count int 0
original_error Optional[str] None
final_error Optional[str] None

Fields:

Field Type Default Description
success bool Whether the retry produced valid output
content Any The formatted content (original or fixed)
wrapped_content Optional[Any] Optional wrapped version (e.g., HTML)
retry_count int 0 Number of retry attempts made
original_error Optional[str] None The original error that triggered retry
final_error Optional[str] None The final error if all retries failed (None if success)

Alias TemplateEngine

Re-exported from parrot.template.engine.TemplateEngine.

Alias get_output_prompt

Re-exported from parrot.outputs.formats.get_output_prompt.

Alias get_renderer

Re-exported from parrot.outputs.formats.get_renderer.

Alias has_system_prompt

Re-exported from parrot.outputs.formats.has_system_prompt.

Attribute logger

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally