Skip to content

Minio connection

upload_run_artifacts(response_text, generated_plots)

Packages and uploads run artifacts (report text and charts) to the MLflow Tracking Server.

This function ensures governance and reproducibility by permanently storing the output of each agent execution.

Fail-Safe Mechanism: This function is designed to be non-blocking. It catches all internal exceptions to ensure that a storage failure (e.g., S3 down) does not crash the main API response returned to the user.

Steps:

  1. Check Context: Verifies if there is an active MLflow run.
  2. Upload Plots: Iterates through generated_plots, validates their existence on disk, and uploads them to the plots/ artifact folder.
  3. Process Report: Converts the Markdown to an "offline" version using _create_offline_markdown.
  4. Upload Report: Logs the modified text as report.md.

Parameters:

Name Type Description Default
response_text str

The full markdown report text.

required
generated_plots list[str]

List of filenames for plots generated in this run.

required
Source code in api/src/db/minio_connection.py
37
38
39
40
41
42
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def upload_run_artifacts(response_text: str, generated_plots: list[str]):
    """
    Packages and uploads run artifacts (report text and charts) to the MLflow Tracking Server.

    This function ensures governance and reproducibility by permanently storing
    the output of each agent execution.

    **Fail-Safe Mechanism:**
    This function is designed to be non-blocking. It catches all internal exceptions
    to ensure that a storage failure (e.g., S3 down) does not crash the main API response
    returned to the user.

    **Steps:**

    1.  **Check Context:** Verifies if there is an active MLflow run.
    2.  **Upload Plots:** Iterates through `generated_plots`, validates their existence
        on disk, and uploads them to the `plots/` artifact folder.
    3.  **Process Report:** Converts the Markdown to an "offline" version using
        `_create_offline_markdown`.
    4.  **Upload Report:** Logs the modified text as `report.md`.

    Args:
        response_text (str): The full markdown report text.
        generated_plots (list[str]): List of filenames for plots generated in this run.
    """
    if not settings.MLFLOW_ENABLE:
        return

    try:
        run = mlflow.last_active_run()

        if not run:
            logger.warning("No active MLflow run found. Skipping artifact upload.")
            return

        run_id = run.info.run_id
        logger.info(f"Packaging artifacts for Run ID: {run_id}")

        for filename in generated_plots:
            file_path = settings.PLOTS_DIR / filename

            if file_path.exists():
                mlflow.log_artifact(local_path=str(file_path), artifact_path="plots")
                logger.debug(f"   -> Uploaded: {filename}")
            else:
                logger.warning(f"   -> File missing on disk: {filename}")

        report_offline = _create_offline_markdown(response_text, generated_plots)
        mlflow.log_text(report_offline, "report.md")

        logger.info("Artifact Package (Report + Plots) uploaded successfully.")

    except Exception as e:
        logger.error(f"Governance upload failed: {e}", exc_info=True)