Python SDK
qwen-code-sdk
qwen-code-sdk is an experimental Python SDK for Qwen Code. v1 targets the
existing stream-json CLI protocol and keeps the transport surface small and
testable.
Scope
- Package name:
qwen-code-sdk - Import path:
qwen_code_sdk - Runtime requirement: Python
>=3.10 - CLI dependency: external
qwenexecutable is required in v1 - Transport scope: process transport only
- Not included in v1: ACP transport, SDK-embedded MCP servers
Install
pip install qwen-code-sdkIf qwen is not on PATH, pass path_to_qwen_executable explicitly.
Quick Start
import asyncio
from qwen_code_sdk import is_sdk_result_message, query
async def main() -> None:
result = query(
"Explain the repository structure.",
{
"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())API Surface
Top-level entry points
query(prompt, options=None) -> Queryquery_sync(prompt, options=None) -> SyncQuery
prompt supports either:
strfor single-turn requestsAsyncIterable[SDKUserMessage]for multi-turn streams
Query
- Async iterable over SDK messages
close()interrupt()set_model(model)set_permission_mode(mode)supported_commands()mcp_server_status()get_session_id()is_closed()
QueryOptions
Supported options in v1:
cwdmodelpath_to_qwen_executablepermission_modecan_use_toolenvsystem_promptappend_system_promptdebugmax_session_turnscore_toolsexclude_toolsallowed_toolsauth_typeinclude_partial_messagesresumecontinue_sessionsession_idtimeoutmcp_serversstderr
Session argument priority is fixed as:
resumecontinue_sessionsession_id
Permission Handling
When the CLI emits a can_use_tool control request, the SDK routes it through
can_use_tool(tool_name, tool_input, context).
- Default behavior: deny
- Default timeout: 60 seconds
- Timeout fallback: deny
- Callback exceptions: converted to deny with an error message
- Callback context:
cancel_event,suggestions, andblocked_path - Callback contract:
can_use_toolmust be async with 3 positional arguments;stderrmust accept 1 positional string argument
Error Model
ValidationError: invalid options, invalid UUIDs, unsupported combinationsControlRequestTimeoutError: initialize, interrupt, or other control request timed outProcessExitError: CLI exited non-zeroAbortError: control request or session was cancelled
Troubleshooting
If the SDK cannot start the CLI:
- Verify
qwen --versionworks in the target environment - Pass
path_to_qwen_executableif your shell usesnvm,pyenv, or other non-standard PATH setup - Use
debug=Trueorstderr=printto surface CLI stderr while debugging
If session control calls time out:
- Check that the target
qwenversion supports--input-format stream-json - Increase
timeout.control_request - Verify that no wrapper script is swallowing stdout/stderr
Repository Integration
Repository-level helper commands:
npm run test:sdk:pythonnpm run lint:sdk:pythonnpm run typecheck:sdk:pythonnpm run smoke:sdk:python -- --qwen qwen
Real E2E Smoke
For a real runtime check (actual qwen process + real model call), run from
the repository root. The npm helper uses python3, so ensure it resolves to a
Python >=3.10 interpreter:
npm run smoke:sdk:python -- --qwen qwenThis script runs:
- async single-turn query
- async control flow (
supported_commands, permission mode updates) - sync
query_syncquery
It prints JSON and returns non-zero on failure.