コンテンツにスキップ

OpenAI STT

OpenAISTTTranscriptionSession

Bases: StreamedTranscriptionSession

A transcription session for OpenAI's STT model.

Source code in src/agents/voice/models/openai_stt.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
class OpenAISTTTranscriptionSession(StreamedTranscriptionSession):
    """A transcription session for OpenAI's STT model."""

    def __init__(
        self,
        input: StreamedAudioInput,
        client: AsyncOpenAI,
        model: str,
        settings: STTModelSettings,
        trace_include_sensitive_data: bool,
        trace_include_sensitive_audio_data: bool,
    ):
        self.connected: bool = False
        self._client = client
        self._model = model
        self._settings = settings
        self._turn_detection = settings.turn_detection or DEFAULT_TURN_DETECTION
        self._trace_include_sensitive_data = trace_include_sensitive_data
        self._trace_include_sensitive_audio_data = trace_include_sensitive_audio_data

        self._input_queue: asyncio.Queue[npt.NDArray[np.int16 | np.float32] | None] = input.queue
        self._output_queue: asyncio.Queue[str | ErrorSentinel | SessionCompleteSentinel] = (
            asyncio.Queue()
        )
        self._websocket: websockets.ClientConnection | None = None
        self._event_queue: asyncio.Queue[dict[str, Any] | ErrorSentinel | WebsocketDoneSentinel] = (
            asyncio.Queue()
        )
        self._state_queue: asyncio.Queue[dict[str, Any] | ErrorSentinel] = asyncio.Queue()
        self._turn_audio_buffer: list[npt.NDArray[np.int16 | np.float32]] = []
        self._tracing_span: Span[TranscriptionSpanData] | None = None

        # tasks
        self._listener_task: asyncio.Task[Any] | None = None
        self._process_events_task: asyncio.Task[Any] | None = None
        self._stream_audio_task: asyncio.Task[Any] | None = None
        self._connection_task: asyncio.Task[Any] | None = None
        self._stored_exception: Exception | None = None

    def _start_turn(self) -> None:
        self._tracing_span = transcription_span(
            model=self._model,
            model_config={
                "temperature": self._settings.temperature,
                "language": self._settings.language,
                "prompt": self._settings.prompt,
                "turn_detection": self._turn_detection,
            },
        )
        self._tracing_span.start()

    def _end_turn(self, _transcript: str) -> None:
        if self._tracing_span is not None:
            # Only encode audio if tracing is enabled AND buffer is not empty
            if self._trace_include_sensitive_audio_data and self._turn_audio_buffer:
                self._tracing_span.span_data.input = _audio_to_base64(self._turn_audio_buffer)

            self._tracing_span.span_data.input_format = "pcm"

            if self._trace_include_sensitive_data:
                self._tracing_span.span_data.output = _transcript

            self._tracing_span.finish()
            self._turn_audio_buffer = []
            self._tracing_span = None

    async def _event_listener(self) -> None:
        assert self._websocket is not None, "Websocket not initialized"

        try:
            async for message in self._websocket:
                event = json.loads(message)

                if event.get("type") == "error":
                    raise STTWebsocketConnectionError(f"Error event: {event.get('error')}")

                if event.get("type") in [
                    "session.updated",
                    "transcription_session.updated",
                    "session.created",
                    "transcription_session.created",
                ]:
                    await self._state_queue.put(event)

                await self._event_queue.put(event)
        except Exception as e:
            error = ErrorSentinel(e)
            await self._event_queue.put(error)
            await self._state_queue.put(error)
        finally:
            await self._event_queue.put(WebsocketDoneSentinel())

    async def _configure_session(self) -> None:
        assert self._websocket is not None, "Websocket not initialized"
        transcription_config: dict[str, Any] = {"model": self._model}
        if self._settings.language is not None:
            if self._model in {"gpt-transcribe", "gpt-live-transcribe"}:
                transcription_config["languages"] = [self._settings.language]
            else:
                transcription_config["language"] = self._settings.language
        if self._settings.prompt is not None:
            transcription_config["prompt"] = self._settings.prompt

        await self._websocket.send(
            json.dumps(
                {
                    "type": "session.update",
                    "session": {
                        "type": "transcription",
                        "audio": {
                            "input": {
                                "format": {"type": "audio/pcm", "rate": 24000},
                                "transcription": transcription_config,
                                "turn_detection": self._turn_detection,
                            }
                        },
                    },
                }
            )
        )

    async def _setup_connection(self, ws: websockets.ClientConnection) -> None:
        self._websocket = ws
        self._listener_task = asyncio.create_task(self._event_listener())

        try:
            event = await _wait_for_event(
                self._state_queue,
                ["session.created", "transcription_session.created"],
                SESSION_CREATION_TIMEOUT,
            )
        except _ListenerError:
            raise
        except TimeoutError as e:
            wrapped_err = STTWebsocketConnectionError(
                "Timeout waiting for transcription_session.created event"
            )
            await self._output_queue.put(ErrorSentinel(wrapped_err))
            raise wrapped_err from e
        except Exception as e:
            await self._output_queue.put(ErrorSentinel(e))
            raise

        await self._configure_session()

        try:
            event = await _wait_for_event(
                self._state_queue,
                ["session.updated", "transcription_session.updated"],
                SESSION_UPDATE_TIMEOUT,
            )
            if _debug.DONT_LOG_MODEL_DATA:
                logger.debug("Session updated")
            else:
                logger.debug("Session updated: %s", event)
        except _ListenerError:
            raise
        except TimeoutError as e:
            wrapped_err = STTWebsocketConnectionError(
                "Timeout waiting for transcription_session.updated event"
            )
            await self._output_queue.put(ErrorSentinel(wrapped_err))
            raise wrapped_err from e
        except Exception as e:
            await self._output_queue.put(ErrorSentinel(e))
            raise

    async def _handle_events(self) -> None:
        while True:
            try:
                event = await asyncio.wait_for(
                    self._event_queue.get(), timeout=EVENT_INACTIVITY_TIMEOUT
                )
                if isinstance(event, WebsocketDoneSentinel):
                    # processed all events and websocket is done
                    break
                if isinstance(event, ErrorSentinel):
                    raise STTWebsocketConnectionError("Error parsing events") from event.error

                event_type = event.get("type", "unknown")
                if event_type in [
                    "input_audio_transcription_completed",  # legacy
                    "conversation.item.input_audio_transcription.completed",
                ]:
                    transcript = cast(str, event.get("transcript", ""))
                    if len(transcript) > 0:
                        self._end_turn(transcript)
                        self._start_turn()
                        await self._output_queue.put(transcript)
                await asyncio.sleep(0)  # yield control
            except asyncio.TimeoutError:
                # No new events for a while. Assume the session is done.
                break
            except Exception as e:
                await self._output_queue.put(ErrorSentinel(e))
                raise
        await self._output_queue.put(SessionCompleteSentinel())

    async def _stream_audio(
        self, audio_queue: asyncio.Queue[npt.NDArray[np.int16 | np.float32] | None]
    ) -> None:
        assert self._websocket is not None, "Websocket not initialized"
        self._start_turn()
        while True:
            buffer = await audio_queue.get()
            if buffer is None:
                break

            if self._trace_include_sensitive_audio_data:
                # The buffer is only read back to populate the span input, so retaining it
                # when audio tracing is off would hold a whole turn of PCM for nothing.
                self._turn_audio_buffer.append(buffer)
            try:
                await self._websocket.send(
                    json.dumps(
                        {
                            "type": "input_audio_buffer.append",
                            "audio": _audio_buffer_to_base64(buffer),
                        }
                    )
                )
            except websockets.ConnectionClosed:
                break
            except Exception as e:
                await self._output_queue.put(ErrorSentinel(e))
                raise

            await asyncio.sleep(0)  # yield control

    async def _process_websocket_connection(self) -> None:
        try:
            await refresh_openai_client_api_key_if_supported(self._client)
            async with websockets.connect(
                _prepare_websocket_url(self._client),
                additional_headers=_prepare_websocket_headers(self._client),
                logger=get_openai_websocket_logger(),
            ) as ws:
                await self._setup_connection(ws)
                self._process_events_task = asyncio.create_task(self._handle_events())
                self._stream_audio_task = asyncio.create_task(self._stream_audio(self._input_queue))
                self.connected = True
                if self._listener_task:
                    await self._listener_task
                else:
                    logger.error("Listener task not initialized")
                    raise AgentsException("Listener task not initialized")
        except _ListenerError as e:
            if self._process_events_task is None:
                self._process_events_task = asyncio.create_task(self._handle_events())
            await self._process_events_task
            raise STTWebsocketConnectionError("Error parsing events") from e.__cause__
        except Exception as e:
            await self._output_queue.put(ErrorSentinel(e))
            raise

    def _check_errors(self) -> None:
        if (
            self._connection_task
            and self._connection_task.done()
            and not self._connection_task.cancelled()
        ):
            exc = self._connection_task.exception()
            if isinstance(exc, Exception):
                self._stored_exception = exc

        if (
            self._process_events_task
            and self._process_events_task.done()
            and not self._process_events_task.cancelled()
        ):
            exc = self._process_events_task.exception()
            if isinstance(exc, Exception):
                self._stored_exception = exc

        if (
            self._stream_audio_task
            and self._stream_audio_task.done()
            and not self._stream_audio_task.cancelled()
        ):
            exc = self._stream_audio_task.exception()
            if isinstance(exc, Exception):
                self._stored_exception = exc

        if (
            self._listener_task
            and self._listener_task.done()
            and not self._listener_task.cancelled()
        ):
            exc = self._listener_task.exception()
            if isinstance(exc, Exception):
                self._stored_exception = exc

    async def _cleanup_tasks(self) -> None:
        owned_tasks = [
            task
            for task in (
                self._listener_task,
                self._process_events_task,
                self._stream_audio_task,
                self._connection_task,
            )
            if task is not None and task is not asyncio.current_task()
        ]
        for task in owned_tasks:
            if not task.done():
                task.cancel()

        if owned_tasks:
            await asyncio.gather(*owned_tasks, return_exceptions=True)

    async def transcribe_turns(self) -> AsyncIterator[str]:
        self._connection_task = asyncio.create_task(self._process_websocket_connection())

        primary_exception: BaseException | None = None
        try:
            while True:
                turn = await self._output_queue.get()
                if (
                    turn is None
                    or isinstance(turn, ErrorSentinel)
                    or isinstance(turn, SessionCompleteSentinel)
                ):
                    self._output_queue.task_done()
                    break
                try:
                    yield turn
                finally:
                    self._output_queue.task_done()
        except BaseException as exc:
            primary_exception = exc
            raise
        finally:
            cleanup_exception: BaseException | None = None
            try:
                await self.close()
            except BaseException as exc:
                cleanup_exception = exc

            # Closing drains the owned tasks, so inspect their final outcomes before choosing
            # between the session error and a secondary cleanup failure.
            self._check_errors()
            task_exception = self._stored_exception
            preserve_primary_exception = primary_exception is not None
            exception_to_raise: BaseException | None = None
            if isinstance(primary_exception, asyncio.CancelledError):
                pass
            elif isinstance(cleanup_exception, asyncio.CancelledError):
                exception_to_raise = cleanup_exception
            elif preserve_primary_exception:
                pass
            elif task_exception is not None:
                exception_to_raise = task_exception
            elif cleanup_exception is not None:
                exception_to_raise = cleanup_exception

            cleanup_exception_was_suppressed = (
                cleanup_exception is not None
                and not isinstance(cleanup_exception, asyncio.CancelledError)
                and cleanup_exception is not exception_to_raise
            )
            if cleanup_exception_was_suppressed:
                try:
                    logger.warning("STT session cleanup failed while preserving another exception")
                except Exception:
                    # Logging must not replace the selected exception.
                    pass

            if exception_to_raise is not None:
                raise exception_to_raise

    async def close(self) -> None:
        try:
            if self._websocket:
                await self._websocket.close()
        finally:
            try:
                await self._cleanup_tasks()
            finally:
                self._end_turn("")

OpenAISTTModel

Bases: STTModel

A speech-to-text model for OpenAI.

Source code in src/agents/voice/models/openai_stt.py
class OpenAISTTModel(STTModel):
    """A speech-to-text model for OpenAI."""

    def __init__(
        self,
        model: str,
        openai_client: AsyncOpenAI,
    ):
        """Create a new OpenAI speech-to-text model.

        Args:
            model: The name of the model to use.
            openai_client: The OpenAI client to use.
        """
        self.model = model
        self._client = openai_client

    @property
    def model_name(self) -> str:
        return self.model

    def _non_null_or_not_given(self, value: Any) -> Any:
        return value if value is not None else None  # NOT_GIVEN

    async def transcribe(
        self,
        input: AudioInput,
        settings: STTModelSettings,
        trace_include_sensitive_data: bool,
        trace_include_sensitive_audio_data: bool,
    ) -> str:
        """Transcribe an audio input.

        Args:
            input: The audio input to transcribe.
            settings: The settings to use for the transcription.

        Returns:
            The transcribed text.
        """
        with transcription_span(
            model=self.model,
            input=input.to_base64() if trace_include_sensitive_audio_data else "",
            input_format="pcm",
            model_config={
                "temperature": self._non_null_or_not_given(settings.temperature),
                "language": self._non_null_or_not_given(settings.language),
                "prompt": self._non_null_or_not_given(settings.prompt),
            },
        ) as span:
            try:
                response = await self._client.audio.transcriptions.create(
                    model=self.model,
                    file=input.to_audio_file(),
                    prompt=self._non_null_or_not_given(settings.prompt),
                    language=self._non_null_or_not_given(settings.language),
                    temperature=self._non_null_or_not_given(settings.temperature),
                )
                if trace_include_sensitive_data:
                    span.span_data.output = response.text
                return response.text
            except Exception as e:
                span.span_data.output = ""
                span.set_error(
                    SpanError(
                        message=get_trace_error(
                            trace_include_sensitive_data=trace_include_sensitive_data,
                            error_message=str(e),
                        ),
                        data={},
                    )
                )
                raise

    async def create_session(
        self,
        input: StreamedAudioInput,
        settings: STTModelSettings,
        trace_include_sensitive_data: bool,
        trace_include_sensitive_audio_data: bool,
    ) -> StreamedTranscriptionSession:
        """Create a new transcription session.

        Args:
            input: The audio input to transcribe.
            settings: The settings to use for the transcription.
            trace_include_sensitive_data: Whether to include sensitive data in traces.
            trace_include_sensitive_audio_data: Whether to include sensitive audio data in traces.

        Returns:
            A new transcription session.
        """
        return OpenAISTTTranscriptionSession(
            input,
            self._client,
            self.model,
            settings,
            trace_include_sensitive_data,
            trace_include_sensitive_audio_data,
        )

__init__

__init__(model: str, openai_client: AsyncOpenAI)

Create a new OpenAI speech-to-text model.

Parameters:

Name Type Description Default
model str

The name of the model to use.

required
openai_client AsyncOpenAI

The OpenAI client to use.

required
Source code in src/agents/voice/models/openai_stt.py
def __init__(
    self,
    model: str,
    openai_client: AsyncOpenAI,
):
    """Create a new OpenAI speech-to-text model.

    Args:
        model: The name of the model to use.
        openai_client: The OpenAI client to use.
    """
    self.model = model
    self._client = openai_client

transcribe async

transcribe(
    input: AudioInput,
    settings: STTModelSettings,
    trace_include_sensitive_data: bool,
    trace_include_sensitive_audio_data: bool,
) -> str

Transcribe an audio input.

Parameters:

Name Type Description Default
input AudioInput

The audio input to transcribe.

required
settings STTModelSettings

The settings to use for the transcription.

required

Returns:

Type Description
str

The transcribed text.

Source code in src/agents/voice/models/openai_stt.py
async def transcribe(
    self,
    input: AudioInput,
    settings: STTModelSettings,
    trace_include_sensitive_data: bool,
    trace_include_sensitive_audio_data: bool,
) -> str:
    """Transcribe an audio input.

    Args:
        input: The audio input to transcribe.
        settings: The settings to use for the transcription.

    Returns:
        The transcribed text.
    """
    with transcription_span(
        model=self.model,
        input=input.to_base64() if trace_include_sensitive_audio_data else "",
        input_format="pcm",
        model_config={
            "temperature": self._non_null_or_not_given(settings.temperature),
            "language": self._non_null_or_not_given(settings.language),
            "prompt": self._non_null_or_not_given(settings.prompt),
        },
    ) as span:
        try:
            response = await self._client.audio.transcriptions.create(
                model=self.model,
                file=input.to_audio_file(),
                prompt=self._non_null_or_not_given(settings.prompt),
                language=self._non_null_or_not_given(settings.language),
                temperature=self._non_null_or_not_given(settings.temperature),
            )
            if trace_include_sensitive_data:
                span.span_data.output = response.text
            return response.text
        except Exception as e:
            span.span_data.output = ""
            span.set_error(
                SpanError(
                    message=get_trace_error(
                        trace_include_sensitive_data=trace_include_sensitive_data,
                        error_message=str(e),
                    ),
                    data={},
                )
            )
            raise

create_session async

create_session(
    input: StreamedAudioInput,
    settings: STTModelSettings,
    trace_include_sensitive_data: bool,
    trace_include_sensitive_audio_data: bool,
) -> StreamedTranscriptionSession

Create a new transcription session.

Parameters:

Name Type Description Default
input StreamedAudioInput

The audio input to transcribe.

required
settings STTModelSettings

The settings to use for the transcription.

required
trace_include_sensitive_data bool

Whether to include sensitive data in traces.

required
trace_include_sensitive_audio_data bool

Whether to include sensitive audio data in traces.

required

Returns:

Type Description
StreamedTranscriptionSession

A new transcription session.

Source code in src/agents/voice/models/openai_stt.py
async def create_session(
    self,
    input: StreamedAudioInput,
    settings: STTModelSettings,
    trace_include_sensitive_data: bool,
    trace_include_sensitive_audio_data: bool,
) -> StreamedTranscriptionSession:
    """Create a new transcription session.

    Args:
        input: The audio input to transcribe.
        settings: The settings to use for the transcription.
        trace_include_sensitive_data: Whether to include sensitive data in traces.
        trace_include_sensitive_audio_data: Whether to include sensitive audio data in traces.

    Returns:
        A new transcription session.
    """
    return OpenAISTTTranscriptionSession(
        input,
        self._client,
        self.model,
        settings,
        trace_include_sensitive_data,
        trace_include_sensitive_audio_data,
    )