Use Case: Accessing and storing Workflow Data in Artifact File Directory

Learn about accessing and storing Workflow Data in the Artifact File Directory.

import os
import pandas as pd
import json

def run_tool(config, args):
    # ✅ Read input from workflow data directory
    workflow_data_dir = os.environ.get('WORKFLOW_DATA_DIRECTORY', '/workflow_data')
    input_file = os.path.join(workflow_data_dir, 'sales_data.csv')
    
    if not os.path.exists(input_file):
        return {"error": f"Input file not found: {input_file}"}
    
    # Process data
    df = pd.read_csv(input_file)
    summary = df.groupby('category').sum()
    
    # ✅ Write output to artifact file directory (using relative path)
    output_file = "sales_summary.csv"
    summary.to_csv(output_file, index=True)
    
    # ✅ Write JSON report to artifact file directory
    report_file = "report.json"
    with open(report_file, 'w') as f:
        json.dump({
            "status": "success",
            "total_rows": len(df),
            "categories": summary.index.tolist()
        }, f, indent=2)
    
    # Both files are automatically visible in Agent Studio UI
    return {
        "status": "success",
        "output_files": [output_file, report_file],
        "message": "Files created in artifact file directory"
    }