Skip to content

Python tool for converting files and office documents to Markdown.

Override default SpeechRecognition endpoint (#2846)

Adam Fourney3ae4f03dd6a7
diff --git a/packages/markitdown/pyproject.toml b/packages/markitdown/pyproject.toml
index fc6db99c..1ef01c22 100644
--- a/packages/markitdown/pyproject.toml
+++ b/packages/markitdown/pyproject.toml
@@ -46,7 +46,7 @@ all = [
"pdfplumber>=0.11.9",
"olefile",
"pydub",
- "SpeechRecognition",
+ "SpeechRecognition>=3.10.4",
"youtube-transcript-api~=1.2.3",
"azure-ai-documentintelligence",
"azure-ai-contentunderstanding>=1.2.0b1",
@@ -58,7 +58,7 @@ xlsx = ["pandas", "openpyxl"]
xls = ["pandas", "xlrd"]
pdf = ["pdfminer.six>=20251230", "pdfplumber>=0.11.9"]
outlook = ["olefile"]
-audio-transcription = ["pydub", "SpeechRecognition"]
+audio-transcription = ["pydub", "SpeechRecognition>=3.10.4"]
youtube-transcription = ["youtube-transcript-api~=1.2.3"]
az-doc-intel = ["azure-ai-documentintelligence", "azure-identity"]
# >=1.2.0b1 required for to_llm_input() helper used by ContentUnderstandingConverter
diff --git a/packages/markitdown/src/markitdown/converters/_transcribe_audio.py b/packages/markitdown/src/markitdown/converters/_transcribe_audio.py
index 6caa2c65..b304ce28 100644
--- a/packages/markitdown/src/markitdown/converters/_transcribe_audio.py
+++ b/packages/markitdown/src/markitdown/converters/_transcribe_audio.py
@@ -56,5 +56,9 @@ def transcribe_audio(
recognizer = sr.Recognizer()
with sr.AudioFile(audio_source) as source:
audio = recognizer.record(source)
- transcript = recognizer.recognize_google(audio, language=language).strip()
+ transcript = recognizer.recognize_google(
+ audio,
+ language=language,
+ endpoint="https://www.google.com/speech-api/v2/recognize",
+ ).strip()
return "[No speech detected]" if transcript == "" else transcript
diff --git a/packages/markitdown/tests/test_module_misc.py b/packages/markitdown/tests/test_module_misc.py
index 23d6f49a..e9cba9ca 100644
--- a/packages/markitdown/tests/test_module_misc.py
+++ b/packages/markitdown/tests/test_module_misc.py
@@ -328,6 +328,32 @@ def test_get_content_disposition_filename_decodes_rfc5987() -> None:
# assert test_string in result.text_content
+def test_speech_transcription_uses_https(monkeypatch: pytest.MonkeyPatch) -> None:
+ from speech_recognition.recognizers import google
+
+ requests_sent = []
+
+ def fake_urlopen(request, timeout=None):
+ requests_sent.append(request)
+ return io.BytesIO(
+ b'{"result":[{"alternative":[{"transcript":"one two three four five"}]}]}'
+ )
+
+ monkeypatch.setattr(google, "urlopen", fake_urlopen)
+
+ result = MarkItDown().convert(os.path.join(TEST_FILES_DIR, "test.wav"))
+
+ assert "one two three four five" in result.text_content
+ assert len(requests_sent) == 1
+ request = requests_sent[0]
+ assert request.full_url.startswith(
+ "https://www.google.com/speech-api/v2/recognize?"
+ )
+ assert request.get_method() == "POST"
+ assert request.get_header("Content-type").startswith("audio/x-flac")
+ assert request.data.startswith(b"fLaC")
+
+
@pytest.mark.skipif(
skip_remote,
reason="do not run remotely run speech transcription tests",