An open API service indexing awesome lists of open source software.

https://github.com/lazauk/aifoundry-responsesapi-codeinterpreter

Demo on how to use the Code Interpreter tool in Azure AI Foundry (Azure OpenAI) via Responses API and then extract generated files.
https://github.com/lazauk/aifoundry-responsesapi-codeinterpreter

ai azure code-interpreter foundry openai rest-api tool-calling

Last synced: 24 days ago
JSON representation

Demo on how to use the Code Interpreter tool in Azure AI Foundry (Azure OpenAI) via Responses API and then extract generated files.

Awesome Lists containing this project

README

          

# Azure AI Foundry: Retrieving files of Code Interpreter tool (Responses API)

This repo demonstrates how to use Azure OpenAI's **Responses API** with the Code Interpreter tool. The Code Interpreter enables your selected GPT model to build and test Python code within a secure sandbox container, generating outputs such as data visualisations and analysis results.

Using REST API calls (via the *requests* library), we can retrieve files generated by the Code Interpreter's Python run, including images, charts and other outputs.

> [!NOTE]
> For the latest implementation details of Responses API in Azure, refer to the Azure AI Foundry [documentation page](https://learn.microsoft.com/en-us/azure/ai-foundry/openai/how-to/responses).

## 📑 Table of Contents:
- [Part 1: Configuring Solution Environment](#part-1-configuring-solution-environment)
- [Part 2: Calling Code Interpreter Tool](#part-2-calling-code-interpreter-tool)
- [Part 3: Retrieving Output Files from Container](#part-3-retrieving-output-files-from-container)

## Part 1: Configuring Solution Environment
To use this notebook, you'll need to set up your Azure OpenAI environment and install the required Python packages.

### 1.1 Azure OpenAI Service Setup
Ensure you have an **Azure OpenAI Service** resource with a model deployment that supports the Code Interpreter tool calling (e.g., GPT-4.1).

### 1.2 Authentication
This demo uses **Microsoft Entra ID** authentication via `DefaultAzureCredential` from the `azure.identity` package.

To enable authentication, ensure your environment is properly configured by:
- Logging in via `az login` (Azure CLI), or
- Setting relevant environment variables for service principals, or
- Using managed identity in Azure environment.

Define a token provider using the `get_bearer_token_provider()` function:
``` Python
token_provider = get_bearer_token_provider(
DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default"
)
```

### 1.3 Environment Variables
Configure the following environment variables for your Azure OpenAI deployment:

| Environment Variable | Description |
| :----------------------- | :-------------------------------------------------------------------------------------- |
| AOAI_API_BASE | Your Azure OpenAI endpoint URL (e.g., https://.openai.azure.com). |
| AOAI_DEPLOYMENT | The name of your model deployment (e.g., gpt-4.1). |
| AOAI_API_VERSION | The API version (e.g., 2025-04-01-preview). |

### 1.4 Installation of Required Python Packages
Install the necessary packages:
``` bash
pip install openai azure-identity requests Pillow ipython pathlib
```

## Part 2: Calling Code Interpreter tool

### 2.1 Initialisation of Azure OpenAI client
Initialise the Azure OpenAI with your environment variables and Entra ID token provider:
``` Python
client = AzureOpenAI(
azure_endpoint = AOAI_API_BASE,
azure_ad_token_provider = token_provider,
api_version = AOAI_API_VERSION,
)
```

### 2.2 Responses API with Code Interpreter tool
Include the Code Interpreter as a tool in your Responses API call, along with instructions (system prompt) directing the model to use the appropriate tool:
``` Python
response = client.responses.create(
model = AOAI_DEPLOYMENT,
tools = [
{
"type": "code_interpreter",
"container": {"type": "auto"}
}
],
instructions = "You are a helpful data analyst. You should use Python tool to perform required calculations.",
input = INPUT_TEXT
)
```

## Part 3: Retrieving output files from container

### 3.1 Details of Generated Files
When the Code Interpreter generates output files, their details (Container ID, File ID and filename) can be extracted from the "annotations" section of the Response API output:
``` JSON
{
"container_id": "cntr_689df3cb69648190b73217f54eeb713806df641648828556",
"file_id": "cfile_689df41900fc81909d22ec83e7dafbe1",
"filename": "cfile_689df41900fc81909d22ec83e7dafbe1.png"
}
```
> [!WARNING]
> Details shown above are examples. Your actual container ID, file IDs and file names may be different for each execution.

### 3.2 REST API for Container Files' Content
Retrieve the content of generated files using the REST API endpoint that references the temporary container ID and each file ID:
``` bash
{AOAI_API_BASE}/openai/v1/containers/{container_id}/files/{file_id}/content
```

### 3.3 File Download and Display
After retrieving and saving the file content bytes locally, you can display the outputs in your Jupyter notebook. For example, a data visualisation might look like this:
![Image of Contoso Operating Profit chart](images/contoso_operating_profit.png)