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:
Responses API
These models also support Code Interpreter but may not perform as well.
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
Recommended models
Responses API
- Qwen-Max: qwen3.8-max, Qwen3.7-Max series
- Qwen-Plus: Qwen3.7-Plus series, Qwen3.6-Plus series, Qwen3.5-Plus series
- DeepSeek: deepseek-v4-pro-0813, deepseek-v4-flash, deepseek-v4-flash-0731, deepseek-v4-pro
- GLM: glm-5.2
- Qwen3.8 open-source series
- Qwen-Max (thinking mode): Qwen3-Max series (requires thinking mode)
- Qwen-Plus: Qwen3.5-Plus series
Other models
These models also support Code Interpreter but may not perform as well.
- Qwen-Flash: Qwen3.7-Flash series, Qwen3.6-Flash series, Qwen3.5-Flash series
- Qwen3.8 open-source series (
qwen3.8-2.4t-a95b) - Qwen3.6 open-source series (excluding qwen3.6-27b)
- Qwen3.5 open-source series
- DeepSeek:
deepseek-v4-pro-0813,deepseek-v4-flash,deepseek-v4-flash-0731,deepseek-v4-pro - GLM:
glm-5.2
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
-
Streaming required (Chat Completions and DashScope): The code interpreter only works with streaming (
stream: true). Non-streaming requests return an error. The Responses API does not have this limitation. -
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.