Run Python in a sandbox
The code interpreter lets the model write and execute Python code in a secure sandbox during a conversation. When the model encounters a task that benefits from computation — such as math, data analysis, or chart generation — it automatically generates code, runs it, and incorporates the result into its response.
The following examples show a complete code interpreter request with output.
Response example
Once enabled, the model processes each request in stages:
The DashScope API returns the code that the interpreter executed in the
When the model calls the interpreter multiple times in a single request,
The code interpreter is temporarily free, but it increases token consumption.
Getting started
The following examples show a complete code interpreter request with output.
- Responses API
- Chat Completions
- DashScope
How it works
Once enabled, the model processes each request in stages:
1
Thinking
The model analyzes the request and determines how to solve the problem.
2
Code execution
The model generates and executes Python code in a sandbox.
3
Result integration
The model processes the execution results and determines next steps.
4
Response
The model generates a natural-language response.
Steps 2 and 3 may repeat multiple times within a single request.
Supported models
| Family | Models | Notes |
|---|---|---|
| Qwen-Max | qwen3-max, qwen3-max-2026-01-23 | Requires thinking mode |
| Qwen-Plus | qwen3.6-plus, qwen3.6-plus-2026-04-02, qwen3.5-plus, qwen3.5-plus-2026-02-15 | |
| Qwen-Flash | qwen3.5-flash, qwen3.5-flash-2026-02-23 | |
| Open source | qwen3.5-397b-a17b, qwen3.5-122b-a10b, qwen3.5-27b, qwen3.5-35b-a3b |
Retrieve executed code
The DashScope API returns the code that the interpreter executed in the tool_info field of each streaming chunk. Each entry contains the code string and a type of code_interpreter:
tool_info accumulates all calls. The total call count is available in usage.plugins:
The Chat Completions API does not return the executed code. Use the Responses API or DashScope API if you need to inspect the generated code.
Limitations
-
The code interpreter and function calling are mutually exclusive and cannot be enabled at the same time.
Enabling both results in an error.
-
When the code interpreter is enabled, a single request triggers multiple model inferences. The
usagefield aggregates token consumption across all inferences.