Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

pyrit.output

Output module for displaying attack, scenario, and scorer results.

This module provides:

File names indicate output format (pretty.py = ANSI-colored, markdown.py = Markdown). Abstract methods inside each printer determine the data source (memory, REST, fixtures).

Functions

get_default_sink

get_default_sink(default: type[Sink] | None = None) → Sink

Return the appropriate default sink for the current environment.

When default is None, auto-detects: uses IPythonMarkdownSink inside Jupyter/IPython notebooks, otherwise StdoutSink.

ParameterTypeDescription
default`type[Sink]None`

Returns:

output_attack_async

output_attack_async(result: AttackResult, format: OutputFormat = 'pretty', sink: Sink | None = None, include_auxiliary_scores: bool = False, include_pruned_conversations: bool = False, include_adversarial_conversation: bool = False, blur_images: bool = False, blur_radius: int = 20, blurred_dir: str | os.PathLike[str] | None = None) → None

Print an attack result in the specified format to the specified destination.

ParameterTypeDescription
resultAttackResultThe attack result to print.
formatOutputFormatOutput format — “pretty” or “markdown”. Defaults to “pretty”. Defaults to 'pretty'.
sink`SinkNone`
include_auxiliary_scoresboolWhether to include auxiliary scores. Defaults to False. Defaults to False.
include_pruned_conversationsboolWhether to include pruned conversations. Defaults to False. Defaults to False.
include_adversarial_conversationboolWhether to include the adversarial conversation. Defaults to False. Defaults to False.
blur_imagesboolIf True, apply a Gaussian blur to image outputs before rendering them. For “pretty” output, image bytes are blurred in-memory before display. For “markdown” output, a blurred file is written to disk and the markdown links to it instead of the original. The original image file is not modified and remains accessible on disk; this flag is intended to reduce reviewer exposure, not to enforce access control. If blurring fails for any reason (I/O error, decode error, etc.), a warning is logged and a plain-text link to the original is emitted instead of an inline image — the original is not silently rendered. Defaults to False. Defaults to False.
blur_radiusintGaussian blur radius applied when blur_images is True. Defaults to 20. Defaults to 20.
blurred_dir`strPathLike

output_conversation_async

output_conversation_async(messages: list[Message], format: OutputFormat = 'pretty', sink: Sink | None = None, include_scores: bool = False, include_reasoning_trace: bool = False, blur_images: bool = False, blur_radius: int = 20) → None

Print a conversation message history in the specified format.

ParameterTypeDescription
messageslist[Message]The messages to print.
formatOutputFormatOutput format — “pretty” or “markdown”. Defaults to “pretty”. Defaults to 'pretty'.
sink`SinkNone`
include_scoresboolWhether to include scores. Defaults to False. Defaults to False.
include_reasoning_traceboolWhether to include reasoning traces. Defaults to False. Defaults to False.
blur_imagesboolIf True, apply a Gaussian blur to image outputs before rendering them. For “pretty” output (the only format supported here), image bytes are blurred in-memory before display. The original image file is not modified; this flag is intended to reduce reviewer exposure, not to enforce access control. If blurring fails for any reason, a warning is logged and the original is shown (pretty path only). Defaults to False. Defaults to False.
blur_radiusintGaussian blur radius applied when blur_images is True. Defaults to 20. Defaults to 20.

Raises:

output_scenario_async

output_scenario_async(result: ScenarioResult, format: OutputFormat = 'pretty', sink: Sink | None = None, sort_groups_by_success_rate: bool = False) → None

Print a scenario result in the specified format to the specified destination.

ParameterTypeDescription
resultScenarioResultThe scenario result to print.
formatOutputFormatOutput format — “pretty” or “markdown”. Defaults to “pretty”. Defaults to 'pretty'.
sink`SinkNone`
sort_groups_by_success_rateboolWhen True, the Per-Group Breakdown is sorted so that the group with the highest success rate appears first. Defaults to False, which preserves the original insertion order. Defaults to False.

Raises:

output_score_async

output_score_async(scores: list[Score], format: OutputFormat = 'pretty', sink: Sink | None = None) → None

Print a list of scores in the specified format.

ParameterTypeDescription
scoreslist[Score]The scores to print.
formatOutputFormatOutput format — “pretty” or “markdown”. Defaults to “pretty”. Defaults to 'pretty'.
sink`SinkNone`

Raises:

output_scorer_async

output_scorer_async(scorer_identifier: ComponentIdentifier, harm_category: str | None = None, format: OutputFormat = 'pretty', sink: Sink | None = None) → None

Print scorer information in the specified format to the specified destination.

Auto-detects scorer type: if harm_category is provided, renders harm metrics; otherwise renders objective metrics.

ParameterTypeDescription
scorer_identifierComponentIdentifierThe scorer identifier.
harm_category`strNone`
formatOutputFormatOutput format — “pretty” or “markdown”. Defaults to “pretty”. Defaults to 'pretty'.
sink`SinkNone`

Raises:

FileSink

Bases: Sink

Sink that writes text to a file.

Constructor Parameters:

ParameterTypeDescription
pathPathThe file path to write to.
modestrThe file open mode. Defaults to “w” (write, overwrite). Use “a” for append mode. Defaults to 'w'.

Methods:

write_async

write_async(data: str) → None

Write data to a file.

ParameterTypeDescription
datastrThe text to write.

IPythonMarkdownSink

Bases: Sink

Sink that renders markdown via IPython’s display(Markdown(...)).

Falls back to print() if IPython is not available (e.g., outside a Jupyter notebook).

Methods:

write_async

write_async(data: str) → None

Display data as rendered markdown in IPython, or print to stdout.

ParameterTypeDescription
datastrThe markdown text to display.

PrinterBase

Bases: ABC

Abstract base class for all printers.

Subclasses implement render_async to produce formatted text. write_async is concrete: it calls render_async then routes the result through the configured sink.

Constructor Parameters:

ParameterTypeDescription
sink`SinkNone`

Methods:

render_async

render_async(args: Any = (), kwargs: Any = {}) → str

Render output and return it as a string.

Subclasses define the specific signature (e.g., scorer_identifier, result, messages, etc.).

write_async

write_async(args: Any = (), kwargs: Any = {}) → None

Render output and write it to the configured sink.

Calls render_async with all arguments, then writes the result through the sink. Subclasses should not override this method.

Sink

Bases: ABC

Abstract base class for output sinks.

A sink defines where rendered output goes (stdout, file, etc.). All printers write their output through a sink.

Methods:

write_async

write_async(data: str) → None

Write rendered output data.

ParameterTypeDescription
datastrThe rendered text output to write.

StdoutSink

Bases: Sink

Sink that prints text to stdout.

This is the default sink used when no sink is specified.

Methods:

write_async

write_async(data: str) → None

Write data to stdout.

ParameterTypeDescription
datastrThe text to print.