@polyweave/qwen (1.0.8)
Installation
@polyweave:registry=https://hub.kl1.tenere.ai/api/packages/Polyweave/npm/npm install @polyweave/qwen@1.0.8"@polyweave/qwen": "1.0.8"About this package
@polyweave/qwen
Qwen / Alibaba adapter for Polyweave — widest catalog, multimodal, 1M context
Published as @polyweave/qwen.
Synopsis
import { createQwenAdapter } from '@polyweave/qwen';
import { createTextFrame, createFrameSink } from '@polyweave/core';
var adapter = createQwenAdapter({ apiKey: process.env.DASHSCOPE_API_KEY, model: 'qwen3-max' });
adapter.source.pipe(createFrameSink(function(frame) {
if (frame.type === 'TEXT') console.log(frame.content.text);
}));
adapter.sink.write(createTextFrame('Explain the theory of relativity'));
Install
npm install @polyweave/qwen
Why
Qwen (Alibaba) offers the widest model catalog among OpenAI-compatible providers — from the flagship qwen3-max through multimodal qwen3.5-plus (text+image+video), coding models (qwen-coder-plus), embeddings (qwen3-embedding), and the omni model. It supports 1M context windows, thinking mode (chain-of-thought up to 81,920 tokens), and multiple deployment regions. For tasks requiring a single adapter that covers text generation, vision, embeddings, and tool calling at competitive per-token pricing, Qwen is the most versatile option.
API
createQwenAdapter(config)
Creates an OpenAI-compatible chat completions adapter for Alibaba Qwen / DashScope.
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
config.apiKey |
string |
No | process.env.QWEN_API_KEY or process.env.DASHSCOPE_API_KEY or null |
API key for DashScope authentication |
config.baseURL |
string |
No | https://dashscope-intl.aliyuncs.com/compatible-mode/v1 |
Base URL for the OpenAI-compatible endpoint. Use a China-specific URL for domestic deployments. |
config.timeout |
number |
No | 120000 |
Request timeout in milliseconds |
config.budget |
number |
No | 0 |
Total spend limit in USD. 0 disables. When exceeded, emits BUDGET_EXCEEDED error and END. |
config.name |
string |
No | null |
Duplex name for topology identification and PARAM targeting |
The model parameter is set via PARAM frame with key model (not in the constructor config). The initial default is qwen3-max.
Returns: IFrameDuplex — an object with the following properties:
| Property | Type | Description |
|---|---|---|
source |
IFrameSource |
Readable source emitting response frames |
sink |
IFrameSink |
Writable sink accepting request frames |
name |
`string | null` |
setReady(key) |
function |
Sets the API key and flushes the frame buffer. Called automatically with config.apiKey if provided. |
Frame Protocol
Inbound frames (sink):
| Frame Type | Effect |
|---|---|
TEXT |
Appends a user message to the conversation and sends /chat/completions. Emits TEXT (streaming or batch), TOOL_CALL, RECEIPT, END on response. |
IMAGE |
Appends a content: [{ type: 'image_url', ... }, { type: 'text', ... }] user message and sends /chat/completions. Requires content.url or content.data/content.b64_json with content.mimeType. Emits TEXT, END, RECEIPT. |
TOOL_RESULT |
Appends a tool message to the conversation with role: 'tool'. Does not emit a response on its own — the next TEXT frame will include the tool results in context. |
EMBEDDING |
Sends an embedding request to /embeddings using params.embedding_model (default qwen3-embedding). Emits EMBEDDING frame with content.vector, content.dimensions, and content.model, then RECEIPT and END. |
PARAM |
Sets runtime parameters. Supported keys: model, max_tokens, temperature, top_p, stream, enable_thinking, system, tools, tool_choice, response_format, embedding_model, budget, apiKey. |
COST_CHECK |
Enters cost-check mode. Subsequent frames are buffered and a COST frame is emitted with cost estimates, then END. If COST_CHECK has maxBudget, a BUDGET_EXCEEDED error is emitted when the estimate exceeds the budget. |
START |
Ignored (no-op). |
END |
Ignored (no-op). Adapter emits its own END per response. |
TOPOLOGY_SCAN |
Emits TOPOLOGY_REPORT and passes through. |
TEST_INJECT |
Injects frames from content.frames array. |
TEST_SNAPSHOT |
Emits a snapshot report. |
Outbound frames (source):
| Frame Type | When Emitted |
|---|---|
TEXT |
Streaming: for each content delta chunk. Batch: single frame with full text. Metadata includes model, usage, finishReason. |
IMAGE |
If the response contains image content (multimodal models). |
TOOL_CALL |
When the model emits tool calls (both streaming and batch). Includes name, arguments (parsed JSON), and toolCallId. |
EMBEDDING |
Embedding response with content.vector, content.dimensions, content.model. |
RECEIPT |
After each response (includes token counts and cost). Streaming: progressive receipts every 50 output tokens. Batch: single receipt at end. |
COST |
Response to COST_CHECK. Includes bounds, accuracy, estimateSource, forwardEstimate, inputBounds, maxOutputTokens. |
ERROR |
On failure. Code is ADAPTER_ERROR or BUDGET_EXCEEDED. |
END |
Marks end of each response stream. |
Error Conditions
| Condition | Frame Type | Code | Message |
|---|---|---|---|
| HTTP status >= 400 from API | ERROR + END |
ADAPTER_ERROR |
API error message from response body |
| Network error (connection refused, DNS, etc.) | ERROR + END |
ADAPTER_ERROR |
Node.js error message |
| Request timeout | ERROR + END |
ADAPTER_ERROR |
'Timeout' |
| JSON parse failure on batch response | ERROR + END |
ADAPTER_ERROR |
'Parse: ' + error message |
| JSON parse failure on streaming chunk | Silently caught — individual chunks are skipped | — | — |
| Stream error in SSE data | ERROR + END |
ADAPTER_ERROR |
Error message from stream chunk |
Budget exceeded (totalSpent >= budgetTotal with budgetTotal > 0) |
ERROR + END |
BUDGET_EXCEEDED |
'Budget exceeded' |
COST_CHECK estimate exceeds maxBudget |
ERROR + END |
BUDGET_EXCEEDED |
'Est cost exceeds budget' |
IMAGE frame without url or data/b64_json |
ERROR + END |
ADAPTER_ERROR |
'IMAGE frame requires url/data' |
| Embedding API error | ERROR + END |
ADAPTER_ERROR |
API error message |
No API key and no PARAM apiKey |
Frame buffers silently until setReady is called |
— | — |
Tests
npm test
Runs node --test test/rigor.test.js test/smoke.test.js. Covers: adapter construction with and without API key, setReady availability, frame buffering before API key is set, property-based testing of adapter behavior.
Requirements
Node.js 22 or later. ESM only.
Peer dependencies: @polyweave/core, @polyweave/costs.
Caveats
Qwen uses tiered pricing by input size for some models. The international endpoint (dashscope-intl.aliyuncs.com) is used by default. For China deployment, set baseURL to the China-specific endpoint. Thinking mode is toggled via PARAM enable_thinking (boolean). The adapter buffers all frames until an API key is available — if no key is provided in config and no PARAM apiKey arrives, frames accumulate in memory indefinitely.
See Also
Dependencies
Development Dependencies
| ID | Version |
|---|---|
| @rigor/core | * |
Peer Dependencies
| ID | Version |
|---|---|
| @polyweave/core | * |
| @polyweave/costs | * |
| @polyweave/errors | * |