Skip to content
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
6 changes: 5 additions & 1 deletion sdk/voice/speechmatics/voice/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,15 @@ def _prepare_config(
# LIFECYCLE METHODS
# ============================================================================

async def connect(self) -> None:
async def connect(self, ws_headers: Optional[dict] = None) -> None:
"""Connect to the Speechmatics API.

Establishes WebSocket connection and starts the transcription session.
This must be called before sending audio.

Args:
ws_headers: Optional headers to pass to the WebSocket connection.

Raises:
Exception: If connection fails.

Expand Down Expand Up @@ -521,6 +524,7 @@ async def connect(self) -> None:
await self.start_session(
transcription_config=self._transcription_config,
audio_format=self._audio_format,
ws_headers=ws_headers,
)
self._is_connected = True
self._start_metrics_task()
Expand Down
76 changes: 73 additions & 3 deletions tests/voice/test_06_stt_config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,78 @@
import os
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a topline description for the test file?


import pytest
from _utils import get_client

# Constants
API_KEY = os.getenv("SPEECHMATICS_API_KEY")


@pytest.mark.asyncio
async def test_with_headers():
"""Tests that a client can be created.

- Checks for a valid session
- Checks that 'English' is the language pack info
Copy link
Contributor

Choose a reason for hiding this comment

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

This docstring isn't correct. There's no check for the language pack.

"""

# API key
if not API_KEY:
pytest.skip("Valid API key required for test")

# Create client
client = await get_client(
api_key=API_KEY,
connect=False,
)

# Headers
ws_headers = {"Z-TEST-HEADER-1": "ValueOne", "Z-TEST-HEADER-2": "ValueTwo"}
Copy link
Contributor

Choose a reason for hiding this comment

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

For the two ws_headers used, it would be clearer to pull them out as constants and have a VALID_WS_HEADERS, INVALID_WS_HEADERS, or something similar, rather than hard-coding them into the tests.


# Check we are connected OK
await client.connect(ws_headers=ws_headers)

# Check we are connected
assert client._is_connected
Copy link
Contributor

Choose a reason for hiding this comment

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

Here, you access a private attribute - a public property or function would be better.


# Disconnect
await client.disconnect()

# Check we are disconnected
assert not client._is_connected


@pytest.mark.asyncio
async def test_no_partials():
"""Tests for STT config (no partials)."""
async def test_with_corrupted_headers():
Copy link
Contributor

@LArmstrongDev LArmstrongDev Mar 9, 2026

Choose a reason for hiding this comment

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

The test name should convey the expected outcome. Can you improve the naming? Same for the test above.

Copy link
Contributor

Choose a reason for hiding this comment

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

This only tests passing a list instead of a dict.

Could expand test coverage to test a few other cases - empty dict, headers with non-string values, very large values etc, with parametrizing a few test headers.

"""Tests that a client can be created.
Copy link
Contributor

Choose a reason for hiding this comment

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

This docstring isn't correct - it's a test for handling corrupted/invalid headers.


- Checks for a valid session
- Checks that 'English' is the language pack info
"""

# API key
if not API_KEY:
pytest.skip("Valid API key required for test")

# Create client
client = await get_client(
api_key=API_KEY,
connect=False,
)

# Headers
ws_headers = ["ItemOne", "ItemTwo"]

# Check we are connected OK
try:
await client.connect(ws_headers=ws_headers)
Copy link
Contributor

Choose a reason for hiding this comment

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

With this, could add a timeout to avoid hanging indefinitely, waiting to connect.

except AttributeError:
Copy link
Contributor

@LArmstrongDev LArmstrongDev Mar 9, 2026

Choose a reason for hiding this comment

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

Why do we silently pass here? This will mask any bugs that come up in the client connection code. Given that the headers are corrupted, I assume we expect a failure - but here, if connect() raises no exception somehow, the test would still pass (false positive).

pass

# Check we are connected
assert not client._is_connected

# Disconnect (in case connected)
await client.disconnect()

pass
# Check we are disconnected
assert not client._is_connected