Skip to main content
Custom hotwords

Custom hotwords Python SDK

Use the Python SDK to create, list, query, update, and delete custom vocabularies for speech recognition.

User guide: Custom hotwords.

Prerequisites

Service URL

Set the base URL before creating the service:
import dashscope

dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

VocabularyService class

Package: dashscope.audio.asr.VocabularyService Manages the lifecycle of custom vocabularies (create, list, query, update, delete).

Constructor

VocabularyService(api_key: str = None, workspace: str = None, model: str = None)
If api_key is not passed, the SDK uses the global dashscope.api_key.

create_vocabulary()

Create a custom vocabulary.
def create_vocabulary(self, target_model: str, prefix: str,
                      vocabulary: List[dict]) -> str
ParameterTypeRequiredDescription
target_modelstrYesThe speech recognition model that uses this vocabulary. Must match the model you specify when calling the speech recognition API.
prefixstrYesA custom prefix for the vocabulary. Only lowercase letters and digits are allowed, max 10 characters.
vocabularyList[dict]YesA list of hotwords. See Hotword entry structure.
Returns: str — the ID of the created vocabulary.

list_vocabularies()

List custom vocabularies with optional filtering and pagination.
The HTTP API uses the singular form list_vocabulary, while the Python method name uses the plural list_vocabularies.
def list_vocabularies(self, prefix: str = None,
                      page_index: int = 0,
                      page_size: int = 10) -> List[dict]
ParameterTypeRequiredDescription
prefixstrNoFilter by vocabulary prefix.
page_indexintNoPage number, starting from 0. Default: 0.
page_sizeintNoNumber of entries per page. Default: 10.
Returns: List[dict] — a list of vocabulary objects, each containing:
FieldTypeDescription
vocabulary_idstrThe vocabulary ID.
gmt_createstrThe creation time.
gmt_modifiedstrThe last modification time.
statusstrOK: Ready. UNDEPLOYED: Not available.

query_vocabulary()

Query details of a specific custom vocabulary.
def query_vocabulary(self, vocabulary_id: str) -> dict
ParameterTypeRequiredDescription
vocabulary_idstrYesThe ID of the custom vocabulary to query.
Returns: dict — a vocabulary object containing:
FieldTypeDescription
vocabularyList[dict]The hotword list content.
target_modelstrThe speech recognition model that uses this vocabulary.
gmt_createstrThe creation time.
gmt_modifiedstrThe last modification time.
statusstrOK: Ready. UNDEPLOYED: Not available.

update_vocabulary()

Update a custom vocabulary. This completely replaces the existing entries.
def update_vocabulary(self, vocabulary_id: str,
                      vocabulary: List[dict]) -> None
ParameterTypeRequiredDescription
vocabulary_idstrYesThe ID of the vocabulary to update.
vocabularyList[dict]YesThe new vocabulary entries. See Hotword entry structure.
Returns: None

delete_vocabulary()

Delete a custom vocabulary.
def delete_vocabulary(self, vocabulary_id: str) -> None
ParameterTypeRequiredDescription
vocabulary_idstrYesThe ID of the vocabulary to delete.
Returns: None

Hotword entry structure

Each entry in the vocabulary list has the following fields:
FieldTypeRequiredDescription
textstrYesThe vocabulary entry text. The text language must be supported by the selected model. Use actual words rather than arbitrary character combinations. Maximum length: 15 characters for text that includes non-ASCII characters, or 7 space-separated words for ASCII-only text.
weightintYesThe 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.
langstrNoThe 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. Valid values vary by model. Fun-ASR: zh (Chinese), en (English), ja (Japanese).
Custom hotwords Python SDK - Qwen Cloud