Skip to content

Manage plots

create_offline_markdown(original_text, plot_filenames)

Rewrites image links in the Markdown report for offline portability.

The live API serves images via endpoints (e.g., /api/v1/plots/...). However, when saving the report to MLflow or zip files, these links need to be relative filesystem paths (e.g., plots/...) to render correctly without a running server.

Parameters:

Name Type Description Default
original_text str

The Markdown text with API URLs.

required
plot_filenames list[str]

List of valid plot filenames to verify/replace.

required

Returns:

Name Type Description
str str

The Markdown text with updated image paths.

Source code in api/src/services/manage_plots.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def create_offline_markdown(original_text: str, plot_filenames: list[str]) -> str:
    """
    Rewrites image links in the Markdown report for offline portability.

    The live API serves images via endpoints (e.g., `/api/v1/plots/...`).
    However, when saving the report to MLflow or zip files, these links need
    to be relative filesystem paths (e.g., `plots/...`) to render correctly
    without a running server.

    Args:
        original_text (str): The Markdown text with API URLs.
        plot_filenames (list[str]): List of valid plot filenames to verify/replace.

    Returns:
        str: The Markdown text with updated image paths.
    """
    offline_text = original_text

    offline_text = re.sub(
        r"\((?:https?://[^)]+)?/api/v1/plots/([^)]+\.png)\)",
        r"(plots/\1)",
        offline_text,
    )

    return offline_text

extract_plots_from_result(result)

Parses the Agent's execution trace to identify generated plot files.

Since the LLM calls a plotting tool, the filename is returned within a ToolReturnPart message. This function scans the history to extract these filenames reliably, rather than relying on the LLM's final text description.

Parameters:

Name Type Description Default
result AgentRunResult

The result object returned by agent.run().

required

Returns:

Type Description
list[str]

list[str]: A list of unique filenames (e.g., ['trend_30d.png']) found

list[str]

in the tool execution outputs.

Source code in api/src/services/manage_plots.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def extract_plots_from_result(result: AgentRunResult) -> list[str]:
    """
    Parses the Agent's execution trace to identify generated plot files.

    Since the LLM calls a plotting tool, the filename is returned within a
    `ToolReturnPart` message. This function scans the history to extract these
    filenames reliably, rather than relying on the LLM's final text description.

    Args:
        result (AgentRunResult): The result object returned by `agent.run()`.

    Returns:
        list[str]: A list of unique filenames (e.g., `['trend_30d.png']`) found
        in the tool execution outputs.
    """
    plot_files = []

    messages = result.all_messages()

    for msg in messages:
        if hasattr(msg, "parts"):
            for part in msg.parts:
                if isinstance(part, ToolReturnPart) and part.tool_name == "plot_tool":
                    content = str(part.content)

                    match = re.search(
                        r"data/plots/([^/\s]+\.png)", content, re.IGNORECASE
                    )

                    if match:
                        filename = match.group(1)
                        plot_files.append(filename)

    return list(set(plot_files))