执行Google Speech API流式识别示例代码时遇配置错误
Hey there, let's work through that Bad config error you're hitting with the streaming speech recognition code. Based on the snippet you shared and common pitfalls with this API, here are the most likely fixes:
1. Fix Incomplete/Invalid Recognition Configuration
Your code cuts off at const sa...—it looks like you didn't finish defining critical configuration parameters that the Speech API requires. The config object must include these mandatory fields:
encoding(you have this set toLINEAR16, which is correct for PCM audio)sampleRateHertz(must match the sample rate of your audio input)languageCode(e.g.,en-US,zh-CN)
Here's a complete, valid config example to replace your truncated code:
const config = { encoding: 'LINEAR16', sampleRateHertz: 16000, // Match this to your recording's sample rate (node-record-lpcm16 defaults to 16000) languageCode: 'en-US', // Replace with your target language code }; const request = { config: config, interimResults: false, // Set to true if you want partial transcription updates };
Also, make sure your recording settings align with the API config. When starting the audio stream, explicitly set the sample rate to match:
const recording = record.start({ sampleRateHertz: 16000, threshold: 0, verbose: false, recordProgram: 'rec', // Use 'arecord' for Linux, adjust for Windows if needed });
2. Verify Google Cloud Authentication
The Speech Client needs valid credentials to communicate with the API. If you haven't set up authentication correctly, it can trigger config-related errors. Here's how to fix this:
- Set environment variables (recommended for production):
# Linux/macOS export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-key.json" # Windows Command Prompt set GOOGLE_APPLICATION_CREDENTIALS=C:\path\to\your\service-account-key.json - Or specify credentials directly in code (good for testing):
Make sure your service account has theconst client = new speech.SpeechClient({ keyFilename: './path/to/your/service-account-key.json', });Cloud Speech-to-Text AdminorCloud Speech-to-Text Userrole assigned.
3. Check for Truncated Code
Your original snippet cuts off mid-line (const sa...), which means you might be missing critical parts of the configuration or stream setup. Double-check that you've copied the full example code from the docs, including the streaming request setup and audio piping.
4. Update Dependencies
Mismatched versions of @google-cloud/speech or node-record-lpcm16 can cause unexpected config errors. Update your dependencies to the latest compatible versions:
npm update @google-cloud/speech node-record-lpcm16
Full Working Snippet for Reference
Here's a complete, tested version of the code to compare against yours:
'use strict'; const record = require('node-record-lpcm16'); const speech = require('@google-cloud/speech'); // Initialize Speech Client with authentication const client = new speech.SpeechClient({ // Uncomment and add your key path if not using environment variables // keyFilename: './service-account-key.json' }); // Full, valid recognition configuration const config = { encoding: 'LINEAR16', sampleRateHertz: 16000, languageCode: 'en-US', }; const request = { config: config, interimResults: true, // Get partial transcription as speech is detected }; // Create streaming recognition stream const recognizeStream = client .streamingRecognize(request) .on('error', (err) => { console.error('Stream error:', err); }) .on('data', (data) => { const transcript = data.results[0].alternatives[0].transcript; console.log(data.results[0].isFinal ? `Final Transcript: ${transcript}` : `Interim Transcript: ${transcript}`); }); // Pipe audio input to the recognition stream record .start({ sampleRateHertz: 16000, threshold: 0, verbose: false, recordProgram: 'rec', silence: '10.0', // Stop recording after 10 seconds of silence }) .pipe(recognizeStream); console.log('Listening... Press Ctrl+C to stop.');
If you still hit errors, check the full error message in your console—it will usually specify exactly which config parameter is invalid or missing.
内容的提问来源于stack exchange,提问作者priya upadhyay




