Share via


Use code interpreter in a prompt to generate and execute Python code

Code interpreter in Microsoft Copilot Studio and prompt builder prompts lets makers build AI agents that:

  • Execute Python code for data analysis, processing Word, Excel, PowerPoint, and PDF files, and visualizations
  • Use language models for generative responses

This feature is tailored for developers, business analysts, and low-code makers. Code generation and execution integrate seamlessly with the Copilot Studio ecosystem to create powerful, AI-driven solutions.

Developers should read Code interpreter for developers and review the Code interpreter Power Apps Component Framework (PCF) component sample to understand how they can use the Dataverse Predict message to invoke prompts and process the responses returned.

Requirements

Code interpreter capabilities

Agents using code interpreter can write and run Python code for advanced tasks. Use cases include:

  • Excel file processing: Create, copy, and update Excel worksheets in a workbook, read and apply cell styles, copy formatting across cells, copy and update formulas across cells, and more.
  • Word and PowerPoint file processing.
  • PDF file processing: Create and copy PDF files, read tables and text paragraphs.
  • Dataverse table data processing.
  • Complex mathematical and statistical computations.
  • Data analysis, manipulation, and visualization (for example, charts and graphs).

Code interpreter adds the following capabilities to prompts:

  • Support for files as an output type in prompts.
  • Support for Excel files as input and output types for prompts.

Create prompts in one of two ways:

  • In Power Apps: Use the Build your own prompt option in AI Hub.
  • In Copilot Studio: Add a prompt as a tool at the agent or topic level.

Learn more about prompts in Prompts overview.

You can also use code interpreter in agent chat in Copilot Studio.

Start a new prompt in Power Apps AI Hub and turn on code interpreter

You can start a new prompt in AI Hub in Power Apps and turn on code interpreter in the prompt settings.

  1. Go to Power Apps.

  2. Select AI hub in the left pane.

  3. Select Prompts.

    Screenshot of selecting prompts in AI hub.

  4. Select Build your own prompt.

    Screenshot of selecting build your own prompt in AI hub.

  5. Enter a name for your prompt.

  6. In the Instructions section, select the three dots (), and then select Settings.

    Screenshot of how to open prompt settings.

  7. On Settings, turn on code interpreter.

  8. Close Settings to return to the prompt editor.

The prompt editor is open with code interpreter turned on. Next, write instructions for your prompt.

Start a new prompt as a tool within an agent and turn on code interpreter

You can start a new prompt as a tool within an agent in Copilot Studio and turn on code interpreter for that prompt.

  1. In Copilot Studio, go to the agent where you want to use code interpreter.

  2. Add a new prompt to the agent as a tool, either at the agent level or the topic level.

    • Add a prompt at the agent level:

      1. Go to the Tools page.

      2. Select Add a tool > Prompt.

    • Add a prompt at the topic level:

      1. Go to the Topics page and select the topic where you want to add the prompt.

      2. Select Add node in the position in the topic where you want to add the prompt.

      3. Select Add a tool > New prompt.

    The embedded prompt builder prompt authoring interface opens.

  3. On the information bar, select the three dots (), and then select Settings.

    Screenshot of opening prompt settings.

  4. On the Settings page, select Enable code interpreter.

  5. Close Settings to return to the prompt editor.

The prompt editor is open with code interpreter turned on. Next, write instructions for your prompt.

Write instructions for your prompt

Next, you can write the prompt instructions, configure the prompt as needed, and save to create the prompt.

Learn more about how to use code interpreter in prompts in Code interpreter prompt examples.

Best practices for code interpreter in prompts

  • Use other AI tools like Copilot to get the optimized prompt to start.
  • Use precise prompts with examples (few-shot prompting).
  • Specify outputs (for example, "Return as JSON" or "Excel" or "PDF").

Limitations

  • Analyzing multiple files uploaded in a single prompt isn't supported.
  • Returning multiple file outputs in a single prompt isn't supported.
  • Asking questions about an uploaded file over multiple turns isn't supported.
  • To use code interpreter, you need to configure your agent, in agent settings, to use user authentication. For example, using Direct Line with No authentication selected doesn't work for scenarios using code interpreter.

Troubleshooting

If you encounter errors when using code interpreter, try the following steps:

  1. Ensure all required permissions are granted. Verify that the user account and agent have the necessary permissions to execute code interpreter prompts.
  2. Verify the configuration settings. Confirm that code interpreter is enabled in the prompt settings and that your agent is configured to use user authentication.
  3. Consult error logs for detailed information. Review the error logs in Copilot Studio or the associated Power Platform environment to identify the root cause of the issue and take corrective action.