Skip to content

Change run async wrapper to use explicit arguments #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [4.0.4] - 2025-06-06

- Changing async run wrapper to use explicit arguments to fix multichannel tests in aladdin

## [4.0.3] - 2025-06-06

- Fixed microphone transcription tests not working after adding multichan dz support
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.3
4.0.4
6 changes: 5 additions & 1 deletion examples/transcribe_from_youtube/youtube_transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ def main(args):

def run(stream):
try:
api.run_synchronously(stream, transcription_config, AudioSettings())
api.run_synchronously(
stream=stream,
transcription_config=transcription_config,
audio_settings=AudioSettings(),
)
except KeyboardInterrupt:
# Gracefully handle Ctrl-C, else we get a huge stack-trace.
LOGGER.warning("Keyboard interrupt received.")
Expand Down
14 changes: 4 additions & 10 deletions speechmatics/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,20 +810,14 @@ def rt_main(args):

def run(stream=None, channel_stream_pairs=None):
try:
# Pass in either stream or channel_stream_pairs depending on what != None
# Dynamically construct the args based on the input
args_list = [transcription_config]
if stream is not None:
args_list.append(stream)
elif channel_stream_pairs is not None:
args_list.append(None) # This skips the stream argument
args_list.append(channel_stream_pairs)
else:
if stream is None and channel_stream_pairs is None:
raise SystemExit(
"Neither stream nor channel_stream_pairs were provided."
)
api.run_synchronously(
*args_list,
transcription_config=transcription_config,
stream=stream,
channel_stream_pairs=channel_stream_pairs,
audio_settings=get_audio_settings(args),
from_cli=True,
extra_headers=extra_headers,
Expand Down
33 changes: 31 additions & 2 deletions speechmatics/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,10 @@ async def run(
consumer/producer tasks.
"""
if channel_stream_pairs:
if not isinstance(channel_stream_pairs, dict):
raise TypeError(
"channel_stream_pairs must be a dict of {str: file-like or path}"
)
opened_streams = {}
self._stream_exits = AsyncExitStack()
for channel_name, path in channel_stream_pairs.items():
Expand All @@ -569,6 +573,9 @@ async def run(
await self._init_synchronization_primitives()
if extra_headers is None:
extra_headers = {}
else:
if not isinstance(extra_headers, dict):
raise TypeError("extra_headers must be a dict")
if audio_settings is None:
audio_settings = AudioSettings()
if (
Expand Down Expand Up @@ -632,13 +639,35 @@ def stop(self):
"""
self._session_needs_closing = True

def run_synchronously(self, *args, timeout=None, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you can't pass arbitrary keyword args any more -- but I presume nothing did. I suppose the original code was just so it wasn't necessary to change run_synchronously and self.run in step.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transcribe_from_microphone example passed things in positionally before multichan dz borked it, but no use of args* AFAIK.

Hoping Dumitru and Tudor/anyone else more invested in the Python client says its fine and that no customers are passing things in this way.

Copy link
Contributor

@dumitrugutu dumitrugutu Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change. All our setups and examples -- both in this repo and in the docs -- expect the first argument to be audio, followed by transcription_config, then audio_settings.

We’ll need to bump the major version if we go ahead with this. That said, this issue is already addressed in the new SDK (yet to be released), so we should either release this as a major version or close it in favor of the new SDK.

def run_synchronously(
self,
transcription_config: TranscriptionConfig,
*,
stream: Optional[Any] = None,
channel_stream_pairs: Optional[Dict[str, Any]] = None,
audio_settings: Optional[AudioSettings] = None,
from_cli: bool = False,
extra_headers: Optional[Dict] = None,
timeout=None,
):
"""
Run the transcription synchronously.
:raises asyncio.TimeoutError: If the given timeout is exceeded.
"""
# pylint: disable=no-value-for-parameter
asyncio.run(asyncio.wait_for(self.run(*args, **kwargs), timeout=timeout))
asyncio.run(
asyncio.wait_for(
self.run(
transcription_config=transcription_config,
stream=stream,
channel_stream_pairs=channel_stream_pairs,
audio_settings=audio_settings,
from_cli=from_cli,
extra_headers=extra_headers,
),
timeout=timeout,
)
)

async def send_message(self, message_type: str, data: Optional[Any] = None):
"""
Expand Down