Skip to content

Commit b4c98cc

Browse files
authored
Merge pull request #2520 from daspecster/fix-encoding-speech-bug-2519
Speech Python 3.5 encoding content - 'not JSON serializable'
2 parents 9a195c6 + 9addc39 commit b4c98cc

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

google-cloud-speech/google/cloud/speech/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from base64 import b64encode
1818

1919
from google.cloud._helpers import _to_bytes
20+
from google.cloud._helpers import _bytes_to_unicode
2021
from google.cloud import client as client_module
2122
from google.cloud.speech.connection import Connection
2223
from google.cloud.speech.encoding import Encoding
@@ -237,7 +238,8 @@ def _build_request_data(sample, language_code=None, max_alternatives=None,
237238
:returns: Dictionary with required data for Google Speech API.
238239
"""
239240
if sample.content is not None:
240-
audio = {'content': b64encode(_to_bytes(sample.content))}
241+
audio = {'content':
242+
_bytes_to_unicode(b64encode(_to_bytes(sample.content)))}
241243
else:
242244
audio = {'uri': sample.source_uri}
243245

google-cloud-speech/unit_tests/test_client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ def test_create_sample_from_client(self):
6161
self.assertEqual(content_sample.encoding, Encoding.FLAC)
6262

6363
def test_sync_recognize_content_with_optional_parameters(self):
64-
import base64
64+
from base64 import b64encode
6565
from google.cloud._helpers import _to_bytes
66+
from google.cloud._helpers import _bytes_to_unicode
67+
6668
from google.cloud.speech.encoding import Encoding
6769
from google.cloud.speech.sample import Sample
6870
from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE
69-
70-
_B64_AUDIO_CONTENT = base64.b64encode(_to_bytes(self.AUDIO_CONTENT))
71+
_AUDIO_CONTENT = _to_bytes(self.AUDIO_CONTENT)
72+
_B64_AUDIO_CONTENT = _bytes_to_unicode(b64encode(_AUDIO_CONTENT))
7173
RETURNED = SYNC_RECOGNIZE_RESPONSE
7274
REQUEST = {
7375
'config': {

0 commit comments

Comments
 (0)