{"id":30874140,"url":"https://github.com/lazauk/aifoundry-responsesapi-codeinterpreter","last_synced_at":"2026-05-08T09:32:47.306Z","repository":{"id":309962510,"uuid":"1034130997","full_name":"LazaUK/AIFoundry-ResponsesAPI-CodeInterpreter","owner":"LazaUK","description":"Demo on how to use the Code Interpreter tool in Azure AI Foundry (Azure OpenAI) via Responses API and then extract generated files.","archived":false,"fork":false,"pushed_at":"2025-08-14T19:48:04.000Z","size":143,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-08T00:29:13.525Z","etag":null,"topics":["ai","azure","code-interpreter","foundry","openai","rest-api","tool-calling"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LazaUK.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-08-07T22:37:55.000Z","updated_at":"2025-08-14T19:48:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"97cdf10e-a934-4098-bf8e-894345b32abf","html_url":"https://github.com/LazaUK/AIFoundry-ResponsesAPI-CodeInterpreter","commit_stats":null,"previous_names":["lazauk/aifoundry-responsesapi-codeinterpreter"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/LazaUK/AIFoundry-ResponsesAPI-CodeInterpreter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LazaUK%2FAIFoundry-ResponsesAPI-CodeInterpreter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LazaUK%2FAIFoundry-ResponsesAPI-CodeInterpreter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LazaUK%2FAIFoundry-ResponsesAPI-CodeInterpreter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LazaUK%2FAIFoundry-ResponsesAPI-CodeInterpreter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LazaUK","download_url":"https://codeload.github.com/LazaUK/AIFoundry-ResponsesAPI-CodeInterpreter/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LazaUK%2FAIFoundry-ResponsesAPI-CodeInterpreter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32774814,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-08T08:22:46.396Z","status":"ssl_error","status_checked_at":"2026-05-08T08:22:45.650Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","azure","code-interpreter","foundry","openai","rest-api","tool-calling"],"created_at":"2025-09-08T00:20:04.383Z","updated_at":"2026-05-08T09:32:47.297Z","avatar_url":"https://github.com/LazaUK.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Azure AI Foundry: Retrieving files of Code Interpreter tool (Responses API)\n\nThis 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.\n\nUsing 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.\n\n\u003e [!NOTE]\n\u003e 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).\n\n## 📑 Table of Contents:\n- [Part 1: Configuring Solution Environment](#part-1-configuring-solution-environment)\n- [Part 2: Calling Code Interpreter Tool](#part-2-calling-code-interpreter-tool)\n- [Part 3: Retrieving Output Files from Container](#part-3-retrieving-output-files-from-container)\n\n## Part 1: Configuring Solution Environment\nTo use this notebook, you'll need to set up your Azure OpenAI environment and install the required Python packages.\n\n### 1.1 Azure OpenAI Service Setup\nEnsure you have an **Azure OpenAI Service** resource with a model deployment that supports the Code Interpreter tool calling (e.g., GPT-4.1).\n\n### 1.2 Authentication\nThis demo uses **Microsoft Entra ID** authentication via `DefaultAzureCredential` from the `azure.identity` package.\n\nTo enable authentication, ensure your environment is properly configured by:\n- Logging in via `az login` (Azure CLI), or\n- Setting relevant environment variables for service principals, or\n- Using managed identity in Azure environment.\n\nDefine a token provider using the `get_bearer_token_provider()` function:\n``` Python\ntoken_provider = get_bearer_token_provider(\n    DefaultAzureCredential(),\n    \"https://cognitiveservices.azure.com/.default\"\n)\n```\n\n### 1.3 Environment Variables\nConfigure the following environment variables for your Azure OpenAI deployment:\n\n| Environment Variable     | Description                                                                             |\n| :----------------------- | :-------------------------------------------------------------------------------------- |\n| AOAI_API_BASE            | Your Azure OpenAI endpoint URL (e.g., https://\u003cYOUR_AOAI_RESOURCE\u003e.openai.azure.com).   |\n| AOAI_DEPLOYMENT          | The name of your model deployment (e.g., gpt-4.1).                                      |\n| AOAI_API_VERSION         | The API version (e.g., 2025-04-01-preview).                                             |\n\n### 1.4 Installation of Required Python Packages\nInstall the necessary packages:\n``` bash\npip install openai azure-identity requests Pillow ipython pathlib\n```\n\n## Part 2: Calling Code Interpreter tool\n\n### 2.1 Initialisation of Azure OpenAI client\nInitialise the Azure OpenAI with your environment variables and Entra ID token provider:\n``` Python\nclient = AzureOpenAI(\n    azure_endpoint = AOAI_API_BASE,\n    azure_ad_token_provider = token_provider,\n    api_version = AOAI_API_VERSION,\n)\n```\n\n### 2.2 Responses API with Code Interpreter tool\nInclude the Code Interpreter as a tool in your Responses API call, along with instructions (system prompt) directing the model to use the appropriate tool:\n``` Python\nresponse = client.responses.create(\n    model = AOAI_DEPLOYMENT,\n    tools = [\n        {\n            \"type\": \"code_interpreter\",\n            \"container\": {\"type\": \"auto\"}\n        }\n    ],\n    instructions = \"You are a helpful data analyst. You should use Python tool to perform required calculations.\",\n    input = INPUT_TEXT\n)\n```\n\n## Part 3: Retrieving output files from container\n\n### 3.1 Details of Generated Files\nWhen 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:\n``` JSON\n{\n    \"container_id\": \"cntr_689df3cb69648190b73217f54eeb713806df641648828556\",\n    \"file_id\": \"cfile_689df41900fc81909d22ec83e7dafbe1\",\n    \"filename\": \"cfile_689df41900fc81909d22ec83e7dafbe1.png\"\n}\n```\n\u003e [!WARNING]\n\u003e Details shown above are examples. Your actual container ID, file IDs and file names may be different for each execution.\n\n### 3.2 REST API for Container Files' Content\nRetrieve the content of generated files using the REST API endpoint that references the temporary container ID and each file ID:\n``` bash\n{AOAI_API_BASE}/openai/v1/containers/{container_id}/files/{file_id}/content\n```\n\n### 3.3 File Download and Display\nAfter 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:\n![Image of Contoso Operating Profit chart](images/contoso_operating_profit.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flazauk%2Faifoundry-responsesapi-codeinterpreter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flazauk%2Faifoundry-responsesapi-codeinterpreter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flazauk%2Faifoundry-responsesapi-codeinterpreter/lists"}