Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

qwen-code-sdk

Experimental Python SDK for programmatic access to Qwen Code through the stream-json protocol.

Installation

pip install qwen-code-sdk

Requirements

  • Python >=3.10
  • External qwen CLI installed and available in PATH

You can also point the SDK at an explicit CLI binary or script with path_to_qwen_executable.

Quick Start

import asyncio

from qwen_code_sdk import is_sdk_result_message, query


async def main() -> None:
    result = query(
        "List the top-level packages in this repository.",
        {
            "cwd": "/path/to/project",
            "path_to_qwen_executable": "qwen",
        },
    )

    async for message in result:
        if is_sdk_result_message(message):
            print(message["result"])


asyncio.run(main())

Sync API

from qwen_code_sdk import query_sync


with query_sync(
    "Say hello",
    {
        "path_to_qwen_executable": "qwen",
    },
) as result:
    for message in result:
        print(message)

Main APIs

  • query(prompt, options=None) -> Query
  • query_sync(prompt, options=None) -> SyncQuery
  • Query.close(), interrupt(), set_model(), set_permission_mode()
  • Query.supported_commands(), mcp_server_status(), get_session_id()

prompt accepts either a single str or an AsyncIterable[SDKUserMessage] for multi-turn sessions.

Permission Callback

from qwen_code_sdk import query


async def can_use_tool(tool_name, tool_input, context):
    if tool_name == "write_file":
        return {"behavior": "deny", "message": "Writes disabled in this app"}
    return {"behavior": "allow", "updatedInput": tool_input}


result = query(
    "Create hello.txt",
    {
        "path_to_qwen_executable": "qwen",
        "can_use_tool": can_use_tool,
    },
)

The callback defaults to deny. If it does not return within 60 seconds, the SDK auto-denies the tool request.

The context argument includes cancel_event, suggestions, and blocked_path when the CLI provides a path-specific permission target. can_use_tool must be an async def callback accepting (tool_name, tool_input, context). stderr must accept a single str.

Errors

  • ValidationError: invalid query options or malformed session identifiers
  • ControlRequestTimeoutError: CLI control operation exceeded timeout
  • ProcessExitError: qwen exited with a non-zero code
  • AbortError: query or control request was cancelled

Current Scope

0.1.x is intentionally narrow:

  • Uses external qwen CLI via process transport
  • Targets stream-json parity with the TypeScript SDK core flow
  • Does not yet implement ACP transport
  • Does not yet embed MCP servers inside the SDK process

See developer documentation for more detail.

Metadata

Release files for qwen-code-sdk 0.1.0rc0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for qwen-code-sdk 0.1.0rc0
File Size Uploaded
qwen_code_sdk-0.1.0rc0.tar.gz 30.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for qwen-code-sdk 0.1.0rc0
File Interpreter ABI Platform
qwen_code_sdk-0.1.0rc0-py3-none-any.whl Python 3 none any Details

Total release size: 48.3 kB

Release files / qwen_code_sdk-0.1.0rc0.tar.gz

Download URL qwen_code_sdk-0.1.0rc0.tar.gz
Size 30.5 kB
Tags Source
SHA-256 checksum
How to use checksums
44778d6a24acf14372de93caec427c0fd6187d772a3893a335bac4e55450b03d
BLAKE2b-256 checksum
How to use checksums
6ca675b21be3972737f8c653ee83ed88824454abd293d432df793e37ce79943f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 9, 2026.

Transparency log

Release files / qwen_code_sdk-0.1.0rc0-py3-none-any.whl

Download URL qwen_code_sdk-0.1.0rc0-py3-none-any.whl
Size 17.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cd31047815168a303f24ca44cc49b93f5ab4c5a731ee3fda33a2c691966804d5
BLAKE2b-256 checksum
How to use checksums
c9d607c374a10453bcf9f0c79cbe3fcd78af791087025aa85c9db5e2ab86947f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 9, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0rc0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page