google speech-to-text apvi2 error: Invalid `language_codes`: field must be non-empty

111 Views Asked by At

I'm trying out the speech-to-text apiv2 in go and I am getting this error:

Failed to recognize: rpc error: code = InvalidArgument desc = Invalid `language_codes`: field must be non-empty.

From what I understand, this field is optional:

https://cloud.google.com/speech-to-text/v2/docs/reference/rpc/google.cloud.speech.v2#recognitionconfig

Regardless, there is no LanguageCodes field in the RecognitionConfig struct.

package main

import (
    speech "cloud.google.com/go/speech/apiv2"
    speechpb "cloud.google.com/go/speech/apiv2/speechpb"
    "context"
    "fmt"
    "log"
)

func main() {
    ctx := context.Background()
    client, err := speech.NewClient(ctx)

    gcsURI := fmt.Sprintf("gs://%s/%s", "my-bucket", "myfile.mp3")

    config := &speechpb.RecognitionConfig{
        DecodingConfig: &speechpb.RecognitionConfig_AutoDecodingConfig{},
    }
    
    audioSource := &speechpb.RecognizeRequest_Uri{
        Uri: gcsURI
    }

    req := &speechpb.RecognizeRequest{
        Config: config,
        Recognizer:  "projects/my-project/locations/global/recognizers/_",
        AudioSource: audioSource,
    }

    resp, err := client.Recognize(ctx, req)
    if err != nil {
        // TODO: Handle error.
        log.Fatalf("Failed to recognize: %v", err)
    }
    
    for _, result := range resp.Results {
        for _, alt := range result.Alternatives {
            fmt.Printf("\"%v\" (confidence=%3f)\n", alt.Transcript, alt.Confidence)
        }
    }

0

There are 0 best solutions below