Sometimes an automated process needs more than just text output. In my case, a Microsoft Foundry Agent generated a summary in Markdown, but I needed a real Word document for further processing. I solved this with Power Automate, SharePoint, and Foundry Code Interpreter. Here’s how I built the flow step by step.
The scenario
The flow reads the generated Markdown file from SharePoint, sends the content to the Foundry Agent to create a Word document using Code Interpreter, downloads the generated DOCX, and saves it back to SharePoint.
The Foundry Agent
The Foundry Agent is straightforward to create. In this example, I’m using GPT-5-chat as the model (feel free to use a model of your choice, but adapt the parameters) and have enabled Code Interpreter, which allows the agent to generate the Word document.
The flow
The flow uses Power Automate to turn a Markdown document into a Microsoft Word document with a Microsoft Foundry Agent using Code Interpreter.
SharePoint → Power Automate → Foundry Agent → DOCX → Agent Container File → SharePoint
The interesting part is getting the generated DOCX file from the Foundry Code Interpreter container back into Power Automate and saving it to SharePoint.
My flow roughly looks like this:
- Read a Markdown text file from SharePoint.
- Send the Markdown content to my Foundry Agent.
- The agent uses Code Interpreter to create a DOCX file.
- Extract the generated
container_id,file_id, andfilenamefrom the response. - Download the binary DOCX from the Foundry container.
- Create the Word document in SharePoint.
The Foundry Agent gets instructions to create a Word document from the supplied content and return the resulting file. You can download the full flow as solution file from my GitHub repository here.
Calling the Foundry Agent from Power Automate
I use the HTTP action in Power Automate to call the Responses endpoint of my Foundry Agent with the API key (good enough for our demo, I´m in Power Automate, not in Azure). You get the agent endpoint in your Foundry agent details.
The request including the endpoint looks like this:
{
"uri": "https://[your-foundry-project]-resource.services.ai.azure.com/api/projects/[your-foundry-project]/agents/[your-foundry-agent]/endpoint/protocols/openai/responses?api-version=v1",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"api-key": "[your-api-key]"
},
"body": {
"model": "gpt-5-chat",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "Create a Microsoft Word document from the text below. Name the file Compared-63.docx, preserve the structure of the supplied content, and return the generated DOCX as a downloadable file.\n\n[your-text-file-content.....]"
}
]
}
]
}
}
The important detail here is api-version=v1. Without it, the agent endpoint returns HTTP 400, BadRequest, Missing required query parameter: api-version. The agent invocation route requires the API version so the Foundry gateway knows which API contract to apply.
Processing the output
- HTTP: Calling the REST API of your agent with the payload.
- ComposeOutput: stores the output to get only the body of the agent request
@{body('HTTP')?['output']}, useful to simplify and for debugging. - FilterArray: ComposeOutput includes Key
contentwhich is an arry[]. Therefore, it´s wise to extract the relevant data to get only the first item. This happens here in this action, I filter for an item wheretype = message.
{
"type": "Query",
"inputs": {
"from": "@outputs('ComposeOutput')",
"where": "@equals(item()?['type'],'message')"
},
"runAfter": {
"ComposeOutput": [
"Succeeded"
]
}
}
- ScopeCheckResult: This scope extracts the relevant data from the FilterArray action. We need this data for processing. The agent uses Code Interpreter to create the Word document. The response therefore contains a
content/annotations/container_file_citation, for example:
{
"type": "container_file_citation",
"container_id": "cntr_...",
"file_id": "cfile_...",
"filename": "Compared-63.docx"
}
So I extract these three values in Power Automate: container_id, file_id, filename.
ComposeFileId: first(body('FilterArray'))?['content']?[0]?['annotations']?[0]?['file_id']
ComposeContainerId: first(body('FilterArray'))?['content']?[0]?['annotations']?[0]?['container_id']
ComposeFileName: first(body('FilterArray'))?['content']?[0]?['annotations']?[0]?['filename']
Note: The output structure can vary depending on the model and input data, so this is not a production-ready approach. For this demo, however, it is sufficient to show how to extract the required values.
Getting the generated file information
Creating the Word document is only half of the job. At this point, the DOCX still lives inside the Code Interpreter container. In ScopeSaveFile we read the generated file from the agent files store and write it to a new file in the SharePoint site.
- HTTPGetFile: To get the file, I can construct our URL for the Power Automate HTTP
GETrequest like this:
{
"type": "Http",
"inputs": {
"uri": "@concat('https://[your-foundry-project]-resource.services.ai.azure.com/api/projects/[your-foundry-project]/openai/v1/containers/',outputs('ComposeContainerId'),'/files/',outputs('ComposeFileId'),'/content')",
"method": "GET",
"headers": {
"Content-Type": "application/json",
"api-key": "@{variables('key')}"
}
}
}
This results in a request like:
{
"uri": "https://[your-foundry-project]-resource.services.ai.azure.com/api/projects/[your-foundry-project]/openai/v1/containers/cntr_6b1083a1036c81909423da162b792d9b04890726209475bb/files/cfile_6b1083a7483481908a1ee9844c80ad96/content",
"method": "GET",
"headers": {
"Content-Type": "application/json",
"api-key": "[your-api-key]"
}
}
Foundry returns the file as application/octet-stream.
Power Automate represents the result similar to this:
{
"$content-type": "application/octet-stream",
"$content": "UEsDB..."
}
The long UEsDB... value is the Base64-encoded DOCX file.
Saving the Word document to SharePoint
Finally, I use SharePoint → Create file.
The filename comes directly from the Foundry response: outputs(‘ComposeFilename’).
For File Content, I convert the Base64 content back to binary:
base64ToBinary(
body('HTTP_GetFile')?['$content']
)
SharePoint now receives the actual binary DOCX file and creates the file Compared-63.docx in the selected document library.
The document can be opened normally in Microsoft Word, with the formatting preserved.
The complete pattern
SharePoint Markdown file
↓
Power Automate
↓
Read Markdown content
↓
POST Prompt to generate Word file to Microsoft Foundry Agent
↓
Code Interpreter creates DOCX
↓
container_id + file_id + filename
↓
GET /openai/v1/containers/{container_id}/files/{file_id}/content
↓
Base64 → Binary
↓
SharePoint Create file
↓
Compared-63.docx
I like this pattern because Power Automate only orchestrates the process. The Foundry Agent takes care of understanding the content and creating the document, while SharePoint remains the place where the source and generated files are stored.
It also opens up more scenarios than just Markdown-to-Word conversion. The same approach can be used whenever a Foundry Agent creates a file with Code Interpreter and that file needs to continue through another process.
I hope this article helps you get started with Code Interpreter and file handling in your Foundry agents!
Happy developing!



