New Cloud Speech-to-Text users should use the V2 API. Read our migration guide to learn how to migrate existing projects to the latest version.

Get automatic punctuation

This page describes how to get automatic punctuation in transcription results from Cloud Speech-to-Text. When you enable this feature, Cloud STT automatically infers the presence of periods, commas, and question marks in your audio data and adds them to the transcript.

By default, Cloud STT does not include punctuation marks in the results from speech recognition. However, you can request that Cloud STT automatically detect and insert punctuation in transcription results. When you enable automatic punctuation Cloud STT will also automatically capitalize the first letter after each period and question mark.

To enable automatic punctuation, set the enableAutomaticPunctuation field to true in the RecognitionConfig parameters for the request. The Cloud Speech-to-Text API supports automatic punctuation for all speech recognition methods: speech:recognize, speech:longrunningrecognize, and Streaming.

The following code samples demonstrate how to get automatic punctuation details in a transcription request.

Protocol

Refer to the speech:recognize API endpoint for complete details.

To perform synchronous speech recognition, make a POST request and provide the appropriate request body. The following shows an example of a POST request using curl. The example uses the Google Cloud CLI to generate an access token. For instructions on installing the gcloud CLI, see the quickstart.

curl -s -H "Content-Type: application/json" \
 -H "Authorization: Bearer "$(gcloud auth print-access-token) \
 https://speech.googleapis.com/v1/speech:recognize \
 --data '{
 "config": {
 "encoding":"FLAC",
 "sampleRateHertz": 16000,
 "languageCode": "en-US",
 "enableAutomaticPunctuation": true
 },
 "audio": {
 "uri":"gs://cloud-samples-tests/speech/brooklyn.flac"
 }
}'

See the RecognitionConfig reference documentation for more information on configuring the request body.

If the request is successful, the server returns a 200 OK HTTP status code and the response in JSON format:

{
 "results": [
 {
 "alternatives": [
 {
 "transcript": "How old is the Brooklyn Bridge?",
 "confidence": 0.98360395
 }
 ]
 }
 ]
}

Go

To learn how to install and use the client library for Cloud STT, see Cloud STT client libraries. For more information, see the Cloud STT Go API reference documentation.

To authenticate to Cloud STT, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


import(
"context"
"fmt"
"io"
"os"
"strings"
speech"cloud.google.com/go/speech/apiv1"
"cloud.google.com/go/speech/apiv1/speechpb"
)
funcautoPunctuation(wio.Writer,pathstring)error{
ctx:=context.Background()
client,err:=speech.NewClient (ctx)
iferr!=nil{
returnfmt.Errorf("NewClient: %w",err)
}
deferclient.Close()
// path = "../testdata/commercial_mono.wav"
data,err:=os.ReadFile(path)
iferr!=nil{
returnfmt.Errorf("ReadFile: %w",err)
}
resp,err:=client.Recognize(ctx,&speechpb.RecognizeRequest{
Config:&speechpb.RecognitionConfig{
Encoding:speechpb.RecognitionConfig_LINEAR16 ,
SampleRateHertz:8000,
LanguageCode:"en-US",
// Enable automatic punctuation.
EnableAutomaticPunctuation:true,
},
Audio:&speechpb.RecognitionAudio{
AudioSource:&speechpb.RecognitionAudio_Content{Content:data},
},
})
iferr!=nil{
returnfmt.Errorf("Recognize: %w",err)
}
fori,result:=rangeresp.Results{
fmt.Fprintf(w,"%s\n",strings.Repeat("-",20))
fmt.Fprintf(w,"Result %d\n",i+1)
forj,alternative:=rangeresult.Alternatives{
fmt.Fprintf(w,"Alternative %d: %s\n",j+1,alternative.Transcript)
}
}
returnnil
}

Java

To learn how to install and use the client library for Cloud STT, see Cloud STT client libraries. For more information, see the Cloud STT Java API reference documentation.

To authenticate to Cloud STT, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

/**
 * Performs transcription on remote FLAC file and prints the transcription.
 *
 * @param gcsUri the path to the remote FLAC audio file to transcribe.
 */
publicstaticvoidtranscribeGcsWithAutomaticPunctuation(StringgcsUri)throwsException{
try(SpeechClientspeechClient=SpeechClient.create()){
// Configure request with raw PCM audio
RecognitionConfigconfig=
RecognitionConfig.newBuilder()
.setEncoding(AudioEncoding.FLAC)
.setLanguageCode("en-US")
.setSampleRateHertz(16000)
.setEnableAutomaticPunctuation(true)
.build();
// Set the remote path for the audio file
RecognitionAudioaudio=RecognitionAudio.newBuilder().setUri(gcsUri).build();
// Use non-blocking call for getting file transcription
OperationFuture<LongRunningRecognizeResponse,LongRunningRecognizeMetadata>response=
speechClient.longRunningRecognizeAsync(config,audio);
while(!response.isDone()){
System.out.println("Waiting for response...");
Thread.sleep(10000);
}
// Just print the first result here.
SpeechRecognitionResultresult=response.get().getResultsList().get(0);
// There can be several alternative transcripts for a given chunk of speech. Just use the
// first (most likely) one here.
SpeechRecognitionAlternativealternative=result.getAlternativesList().get(0);
// Print out the result
System.out.printf("Transcript : %s\n",alternative.getTranscript());
}
}

Node.js

To learn how to install and use the client library for Cloud STT, see Cloud STT client libraries. For more information, see the Cloud STT Node.js API reference documentation.

To authenticate to Cloud STT, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

// Imports the Google Cloud client library for API
/**
 * TODO(developer): Update client library import to use new
 * version of API when desired features become available
 */
constspeech=require('@google-cloud/speech');
constfs=require('fs');
// Creates a client
constclient=newspeech.SpeechClient ();
/**
 * TODO(developer): Uncomment the following lines before running the sample.
 * Include the sampleRateHertz field in the config object.
 */
// const filename = 'Local path to audio file, e.g. /path/to/audio.raw';
// const encoding = 'Encoding of the audio file, e.g. LINEAR16';
// const sampleRateHertz = 16000;
// const languageCode = 'BCP-47 language code, e.g. en-US';
constconfig={
encoding:encoding,
languageCode:languageCode,
enableAutomaticPunctuation:true,
};
constaudio={
content:fs.readFileSync(filename).toString('base64'),
};
constrequest={
config:config,
audio:audio,
};
// Detects speech in the audio file
const[response]=awaitclient.recognize(request);
consttranscription=response.results
.map(result=>result.alternatives[0].transcript)
.join('\n');
console.log('Transcription: ',transcription);

Python

To learn how to install and use the client library for Cloud STT, see Cloud STT client libraries. For more information, see the Cloud STT Python API reference documentation.

To authenticate to Cloud STT, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


fromgoogle.cloudimport speech
deftranscribe_file_with_auto_punctuation(audio_file: str) -> speech.RecognizeResponse:
"""Transcribe the given audio file with auto punctuation enabled.
 Args:
 audio_file (str): Path to the local audio file to be transcribed.
 Returns:
 speech.RecognizeResponse: The response containing the transcription results.
 """
 client = speech.SpeechClient()
 with open(audio_file, "rb") as f:
 audio_content = f.read()
 audio = speech.RecognitionAudio(content=audio_content)
 config = speech.RecognitionConfig(
 encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
 sample_rate_hertz=8000,
 language_code="en-US",
 # Enable automatic punctuation
 enable_automatic_punctuation=True,
 )
 response = client.recognize(config=config, audio=audio)
 for i, result in enumerate(response.results):
 alternative = result.alternatives[0]
 print("-" * 20)
 print(f"First alternative of result {i}")
 print(f"Transcript: {alternative.transcript}")
 return response

Additional languages

C#: Please follow the C# setup instructions on the client libraries page and then visit the Cloud STT reference documentation for .NET.

PHP: Please follow the PHP setup instructions on the client libraries page and then visit the Cloud STT reference documentation for PHP.

Ruby: Please follow the Ruby setup instructions on the client libraries page and then visit the Cloud STT reference documentation for Ruby.

What's next

Review how to make synchronous transcription requests.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年11月20日 UTC.