Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Issue with Google Cloud Speech-to-Text Streaming Recognition and Custom Classes in Java SDK

Hello Google Cloud Speech-to-Text community,

I hope this message finds you well. I am currently working on a Java client application that utilizes the Speech-to-Text streaming recognition functionality provided by the Google Cloud SDK. The overall functionality is working smoothly; however, I have encountered an issue when attempting to integrate custom classes into the recognition process.

I am facing an "io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Custom classes need at least one item" error at runtime, even though I have configured and verified the presence of items within the custom class "leikaiwei" through the Google Cloud Console.

Here's a snippet of the relevant code:

 

Hello Google Cloud Speech-to-Text community,

I hope this message finds you well. I am currently working on a Java client application that utilizes the Speech-to-Text streaming recognition functionality provided by the Google Cloud SDK. The overall functionality is working smoothly; however, I have encountered an issue when attempting to integrate custom classes into the recognition process.

I am facing an "io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Custom classes need at least one item" error at runtime, even though I have configured and verified the presence of items within the custom class "leikaiwei" through the Google Cloud Console.

Here's a snippet of the relevant code:

```java
// Code snippet
ClientStream<StreamingRecognizeRequest> clientStream = speechClient.streamingRecognizeCallable().splitCall(responseObserver);

RecognitionConfig.Builder recognitionConfigBuilder = RecognitionConfig.newBuilder()
    .setEncoding(RecognitionConfig.AudioEncoding.LINEAR16)
    .setLanguageCode(config.getLanguageCode())
    .setModel("command_and_search")
    .setSampleRateHertz(16000);

if (Phrases.size() > 0) {
    recognitionConfigBuilder.addSpeechContexts(0, SpeechContext.newBuilder()
        .addAllPhrases(instance.Phrases)
        .build());
}

if (true) {
    recognitionConfigBuilder.setAdaptation(
        SpeechAdaptation.newBuilder().addCustomClasses(
            CustomClass.newBuilder()
                .setCustomClassId("leikaiwei")
                .setName("projects/787011263872/locations/global/customClasses/leikaiwei")
                .build()
        ).build()
    );
}

RecognitionConfig recognitionConfig = recognitionConfigBuilder.build();

 

 

Despite having items in the "leikaiwei" custom class, I'm encountering the mentioned runtime error. I have verified the configuration in the Google Cloud Console, and everything seems correct.

Any assistance or guidance on resolving this issue would be greatly appreciated. If you have any insights into potential misconfigurations or if there's something specific I need to adjust in the code, please let me know.

Thank you in advance for your support!

Best regards,

Laynex

2 1 1,039
1 REPLY 1

The error message you're encountering, `io.grpc.StatusRuntimeException: INVALID_ARGUMENT: Custom classes` need at least one item, is specific to the Google Cloud Speech-to-Text service and typically occurs when attempting to use a custom class that doesn't contain any items.

Here's an example of how you might modify your code:

Show More

// Assuming you have a list of items in the 'leikaiwei' class stored in 'customClassItemsList'

CustomClass customClass = CustomClass.newBuilder()
.setCustomClassId("leikaiwei")
.setName("projects/787011263872/locations/global/customClasses/leikaiwei")
.addAllItems(customClassItemsList) // Add your items here
.build();

SpeechAdaptation adaptation = SpeechAdaptation.newBuilder()
.addCustomClasses(customClass)
.build();

recognitionConfigBuilder.setAdaptation(adaptation);

`addAllItems(customClassItemsList)` is where you include the items you want to add to your custom class.

`customClassItemsList` is assumed to be a list containing the words, phrases, or items you want to include in this custom class. It's essential to ensure these items are compatible with the Speech-to-Text API requirements.

Custom classes need at least one item. Google Cloud Speech-to-Text custom classes are used to enhance speech recognition by providing additional context or specifying words or phrases that the service should prioritize or recognize better. The error message is straightforward. It indicates that the custom class you're trying to use must contain at least one item (word or phrase) within it.

Top Labels in this Space
Top Solution Authors