0

In a Python program performing streaming speech recognition to Google's Speech API (essentially as detailed here), I'm trying to capture the following exception:

google.api_core.exceptions.InvalidArgument: 400 Client GRPC deadline too short. Should be at least: 3 * audio-duration + 5 seconds. Current deadline is: 189.99934293236583 second(s). Required at least: 196 second(s).

The relevant code block:

    with self.MicrophoneStream(self.RATE, self.CHUNK) as stream:
        while True:
            audio_generator = stream.generator()
            requests = (types.StreamingRecognizeRequest(audio_content=content)
                        for content in audio_generator)
            responses = client.streaming_recognize(streaming_config, requests)

            try:
                self.listen_print_loop(responses, self.callback)
                break
            except:
                self.start()
                break

Not labeling the exception is functional, except that it makes the program hard to debug, since it causes any exception anywhere to trigger self.start().

I've tried:
- except StatusCode.INVALID_ARGUMENT # NameError: name 'StatusCode' is not defined
- except google.api_core.exceptions.InvalidArgument # NameError: name 'google' is not defined
- except grpc.RpcError # no error, just the usual exception above

Naftali Beder
  • 1,066
  • 1
  • 10
  • 27
  • 1
    Why isn't adding `from google.api_core import exceptions` at the top of your file and then changing your `except` to `except exceptions.InvalidArgument` the right solution to this problem? (It may or may not be; it's not clear to me that that's something that you already tried.) – Nathaniel Manista At Google Apr 02 '18 at 21:04
  • 1
    @NathanielManistaAtGoogle This works - I hadn't imported the exceptions correctly. Happy to approve it as the answer if you post it. – Naftali Beder Apr 16 '18 at 20:08

0 Answers0