How to download files generated by the code interpreter tool with Semantic Kernel?

takeolexus 200 Reputation points
2025-06-30T15:53:32.46+00:00

Based on the following learning content, I have created an AI agent for the code interpreter tool.

https://free.blessedness.top/en-us/azure/ai-foundry/agents/how-to/tools/code-interpreter-samples?pivots=csharp

PersistentAgentsClient client = AzureAIAgent.CreateAgentsClient("<your endpoint>", new AzureCliCredential());
PersistentAgent definition = await agentsClient.CreateAgentAsync(
    "<name of the the model used by the agent>",
    name: "<agent name>",
    description: "<agent description>",
    instructions: "You politely help with math questions. Use the code interpreter tool when asked to visualize numbers.",
    tools: [new CodeInterpreterToolDefinition()],
    toolResources:
        new()
        {
            CodeInterpreter = new()
            {
                FileIds = { ... },
            }
        }));
AzureAIAgent agent = new(definition, agentsClient);

ChatMessageContent message = new(AuthorRole.User, "Hi, Agent! Draw a graph for a line with a slope of 4 and y-intercept of 9.");
await foreach (StreamingChatMessageContent response in agent.InvokeStreamingAsync(message, agentThread))
{
    Console.Write(response.Content);
}

Python code and intermediate assistant messages can be obtained, but the image files generated by the Python code cannot be retrieved. If I don't use SemanticKernel , I know I have the following implementation options.

https://free.blessedness.top/en-us/azure/ai-foundry/agents/how-to/tools/code-interpreter-samples?pivots=csharp#process-the-results-and-handle-files

Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Amira Bedhiafi 39,106 Reputation points Volunteer Moderator
    2025-07-01T20:05:09.8133333+00:00

    Hello !

    Thank you for posting on Microsoft Learn.

    With Semantic Kernel you get every file that the code interpreter produces as a file-reference item in the assistant reply. All you have to do is :

    • Collect the file-ids (they arrive as StreamingFileReferenceContent while you stream, or FileReferenceContent when you ask for the full message).
    • Use the underlying OpenAIFileClient (or the file_client you created) to download the bytes.
    using Azure.AI.OpenAI;
    
    using Microsoft.SemanticKernel.Agents.OpenAI;
    
    using Microsoft.SemanticKernel.Contents;   
    
    using Microsoft.SemanticKernel;
    
    using OpenAI.Files;
    
    using System.Linq;
    
    OpenAIFileClient fileClient = client.GetOpenAIFileClient();
    
    List<string> fileIds = new();
    
    await foreach (StreamingChatMessageContent chunk
    
                   in agent.InvokeStreamingAsync(userMessage, thread))
    
    {
    
        Console.Write(chunk.Content);                     
    
        fileIds.AddRange(chunk.Items
    
                         .OfType<StreamingFileReferenceContent>() 
    
                         .Select(f => f.FileId));
    
    }
    
    foreach (string id in fileIds)
    
    {
    
        OpenAIFile info = fileClient.GetFile(id);                       
    
        BinaryData data = await fileClient.DownloadFileAsync(id);      
    
        string localName = Path.Combine("downloads",
    
                            Path.GetFileName(info.Filename ?? $"{id}.bin"));
    
        await File.WriteAllBytesAsync(localName, data.ToArray());
    
        Console.WriteLine($"saved → {localName}");
    
    }
    
    

    Another alternative you can have (non-streaming) :

    ChatMessageContent reply = await agent.InvokeAsync(userMessage, thread);
    
    foreach (var file in reply.Items.OfType<FileReferenceContent>())
    
    {
    
        var bytes = await fileClient.DownloadFileAsync(file.FileId);
    
        File.WriteAllBytes($"out/{file.FileId}.png", bytes.ToArray());
    
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.