How to beginner · 3 min read

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.

bash
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.

python
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=True with different Qwen models like qwen-v1-large for more capacity.
  • Combine with max_tokens to control response length.
  • Use async calls with asyncio for non-blocking requests.
python
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_thinking being 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=True in your chat completion request.
  • Use the official openai Python 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.
Verified 2026-04 · qwen-v1, qwen-v1-large
Verify ↗