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 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. Click on the request and go to the Headers tab. Copy the value of the Authorization header without the word "Bearer " (just copy the token starting with eyJ...).
  6. Save this value in a .env file in the root of your project:
QWEN_AUTH_TOKEN=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

🚀 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.1.tar.gz (31.2 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.1-py3-none-any.whl (33.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: qwen_chat-1.0.1.tar.gz
  • Upload date:
  • Size: 31.2 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.1.tar.gz
Algorithm Hash digest
SHA256 494612582f8655dcdd031efd93b5ede7ba4b87ba2c33ab3ac86db5d0e332fa58
MD5 7251256c69f085240fad9997eeb47b5b
BLAKE2b-256 6f703c8272d72c48bc6fb0222a9b2bb7f788053477d5294db9e73a6adb84fa72

See more details on using hashes here.

File details

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

File metadata

  • Download URL: qwen_chat-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f983c8e3ebbdd0b98f06ac4009ce698e2d756f8430e09e6b797990656a86fd3b
MD5 db66579f6565bb370f77d200b543b849
BLAKE2b-256 2e4b1c116a1e307903f03421aa599e7e7f4af18288a55792e104fb4f0b3ba4cb

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