Skip to content

Add a practical Code Interpreter example with JSON/file-based data analysis #117

Description

The current Code Interpreter sample demonstrates how to enable the tool and ask the agent to execute Python code:

04.Tools/code_samples/python/msfoundry/02.python-agent-framework-msfoundry-code-interpreter.ipynb

However, it is not very clear how Code Interpreter should be used for a typical data-analysis scenario, especially when the application already has structured data such as JSON.

It would be helpful to add a more practical example showing how to:

  1. Pass a small JSON object directly in the prompt.
  2. Upload a larger .json or .csv file.
  3. Attach the uploaded file to the Code Interpreter tool.
  4. Ask the agent to analyse the data using Python or pandas.
  5. Retrieve the analysis results and any generated files or charts.
  6. Clean up the uploaded files after the analysis.

This would help developers understand how Code Interpreter can be used as a data-analysis tool rather than only as a general Python execution tool.

Following code uses openai_client, so I am not sure if it is optimized as far as Agent Framework API is concerned, but this works:

import asyncio
import json
import os
from dotenv import load_dotenv

from agent_framework import Agent
from agent_framework.foundry import FoundryChatClient
from azure.identity import AzureCliCredential


async def main():
    load_dotenv()
    data = {
        "assets": [
            {"id": "Machine-1", "temperature": 24.5, "power": 12.1},
            {"id": "Machine-2", "temperature": 29.2, "power": 18.7},
            {"id": "Machine-3", "temperature": 26.1, "power": 13.4},
        ]
    }

    file_path = "analysis_input.json"
    with open(file_path, "w", encoding="utf-8") as f:
        json.dump(data, f)

    client = FoundryChatClient(
        project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
        model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
        credential=AzureCliCredential(),
    )

    # Get the underlying asynchronous OpenAI client
    openai_client = client.project_client.get_openai_client()

    with open(file_path, "rb") as f:
        uploaded_file = await openai_client.files.create(
            file=f,
            purpose="assistants",
        )

    agent = Agent(
        client=client,
        instructions=(
            "You are a data analyst. Use Python and pandas to analyse "
            "uploaded data files. Explain the important findings."
        ),
        tools=[
            FoundryChatClient.get_code_interpreter_tool(
                file_ids=[uploaded_file.id]
            )
        ],
    )

    try:
        result = await agent.run(
            """
Analyse the uploaded JSON file.

Provide:
- descriptive statistics,
- missing-value checks,
- important patterns and anomalies,
- a concise conclusion.

Use Code Interpreter and pandas.
"""
        )

        print(result.text)

    finally:
        await openai_client.files.delete(uploaded_file.id)
        os.remove(file_path)


asyncio.run(main())

cc: @kinfey

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions