Skip to main content

Python SDK for programmatic access to qwen-code CLI

Project description

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.

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_code_sdk-0.1.0rc0.tar.gz (30.5 kB view details)

Uploaded Source

Built Distribution

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

qwen_code_sdk-0.1.0rc0-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file qwen_code_sdk-0.1.0rc0.tar.gz.

File metadata

  • Download URL: qwen_code_sdk-0.1.0rc0.tar.gz
  • Upload date:
  • Size: 30.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for qwen_code_sdk-0.1.0rc0.tar.gz
Algorithm Hash digest
SHA256 44778d6a24acf14372de93caec427c0fd6187d772a3893a335bac4e55450b03d
MD5 3953ab02fd9b4be7e8a8b1c20ec7b2d6
BLAKE2b-256 6ca675b21be3972737f8c653ee83ed88824454abd293d432df793e37ce79943f

See more details on using hashes here.

Provenance

The following attestation bundles were made for qwen_code_sdk-0.1.0rc0.tar.gz:

Publisher: release-sdk-python.yml on QwenLM/qwen-code

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qwen_code_sdk-0.1.0rc0-py3-none-any.whl.

File metadata

File hashes

Hashes for qwen_code_sdk-0.1.0rc0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd31047815168a303f24ca44cc49b93f5ab4c5a731ee3fda33a2c691966804d5
MD5 f94cbede30959d82b94caa75aacbba64
BLAKE2b-256 c9d607c374a10453bcf9f0c79cbe3fcd78af791087025aa85c9db5e2ab86947f

See more details on using hashes here.

Provenance

The following attestation bundles were made for qwen_code_sdk-0.1.0rc0-py3-none-any.whl:

Publisher: release-sdk-python.yml on QwenLM/qwen-code

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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