All Products
Search
Document Center

Alibaba Cloud Model Studio:Custom vocabulary

Last Updated:Dec 15, 2025

If speech recognition performance is poor for certain words or phrases in your specific business domain, add them to a custom vocabulary. This prioritizes their recognition and improves accuracy.

Important

This document applies only to the China (Beijing) region. To use the models, you need an API key for the China (Beijing) region.

Overview

Custom vocabularies are used in the SDK as a list of terms. The list is a JSON array where each term is an object with the following fields:

Field

Type

Required

Description

text

string

Yes

The custom vocabulary term.

Each term cannot exceed 15 Chinese characters or 7 English words. If a term contains both Chinese and English, the total number of characters and letters cannot exceed 15.

weight

int

Yes

The weight of the term. This must be an integer from 1 to 5.

A common value is 4.

If recognition does not improve, you can increase the weight. However, a high weight might cause other words to be recognized incorrectly.

lang

string

No

The language code. This lets you boost terms for a specific language supported by the Automatic Speech Recognition (ASR) model. For more information about supported languages and their codes, see the API reference for the model. To apply the boost, you must specify the same language in the language_hints parameter of your recognition request. Terms for other languages will be ignored.

Scenarios

To improve the recognition accuracy of movie titles, add the following titles to a custom vocabulary.

[
    {"text": "赛德克巴莱", "weight": 4, "lang": "zh"},
    {"text": "Seediq Bale", "weight": 4, "lang": "en"},
    
    {"text": "夏洛特烦恼", "weight": 4, "lang": "zh"},
    {"text": "Goodbye Mr. Loser", "weight": 4, "lang": "en"},
    
    {"text": "阙里人家", "weight": 4, "lang": "zh"},
    {"text": "Confucius' Family", "weight": 4, "lang": "en"},
]

Supported models

  • Real-time speech recognition: paraformer-realtime-v2, paraformer-realtime-8k-v2, fun-asr-realtime, fun-asr-realtime-2025-11-07, fun-asr-realtime-2025-09-15

  • Batch speech recognition: paraformer-v2, paraformer-8k-v2, fun-asr, fun-asr-2025-11-07, fun-asr-2025-08-25

Billing

Custom vocabularies are currently free of charge.

Quotas

  1. Each account can create up to 10 vocabularies. Contact us to increase the limit.

  2. The number of words in each vocabulary varies by models:

    • Fun-ASR series:

      • fun-asr, fun-asr-2025-11-07: Up to 10,000.

      • Others: Up to 1,000.

    • Paraformer series: Up to 500.

Prerequisites

  • Activate the service and obtain an API key: Create an API key.

  • Set your API key as an environment variable to reduce the risk of leaks: Set API key as environment variable. You can also write the API key in your code, but this increases the risk of leaks.

  • The latest version of the SDK has been installed: Install SDK.

Note:

1. Each vocabulary can contain up to 500 words.

2. Each account is allocated 10 vocabularies by default. To increase this limit, contact us.

Code examples

The following code examples show how to create a vocabulary and use it to recognize a local audio file with the paraformer-realtime-v2 model.

import dashscope
from dashscope.audio.asr import *


dashscope.api_key = 'your-dashscope-api-key'
prefix = 'prefix'
target_model = "paraformer-realtime-v2"

my_vocabulary = [
    {"text": "吴贻弓", "weight": 4, "lang": "zh"},
    {"text": "阙里人家", "weight": 4, "lang": "zh"},
]


# create a vocabulary
service = VocabularyService()
vocabulary_id = service.create_vocabulary(
      prefix=prefix,
      target_model=target_model,
      vocabulary=my_vocabulary)

print(f"your vocabulary id is {vocabulary_id}")

# use the vocabulary to recognize a file
recognition = Recognition(model=target_model,
                          format='wav',
                          sample_rate=16000,
                          callback=None,
                          vocabulary_id=vocabulary_id,
                          language_hints=['zh'])  # Only paraformer-v2 and paraformer-realtime-v2 supports language_hints
result = recognition.call('your-audio-file.wav')
print(result.output)
package org.example.customization;

import com.alibaba.dashscope.audio.asr.recognition.Recognition;
import com.alibaba.dashscope.audio.asr.recognition.RecognitionParam;
import com.alibaba.dashscope.audio.asr.vocabulary.Vocabulary;
import com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class VocabularySampleCodes {
    public static String apiKey = "your-dashscope-apikey";

    public static void main(String[] args) throws NoApiKeyException, InputRequiredException {
        String targetModel = "paraformer-realtime-v2";
        // prepare vocabulary
        class Hotword {
            String text;
            int weight;
            String lang;

            public Hotword(String text, int weight, String lang) {
                this.text = text;
                this.weight = weight;
                this.lang = lang;
            }
        }
        JsonArray vocabulary = new JsonArray();
        List<Hotword> wordList = new ArrayList<>();
        wordList.add(new Hotword("吴贻弓", 4, "zh"));
        wordList.add(new Hotword("阙里人家", 4, "zh"));

        for (Hotword word : wordList) {
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("text", word.text);
            jsonObject.addProperty("weight", word.weight);
            jsonObject.addProperty("lang", word.lang);
            vocabulary.add(jsonObject);
        }
        // create vocabulary
        VocabularyService service = new VocabularyService(apiKey);
        Vocabulary myVoc = service.createVocabulary(targetModel, "prefix", vocabulary);
        System.out.println("your vocabulary id is " + myVoc.getVocabularyId());
        // use the vocabulary to recognize a file
        Recognition recognizer = new Recognition();
        RecognitionParam param =
                RecognitionParam.builder()
                        .model(targetModel)
                        .format("wav")
                        .sampleRate(16000)
                        .apiKey(apiKey)
                        .vocabularyId(myVoc.getVocabularyId())
                        // Only paraformer-v2 and paraformer-realtime-v2 supports language_hints
                        .parameter("language_hints", new String[] {"zh"})
                        .build();
        String result = recognizer.call(param, new File("your-local-audio-file.wav"));
        System.out.println(result);
        System.exit(0);
    }
}

Manage vocabularies with the DashScope SDK

Initialize the object

service = VocabularyService(api_key="your-dashscope-apikey")
VocabularyService service = new VocabularyService("your-dashscope-apikey");

Create a vocabulary

def create_vocabulary(self, target_model: str, prefix: str, vocabulary: List[dict]) -> str:
    '''
    Create a vocabulary.
    param: target_model The speech recognition model for the vocabulary.
    param: prefix A custom prefix for the vocabulary. It must contain fewer than 10 lowercase letters and digits.
    param: vocabulary A dictionary for the vocabulary.
    return: The vocabulary ID.
    '''
/**
 * Create a new vocabulary.
 *
 * @param targetModel The speech recognition model for the vocabulary.
 * @param prefix A custom prefix for the vocabulary. It must contain fewer than 10 lowercase letters and digits.
 * @param vocabulary The vocabulary list.
 * @return The vocabulary object.
 * @throws NoApiKeyException if the API key is empty.
 * @throws InputRequiredException if a required parameter is empty.
 */
public Vocabulary createVocabulary(String targetModel, String prefix, JsonArray vocabulary)
    throws NoApiKeyException, InputRequiredException 

Query all vocabularies

def list_vocabularies(self, prefix=None, page_index: int = 0, page_size: int = 10) -> List[dict]:
    '''
    Query all created vocabularies.
    param: prefix A custom prefix. If specified, returns only the vocabularies with this prefix.
    param: page_index The page index.
    param: page_size The page size.
    return: A list of vocabulary identifiers.
    '''
/**
 * Query all created vocabularies. The default page index is 0, and the default page size is 10.
 *
 * @param prefix A custom prefix for the vocabulary.
 * @return An array of vocabulary objects.
 * @throws NoApiKeyException if the API key is empty.
 * @throws InputRequiredException if a required parameter is empty.
 */
public Vocabulary[] listVocabulary(String prefix)
    throws NoApiKeyException, InputRequiredException

/**
 * Query all created vocabularies.
 *
 * @param prefix A custom prefix for the vocabulary.
 * @param pageIndex The page index.
 * @param pageSize The page size.
 * @return An array of vocabulary objects.
 * @throws NoApiKeyException if the API key is empty.
 * @throws InputRequiredException if a required parameter is empty.
 */
public Vocabulary[] listVocabulary(String prefix, int pageIndex, int pageSize)
    throws NoApiKeyException, InputRequiredException

Response example

[
    {
        "gmt_create": "2024-08-21 15:19:09",
        "vocabulary_id": "vocab-xxx-1f8b10e61ac54b1da86a8d5axxxxxxxx",
        "gmt_modified": "2024-08-21 15:19:09",
        "status": "OK",
    },
    {
        "gmt_create": "2024-08-27 11:17:04",
        "vocabulary_id": "vocab-xxx-24ee19fa8cfb4d52902170a0xxxxxxxx",
        "gmt_modified": "2024-08-27 11:17:04",
        "status": "OK",
    }
]

Query a specific vocabulary

def query_vocabulary(self, vocabulary_id: str) -> List[dict]:
    '''
    Get the content of a vocabulary.
    param: vocabulary_id The vocabulary identifier.
    return: The vocabulary.
    '''
/**
 * Query a specific vocabulary.
 *
 * @param vocabularyId The vocabulary to query.
 * @return The vocabulary object.
 * @throws NoApiKeyException if the API key is empty.
 * @throws InputRequiredException if a required parameter is empty.
 */
public Vocabulary queryVocabulary(String vocabularyId)
    throws NoApiKeyException, InputRequiredException

Response example

{
    "gmt_create": "2024-08-21 15:19:09",
    "vocabulary": [
        {"weight": 4, "text": "吴贻弓", "lang": "zh"},
        {"weight": 4, "text": "阙里人家", "lang": "zh"},
    ],
    "target_model": "paraformer-realtime-v2",
    "gmt_modified": "2024-08-21 15:19:09",
    "status": "OK",
}

Update a vocabulary

def update_vocabulary(self, vocabulary_id: str, vocabulary: List[dict]) -> None:
    '''
    Replace an existing vocabulary with a new one.
    param: vocabulary_id The identifier of the vocabulary to replace.
    param: vocabulary The vocabulary.
    '''
/**
 * Update a vocabulary.
 *
 * @param vocabularyId The vocabulary to update.
 * @param vocabulary The vocabulary object.
 * @throws NoApiKeyException if the API key is empty.
 * @throws InputRequiredException if a required parameter is empty.
 */
public void updateVocabulary(String vocabularyId, JsonArray vocabulary)
    throws NoApiKeyException, InputRequiredException

Delete a vocabulary

def delete_vocabulary(self, vocabulary_id: str) -> None:
    '''
    Delete a vocabulary.
    param: vocabulary_id The identifier of the vocabulary to delete.
    '''
/**
 * Delete a vocabulary.
 *
 * @param vocabularyId The vocabulary to delete.
 * @throws NoApiKeyException if the API key is empty.
 * @throws InputRequiredException if a required parameter is empty.
 */
public void deleteVocabulary(String vocabularyId)
    throws NoApiKeyException, InputRequiredException

Error handling

In the Python SDK, errors are thrown as a VocabularyServiceException. The exception includes a status code, an error code, and an error message.

class VocabularyServiceException(Exception):
  def __init__(self, status_code: int, code: str, error_message: str)

In the Java SDK, errors are thrown as NoApiKeyException and InputRequiredException.

Manage vocabularies with the HTTP service

The custom vocabulary service uses the HTTPS protocol. You can create, read, update, and delete vocabularies via HTTP requests.

If you have not configured your API key as an environment variable, replace Authorization: Bearer $DASHSCOPE_API_KEY with Authorization: Bearer your-api-key in the cURL commands, where your-api-key is your API key.

Note

The usage field in the response is for billing purposes. Because the custom vocabulary service is free, you can ignore this field.

Create a vocabulary

cURL example:

curl -X POST https://dashscope.aliyuncs.com/api/v1/services/audio/asr/customization \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "speech-biasing",
    "input": {
            "action": "create_vocabulary",
            "target_model": "paraformer-realtime-v2",
            "prefix": "testpfx",
            "vocabulary": [
              {"text": "吴贻弓", "weight": 4, "lang": "zh"},
              {"text": "阙里人家", "weight": 4, "lang": "zh"}
            ]
        }
}'

Response example:

{
  "output": {
    "task_status": "PENDING",
    "task_id": "c2e5d63b-96e1-4607-bb91-************"
  },
  "request_id": "77ae55ae-be17-97b8-9942--************"
}

Query all vocabularies

cURL example:

curl -X POST https://dashscope.aliyuncs.com/api/v1/services/audio/asr/customization \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "speech-biasing",
    "input": {
                "action": "list_vocabulary",
                "prefix": null,
                "page_index": 0,
                "page_size": 10
            }
}'
Note

In this example, the prefix field is null, which means all vocabularies are returned. You can change this value to a specific string.

Example response:

{
	"output": {
		"vocabulary_list": [{
			"gmt_create": "2024-11-05 16:31:32",
			"vocabulary_id": "vocab-testpfx-6977ae49f65c4c3db054727cxxxxxxxx",
			"gmt_modified": "2024-11-05 16:31:32",
			"status": "OK"
		}]
	},
	"usage": {
		"count": 1
	},
	"request_id": "4e7df7c0-18a8-9f3e-bfc4-xxxxxxxxxxxx"
}

Query a specific vocabulary

cURL example:

curl -X POST https://dashscope.aliyuncs.com/api/v1/services/audio/asr/customization \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "speech-biasing",
    "input": {
                "action": "query_vocabulary",
                "vocabulary_id": "vocab-testpfx-6977ae49f65c4c3db054727cxxxxxxxx"
            }
}'

Example response:

{
	"output": {
		"gmt_create": "2024-11-05 16:31:32",
		"vocabulary": [{
			"weight": 4,
			"text": "吴贻弓",
			"lang": "zh"
		}, {
			"weight": 4,
			"text": "阙里人家",
			"lang": "zh"
		}],
		"target_model": "paraformer-realtime-v2",
		"gmt_modified": "2024-11-05 16:31:32",
		"status": "OK"
	},
	"usage": {
		"count": 1
	},
	"request_id": "b02d18a4-ff8d-9fd4-b4f0-xxxxxxxxxxxx"
}

Update a vocabulary

cURL example:

curl -X POST https://dashscope.aliyuncs.com/api/v1/services/audio/asr/customization \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "speech-biasing",
    "input": {
                "action": "update_vocabulary",
                "vocabulary_id": "vocab-testpfx-6977ae49f65c4c3db054727cxxxxxxxx",
                "vocabulary": [
                  {"text": "吴贻弓", "weight": 4, "lang": "zh"}
                ]      
            }
}'

Example response:

{
	"output": {},
	"usage": {
		"count": 1
	},
	"request_id": "a51f3139-7aaa-941b-994f-xxxxxxxxxxxx"
}

Delete a vocabulary

cURL example:

curl -X POST https://dashscope.aliyuncs.com/api/v1/services/audio/asr/customization \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "speech-biasing",
    "input": {
                "action": "delete_vocabulary",
                "vocabulary_id": "vocab-testpfx-6977ae49f65c4c3db054727cxxxxxxxx"
            }
}'

Example response:

{
	"output": {},
	"usage": {
		"count": 1
	},
	"request_id": "d7499ee5-6c91-956c-a1aa-xxxxxxxxxxxx"
}

Error codes

If an error occurs, see Error messages for troubleshooting.