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, set dashscope.base_http_api_url before making any API calls.
- Singapore
- China (Beijing)
https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1Replace {WorkspaceId} with your actual Workspace ID.dashscope.base_http_api_url before making any calls:
- API keys differ across regions. Use the API key that matches your region.
- The region setting is global and affects all DashScope API calls.
VocabularyService
Package: dashscope.audio.asr.VocabularyService
Description: Creates, lists, queries, updates, and deletes custom vocabularies.
Constructor
api_key is not passed, the SDK uses the global dashscope.api_key.
create_vocabulary() - 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 |
|---|---|
str | The ID of the created vocabulary. |
list_vocabularies() - List custom vocabularies
Maps to the HTTP API action: list_vocabulary (HTTP uses the singular form, while the Python method name list_vocabularies uses the plural).
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 |
|---|---|
List[dict] | A list of vocabulary objects, each containing vocabulary_id, gmt_create, gmt_modified, and status. |
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:
|
query_vocabulary() - Query a custom vocabulary
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
vocabulary_id | str | Yes | The ID of the custom vocabulary to query. |
Type | Description |
|---|---|
dict | A vocabulary 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_vocabulary() - 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. |
delete_vocabulary() - Delete a custom vocabulary
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
vocabulary_id | str | Yes | The ID of the vocabulary to delete. |
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):
|