Skip to content

FIX: AzureSpeechAudioToTextConverter stop callback handles session_stopped events, which have no result - #3241

Merged
hannahwestra25 merged 5 commits into
microsoft:mainfrom
Sriram-PR:fix-speech-stop-cb
Oct 19, 2026
Merged

hannahwestra25 merged 5 commits into
microsoft:mainfrom
Sriram-PR:fix-speech-stop-cb

Conversation

@Sriram-PR

Copy link
Copy Markdown
Contributor

Description

Fixes #3237 (the underlying bug from #3073).

stop_cb is connected to both the recognizer's canceled and session_stopped events, but it always read evt.result.reason. session_stopped delivers a SessionEventArgs, which has no result, so every normal end of a session raised AttributeError inside the callback. The SDK swallows exceptions from callbacks, and self.done is set before the failing line, so transcription still returned its text and the error went unnoticed.

The callback now reads result only when the event has one. Checked against azure-cognitiveservices-speech 1.52.0: SessionEventArgs has no result, while SpeechRecognitionCanceledEventArgs inherits it from RecognitionEventArgs. A session event just stops recognition and marks the conversion done, and cancellation events keep their existing reason and error-detail logging. I also updated the docstring to say which events it receives.

Tests and Documentation

Two tests in tests/unit/converter/test_azure_speech_text_converter.py, both using spec-restricted mocks as the issue suggests, since a plain MagicMock makes up a result attribute and hides the bug:

  • test_stop_cb_session_stopped_event_has_no_result: MagicMock(spec=SessionEventArgs). Recognition is stopped, done is set, and nothing is logged as a cancellation or an error. It fails on main with AttributeError: Mock object has no attribute 'result'.
  • test_stop_cb_canceled_event_logs_error_details: MagicMock(spec=SpeechRecognitionCanceledEventArgs) with CancellationReason.Error. The cancellation reason and error details are still logged.

The existing test_stop_cb (EndOfStream cancellation) passes unchanged, and so does the reproduction from the issue. pytest tests/unit/converter passes, with Python 3.11 and the speech extra. ruff format, ruff check and ty check pass on the changed files. No docs or notebooks changed.

@hannahwestra25 hannahwestra25 self-assigned this Oct 15, 2026
Comment thread pyrit/converter/azure_speech_audio_to_text_converter.py Outdated
Comment thread tests/unit/converter/test_azure_speech_text_converter.py Outdated

@hannahwestra25 hannahwestra25 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thanks for contributing!

@hannahwestra25 hannahwestra25 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

thanks for contributing!

@hannahwestra25
hannahwestra25 added this pull request to the merge queue Oct 19, 2026
Merged via the queue into microsoft:main with commit d44b3d1 Oct 19, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Speech transcription callback fails on session_stopped events

2 participants