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:
- Pass a small JSON object directly in the prompt.
- Upload a larger
.json or .csv file.
- Attach the uploaded file to the Code Interpreter tool.
- Ask the agent to analyse the data using Python or pandas.
- Retrieve the analysis results and any generated files or charts.
- 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
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.ipynbHowever, 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:
.jsonor.csvfile.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:cc: @kinfey