|
| 1 | +# Python SDK |
| 2 | + |
| 3 | +## `qwen-code-sdk` |
| 4 | + |
| 5 | +`qwen-code-sdk` is an experimental Python SDK for Qwen Code. v1 targets the |
| 6 | +existing `stream-json` CLI protocol and keeps the transport surface small and |
| 7 | +testable. |
| 8 | + |
| 9 | +## Scope |
| 10 | + |
| 11 | +- Package name: `qwen-code-sdk` |
| 12 | +- Import path: `qwen_code_sdk` |
| 13 | +- Runtime requirement: Python `>=3.10` |
| 14 | +- CLI dependency: external `qwen` executable is required in v1 |
| 15 | +- Transport scope: process transport only |
| 16 | +- Not included in v1: ACP transport, SDK-embedded MCP servers |
| 17 | + |
| 18 | +## Install |
| 19 | + |
| 20 | +```bash |
| 21 | +pip install qwen-code-sdk |
| 22 | +``` |
| 23 | + |
| 24 | +If `qwen` is not on `PATH`, pass `path_to_qwen_executable` explicitly. |
| 25 | + |
| 26 | +## Quick Start |
| 27 | + |
| 28 | +```python |
| 29 | +import asyncio |
| 30 | + |
| 31 | +from qwen_code_sdk import is_sdk_result_message, query |
| 32 | + |
| 33 | + |
| 34 | +async def main() -> None: |
| 35 | + result = query( |
| 36 | + "Explain the repository structure.", |
| 37 | + { |
| 38 | + "cwd": "/path/to/project", |
| 39 | + "path_to_qwen_executable": "qwen", |
| 40 | + }, |
| 41 | + ) |
| 42 | + |
| 43 | + async for message in result: |
| 44 | + if is_sdk_result_message(message): |
| 45 | + print(message["result"]) |
| 46 | + |
| 47 | + |
| 48 | +asyncio.run(main()) |
| 49 | +``` |
| 50 | + |
| 51 | +## API Surface |
| 52 | + |
| 53 | +### Top-level entry points |
| 54 | + |
| 55 | +- `query(prompt, options=None) -> Query` |
| 56 | +- `query_sync(prompt, options=None) -> SyncQuery` |
| 57 | + |
| 58 | +`prompt` supports either: |
| 59 | + |
| 60 | +- `str` for single-turn requests |
| 61 | +- `AsyncIterable[SDKUserMessage]` for multi-turn streams |
| 62 | + |
| 63 | +### `Query` |
| 64 | + |
| 65 | +- Async iterable over SDK messages |
| 66 | +- `close()` |
| 67 | +- `interrupt()` |
| 68 | +- `set_model(model)` |
| 69 | +- `set_permission_mode(mode)` |
| 70 | +- `supported_commands()` |
| 71 | +- `mcp_server_status()` |
| 72 | +- `get_session_id()` |
| 73 | +- `is_closed()` |
| 74 | + |
| 75 | +### `QueryOptions` |
| 76 | + |
| 77 | +Supported options in v1: |
| 78 | + |
| 79 | +- `cwd` |
| 80 | +- `model` |
| 81 | +- `path_to_qwen_executable` |
| 82 | +- `permission_mode` |
| 83 | +- `can_use_tool` |
| 84 | +- `env` |
| 85 | +- `system_prompt` |
| 86 | +- `append_system_prompt` |
| 87 | +- `debug` |
| 88 | +- `max_session_turns` |
| 89 | +- `core_tools` |
| 90 | +- `exclude_tools` |
| 91 | +- `allowed_tools` |
| 92 | +- `auth_type` |
| 93 | +- `include_partial_messages` |
| 94 | +- `resume` |
| 95 | +- `continue_session` |
| 96 | +- `session_id` |
| 97 | +- `timeout` |
| 98 | +- `mcp_servers` |
| 99 | +- `stderr` |
| 100 | + |
| 101 | +Session argument priority is fixed as: |
| 102 | + |
| 103 | +1. `resume` |
| 104 | +2. `continue_session` |
| 105 | +3. `session_id` |
| 106 | + |
| 107 | +## Permission Handling |
| 108 | + |
| 109 | +When the CLI emits a `can_use_tool` control request, the SDK routes it through |
| 110 | +`can_use_tool(tool_name, tool_input, context)`. |
| 111 | + |
| 112 | +- Default behavior: deny |
| 113 | +- Default timeout: 60 seconds |
| 114 | +- Timeout fallback: deny |
| 115 | +- Callback exceptions: converted to deny with an error message |
| 116 | +- Callback context: `cancel_event`, `suggestions`, and `blocked_path` |
| 117 | +- Callback contract: `can_use_tool` must be async with 3 positional arguments; |
| 118 | + `stderr` must accept 1 positional string argument |
| 119 | + |
| 120 | +## Error Model |
| 121 | + |
| 122 | +- `ValidationError`: invalid options, invalid UUIDs, unsupported combinations |
| 123 | +- `ControlRequestTimeoutError`: initialize, interrupt, or other control request |
| 124 | + timed out |
| 125 | +- `ProcessExitError`: CLI exited non-zero |
| 126 | +- `AbortError`: control request or session was cancelled |
| 127 | + |
| 128 | +## Troubleshooting |
| 129 | + |
| 130 | +If the SDK cannot start the CLI: |
| 131 | + |
| 132 | +- Verify `qwen --version` works in the target environment |
| 133 | +- Pass `path_to_qwen_executable` if your shell uses `nvm`, `pyenv`, or other |
| 134 | + non-standard PATH setup |
| 135 | +- Use `debug=True` or `stderr=print` to surface CLI stderr while debugging |
| 136 | + |
| 137 | +If session control calls time out: |
| 138 | + |
| 139 | +- Check that the target `qwen` version supports `--input-format stream-json` |
| 140 | +- Increase `timeout.control_request` |
| 141 | +- Verify that no wrapper script is swallowing stdout/stderr |
| 142 | + |
| 143 | +## Repository Integration |
| 144 | + |
| 145 | +Repository-level helper commands: |
| 146 | + |
| 147 | +- `npm run test:sdk:python` |
| 148 | +- `npm run lint:sdk:python` |
| 149 | +- `npm run typecheck:sdk:python` |
| 150 | +- `npm run smoke:sdk:python -- --qwen qwen` |
| 151 | + |
| 152 | +## Real E2E Smoke |
| 153 | + |
| 154 | +For a real runtime check (actual `qwen` process + real model call), run from |
| 155 | +the repository root. The npm helper uses `python3`, so ensure it resolves to a |
| 156 | +Python `>=3.10` interpreter: |
| 157 | + |
| 158 | +```bash |
| 159 | +npm run smoke:sdk:python -- --qwen qwen |
| 160 | +``` |
| 161 | + |
| 162 | +This script runs: |
| 163 | + |
| 164 | +- async single-turn query |
| 165 | +- async control flow (`supported_commands`, permission mode updates) |
| 166 | +- sync `query_sync` query |
| 167 | + |
| 168 | +It prints JSON and returns non-zero on failure. |
0 commit comments