Use the Python SDK to create, list, query, update, and delete custom vocabularies for speech recognition.
Endpoint
The SDK connects to the China (Beijing) endpoint by default. To use a different region, specify the region parameter when constructing dashscope.Client (for example, ap-southeast-1 for the Singapore region).
- Singapore
- China (Beijing)
https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1Replace {WorkspaceId} with your actual Workspace ID.- API keys differ across regions. Use the API key that matches your region.
- The region setting is specific to the
dashscope.Clientinstance and only affects API calls made through that client.
client.audio.vocabularies
Resource: the client.audio.vocabularies resource of a dashscope.Client instance
Description: Creates, lists, queries, updates, and deletes custom vocabularies.
Constructor
api_key is not passed, the SDK automatically reads the value of the DASHSCOPE_API_KEY environment variable.
create() - Create a custom vocabulary
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
target_model | str | Yes | The speech recognition model that uses this vocabulary. This value must match the model you specify when calling the speech recognition API. |
prefix | str | Yes | A custom prefix for the vocabulary. Only lowercase letters and digits are allowed, with a maximum length of 10 characters. |
vocabulary | List[dict] | Yes | A list of hotwords. Each entry is a dict with fields text, weight, and lang. For more information, see Hotword dictionary structure. |
Type | Description |
|---|---|
Vocabulary | The vocabulary object. Use the |
list() - List custom vocabularies
Maps to the HTTP API action: list_vocabulary.
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
prefix | str | No | The custom prefix of the vocabulary. When specified, only vocabularies with this prefix are returned. |
page_index | int | No | The page number, starting from 0. Default value: 0. |
page_size | int | No | The number of entries per page. Default value: 10. |
Type | Description |
|---|---|
VocabularyList | A paginated vocabulary list object. The |
Field | Type | Description |
|---|---|---|
vocabulary_id | str | The vocabulary ID. |
gmt_create | str | The creation time. |
gmt_modified | str | The last modification time. |
status | str | The status:
|
get() - Query a custom vocabulary
Maps to query_vocabulary() in the 1.x SDK.
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
vocabulary_id | str | Yes | The ID of the custom vocabulary to query. |
Type | Description |
|---|---|
Vocabulary | A vocabulary details object containing vocabulary, target_model, gmt_create, gmt_modified, and status. |
Field | Type | Description |
|---|---|---|
vocabulary | List[dict] | The hotword list content. |
target_model | str | The speech recognition model that uses this vocabulary. This value must match the model you specify when calling the speech recognition API. |
gmt_create | str | The creation time. |
gmt_modified | str | The last modification time. |
status | str | The status:
|
update() - Update a custom vocabulary
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
vocabulary_id | str | Yes | The ID of the vocabulary to update. |
vocabulary | List[dict] | Yes | The new vocabulary. This completely replaces the existing entries. |
JobStatus object, whose status field indicates the operation status.
delete() - Delete a custom vocabulary
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
vocabulary_id | str | Yes | The ID of the vocabulary to delete. |
JobStatus object, whose status field indicates the operation status.
Hotword dictionary structure
Fields in each vocabulary dict:
Field | Type | Required | Description |
|---|---|---|---|
text | str | Yes | The vocabulary entry text. The text language must be supported by the selected model. Supported languages vary by model. Use actual words rather than arbitrary character combinations to improve recognition accuracy. Maximum length: 15 characters for text that includes non-ASCII characters, or 7 space-separated words for ASCII-only text. |
weight | int | Yes | The vocabulary entry weight. Recommended value: 4. Valid values: 1 to 5. If recognition accuracy doesn't improve, increase the weight. An excessively high weight may reduce the recognition accuracy of other words. |
lang | str | No | The language code of the audio to be recognized. When set, the system improves recognition of vocabulary entries in the specified language. If you can't determine the language in advance, leave this parameter unset. The model detects the language automatically. Valid values (vary by model):
|