Running Qwen3 Models with llama.cpp and Python
Qwen3 models are now supported in llama.cpp (from version b5092 onward), but the Python bindings (llama-cpp-python) are currently unreliable or unsupported for Qwen3[2][7]. The recommended approach is to use the native llama.cpp CLI tools or server, and, if you need Python integration, interact with these tools via subprocesses or through the OpenAI-compatible API exposed by llama.cpp’s server mode.
Below are step-by-step instructions and code snippets for both direct CLI usage and Python integration.
1. Prepare llama.cpp and Qwen3 Model
- Update llama.cpp:
Ensure you have the latest version of llama.cpp (at least b5092) compiled locally for best compatibility with Qwen3[2][3][7].
- Download Qwen3 GGUF Model:
Get the Qwen3 model in GGUF format from Hugging Face or another trusted source[2][8].
2. Run Qwen3 with llama.cpp CLI
You can use the llama-cli tool directly:
./llama-cli -m ./qwen3-8b-q8_0.gguf --color --temp 0.6 --top-k 20 --top-p 0.95 -c 40960 -n 32768
Or, to load from Hugging Face:
./llama-cli -hf Qwen/Qwen3-8B-GGUF:Q8_0 --color --temp 0.6 --top-k 20 --top-p 0.95 -c 40960 -n 32768
- Replace the model path as needed.
- Adjust parameters for your use case[2].
3. Run llama.cpp as an OpenAI-Compatible Server
Start the server:
./llama-server -m ./qwen3-8b-q8_0.gguf --chat-format chatml
- The server will be available at
http://localhost:8080/v1/ for API calls[2][1].
4. Python Integration via OpenAI-Compatible API
Since llama-cpp-python is not working reliably with Qwen3, use Python’s requests library to interact with the running llama.cpp server.
Example Python Code:
import requests
API_URL = "http://localhost:8080/v1/chat/completions"
headers = {"Content-Type": "application/json"}
payload = {
"model": "qwen3-8b-q8_0.gguf",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain how to run Qwen3 with llama.cpp."}
],
"max_tokens": 256,
"temperature": 0.7,
}
response = requests.post(API_URL, headers=headers, json=payload)
print(response.json())
- This code sends a chat prompt to the llama.cpp server and prints the response.
- Adjust the payload as needed for your application.
5. Alternative: Python Subprocess (Direct CLI Calls)
If you prefer not to use the server, you can call the CLI tool from Python and capture its output:
import subprocess
cmd = [
"./llama-cli",
"-m", "./qwen3-8b-q8_0.gguf",
"--temp", "0.7",
"--top-k", "20",
"--top-p", "0.95",
"-c", "40960",
"-n", "256",
"--prompt", "Explain how to run Qwen3 with llama.cpp."
]
result = subprocess.run(cmd, capture_output=True, text=True)
print(result.stdout)
- This approach is robust and works even if Python bindings are broken[6].
Troubleshooting Tips
- Model Loading Errors:
Ensure you have the latest llama.cpp and use official or Unsloth-fixed GGUF files[7].
- Server Not Responding:
Check that the server is running and accessible at the correct port.
- Prompt Formatting:
Use --chat-format chatml for Qwen3 models, as they expect ChatML-style prompts[2].
Summary Table: Approaches
| Method | Qwen3 Support | Python Integration | Notes |
|---|
| llama-cpp-python | ❌ | ✅ | Not working for Qwen3 currently |
| llama.cpp CLI | ✅ | Via subprocess | Reliable, direct execution |
| llama.cpp Server (API) | ✅ | Via HTTP requests | OpenAI-compatible, easy Python use |
In summary:
To run Qwen3 with llama.cpp and Python, use the latest llama.cpp CLI or server. For Python, interact with the server’s OpenAI-compatible API using requests, or call the CLI via subprocess. Avoid llama-cpp-python for Qwen3 until official support is restored[2][7][1].