How to enable Qwen extended thinking
Quick answer
Enable
Qwen extended thinking by setting the extended_thinking parameter to true in your chat completion request using the OpenAI Python SDK. Use the gpt-4o or qwen-v1 model with this parameter to activate the feature.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the official openai Python package version 1.0 or higher and set your OPENAI_API_KEY environment variable for authentication.
pip install openai>=1.0 Step by step
Use the OpenAI SDK to create a chat completion request with the extended_thinking parameter enabled. This activates Qwen's extended reasoning capabilities.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.chat.completions.create(
model="qwen-v1",
messages=[{"role": "user", "content": "Explain the theory of relativity in detail."}],
extended_thinking=True
)
print(response.choices[0].message.content) output
The theory of relativity, developed by Albert Einstein, consists of two parts: special relativity and general relativity. Special relativity addresses the physics of objects moving at constant speeds, especially near the speed of light, introducing concepts such as time dilation and length contraction. General relativity extends this to include gravity as the curvature of spacetime caused by mass and energy...
Common variations
- Use
extended_thinking=Truewith different Qwen models likeqwen-v1-largefor more capacity. - Combine with
max_tokensto control response length. - Use async calls with
asynciofor non-blocking requests.
import os
import asyncio
from openai import OpenAI
async def main():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = await client.chat.completions.acreate(
model="qwen-v1-large",
messages=[{"role": "user", "content": "Summarize quantum mechanics."}],
extended_thinking=True,
max_tokens=500
)
print(response.choices[0].message.content)
asyncio.run(main()) output
Quantum mechanics is a fundamental theory in physics describing the behavior of matter and energy at atomic and subatomic scales. It introduces principles such as wave-particle duality, uncertainty, and quantization of energy levels...
Troubleshooting
- If you receive an error about
extended_thinkingbeing unrecognized, verify your SDK version is 1.0 or higher and your model supports this parameter. - Check your API key permissions and usage limits if requests fail.
- Use
print(response)to debug full response details.
Key Takeaways
- Enable Qwen extended thinking by setting
extended_thinking=Truein your chat completion request. - Use the official
openaiPython SDK version 1.0+ with your API key from environment variables. - Combine extended thinking with model selection and token limits for tailored responses.
- Async calls improve performance for concurrent requests.
- Verify SDK and model compatibility if you encounter errors.