microsoft / microsoft/Agent-Framework-Samples

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

Open
#117 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
378
Forks
147
PR merge metrics
No merged PRs in 30d

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with 04.Tools/code_samples/python/msfoundry/02.python-agent-framework-msfoundry-code-interpreter.ipynb and compare its existing Code Interpreter setup with the provided FoundryChatClient example. Verify the Agent Framework API for inline JSON, uploaded JSON or CSV files, file attachments, result retrieval, and cleanup. Done means the notebook demonstrates the requested data-analysis flow, including generated outputs and file deletion.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, jupyter-notebook, pandas, python
Domain
ai, data, documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.