Skip to main content

Unofficial Qwen API Client & Chat Wrapper

Project description

qwen-chat 🚀

PyPI version License: MIT Python Version

An unofficial, feature-rich Python SDK and client wrapper for the Qwen AI Web API. Enjoy seamless access to advanced models like qwen3.6-plus with support for search, deep reasoning, streaming, and automatic local image uploads.


✨ Features

  • 🧠 Complete Model Support
    Interact with Qwen's latest web models including qwen3.6-plus, qwen-max-latest, qwq-32b, qwen2.5-omni-7b, and specialized vision/coder versions.

  • ⚡ Synchronous & Asynchronous Clients
    Whether you're building a script or a highly concurrent async web server, qwen-chat has you covered with native create() and acreate() workflows.

  • 📸 Automatic Local Image Uploads (New!)
    Pass a local file path or raw bytes to an ImageBlock. The client automatically fetches STS tokens and uploads the image to Alibaba Cloud OSS under the hood—no boilerplate manual upload code required!

  • 🌊 Real-time SSE Streaming
    Stream token-by-token responses directly to your UI or console, with fully structured chunks and search info outputs.

  • 🔍 Integrated Web Search
    Toggle real-time search on or off per message to query live web data and receive detailed citations alongside responses.

  • 💡 Thinking & Reasoning Controls
    Adjust thinking budget settings to enable advanced reasoning capabilities on complex tasks.


📦 Installation

Install qwen-chat via pip:

pip install qwen-chat

⚙️ Environment Setup

To authenticate requests, extract your Authorization bearer token and Cookie from the Qwen Web UI:

  1. Go to https://chat.qwen.ai and log in.
  2. Open developer tools (F12 or Ctrl+Shift+I / Cmd+Option+I) and navigate to the Network tab.
  3. Send any message in the chat interface.
  4. Locate the completions request (filter by Fetch/XHR).
  5. In the request headers:
    • Copy the value of the Authorization header without the word "Bearer " (just copy the token starting with eyJ...).
    • Copy the entire value of the Cookie header.
  6. Save these values in a .env file in the root of your project:
QWEN_AUTH_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
QWEN_COOKIE="cna=...; cnaui=...; token=..."

🚀 Quick Start Examples

1. Basic Text Completion (Sync & Async)

Sync:

from qwen_chat import Qwen
from qwen_chat.core.types.chat import ChatMessage

# Initializes automatically using environment variables
client = Qwen()

messages = [
    ChatMessage(
        role="user",
        content="Tell me a joke about programming!"
    )
]

response = client.chat.create(
    messages=messages,
    model="qwen3.6-plus"
)
print("🤖 AI Response:", response.choices.message.content)

Async:

import asyncio
from qwen_chat import Qwen
from qwen_chat.core.types.chat import ChatMessage

async def main():
    client = Qwen()
    messages = [ChatMessage(role="user", content="Explain quantum computing in one sentence.")]
    
    response = await client.chat.acreate(
        messages=messages,
        model="qwen3.6-plus"
    )
    print("🤖 AI Response:", response.choices.message.content)

asyncio.run(main())

2. SSE Streaming with Search Citations

from qwen_chat import Qwen
from qwen_chat.core.types.chat import ChatMessage

client = Qwen()

messages = [
    ChatMessage(
        role="user",
        content="What is the latest news about Space Devs?",
        web_search=True  # Enables real-time web search citations
    )
]

stream = client.chat.create(
    messages=messages,
    model="qwen3.6-plus",
    stream=True
)

for chunk in stream:
    delta = chunk.choices[0].delta
    # Extract search citation results if present
    if delta.extra and delta.extra.web_search_info:
        print("\n🔍 Web Search Sources:")
        for source in delta.extra.web_search_info:
            print(f"- {source.title}: {source.url}")
        print("\n🤖 Assistant Response:")
        
    print(delta.content, end="", flush=True)
print()

3. Automatic Local Image Upload (Vision)

Just pass your local image path directly inside ImageBlock. The client automatically handles the secure upload sequence:

from qwen_chat import Qwen
from qwen_chat.core.types.chat import ChatMessage, TextBlock, ImageBlock

client = Qwen()

messages = [
    ChatMessage(
        role="user",
        blocks=[
            TextBlock(text="Extract all the text inside this image:"),
            ImageBlock(path="receipt.jpg")  # Automatically uploaded to Alibaba OSS!
        ]
    )
]

response = client.chat.create(
    messages=messages,
    model="qwen3.6-plus"
)
print("🤖 AI Response:", response.choices.message.content)

🙋‍♂️ Contributing

Contributions are welcome! Please feel free to open Issues or submit Pull Requests to help improve the library.

📃 License

This project is licensed under the MIT License.


📞 Contact & Support

For queries, support, or custom integrations, feel free to contact the author:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qwen_chat-1.0.0.tar.gz (31.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qwen_chat-1.0.0-py3-none-any.whl (33.6 kB view details)

Uploaded Python 3

File details

Details for the file qwen_chat-1.0.0.tar.gz.

File metadata

  • Download URL: qwen_chat-1.0.0.tar.gz
  • Upload date:
  • Size: 31.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for qwen_chat-1.0.0.tar.gz
Algorithm Hash digest
SHA256 720314766aca2f25349fb774f307d9b2e1c11bcf604186d8faf4af4620692ef1
MD5 df155258134db0d078bcb7e425cd1afe
BLAKE2b-256 ecb89c3149b0fdc70ace6283448672684e7340861898c94305ab104d79a0f6be

See more details on using hashes here.

File details

Details for the file qwen_chat-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: qwen_chat-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 33.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for qwen_chat-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cacb56820991ae786272ae6d9adb87fa16b2be2daf5497076f2849bbc4955bf9
MD5 fbc9e8cd803eaf7b9c7dd001bba75fa1
BLAKE2b-256 792de033c36d11de9854267e850a3f31fa4af6cdd31ad038374dfab243cbd84d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page