diff --git a/README.md b/README.md index 94b4d84..6126dda 100644 --- a/README.md +++ b/README.md @@ -191,7 +191,7 @@ with client.tts.stream_with_timestamps(text="Long content...", format="opus") as Text without words (only punctuation, or a sound tag such as `[sigh]`) still produces audio, but the API has no timings for it: `segments` is then empty and `result.duration` is `None`. -`s1` and `transcribe-1` are deprecated and will be retired on December 31, 2026. After that date, requests that specify them are served by `s3-pro` and `transcribe-2-pro`. Using a deprecated model emits a `FutureWarning`; with Python's default warning filters it is shown once per calling line. A coroutine scheduled with `asyncio.create_task` or `gather` runs from asyncio's code, so its warning points there and is shown once in all. See [Model Deprecations](https://docs.fish.audio/developer-guide/models-pricing/deprecations). +`s1` and `transcribe-1` are deprecated and will be retired on December 31, 2026. After that date, requests that specify them are served by `s3-pro` and `transcribe-2-pro`. Using a deprecated model emits a `FutureWarning`; with Python's default warning filters it is shown once per calling line. A coroutine scheduled with `asyncio.create_task` or `gather` runs from asyncio's code, so its warning points there and is shown once in all. A transcription that names no model runs on the API's default model (`transcribe-1` until it is retired, `transcribe-2-pro` after) and emits a `FutureWarning`, shown as the deprecation warnings above are; pass a model (the `model` argument or a `model` header) to silence it. Write the model ID exactly, for example `transcribe-2-pro`: the API runs a name it does not know on its default model, and the SDK does not check names. A model that only an httpx request hook or auth flow adds is not seen, so such a request still gets the warning; if you also turn warnings into errors, pass `model` in the call. See [Model Deprecations](https://docs.fish.audio/developer-guide/models-pricing/deprecations). [Learn more](https://docs.fish.audio/developer-guide/sdk-guide/python/text-to-speech) @@ -199,7 +199,8 @@ Text without words (only punctuation, or a sound tag such as `[sigh]`) still pro ```python # transcribe-2-pro is the recommended model; without model, the request runs on transcribe-1, -# which is deprecated and will be retired on December 31, 2026 (then served by transcribe-2-pro). +# which is deprecated and will be retired on December 31, 2026 (then served by transcribe-2-pro), +# and emits a FutureWarning. with open("meeting.wav", "rb") as f: result = client.asr.transcribe(audio=f.read(), model="transcribe-2-pro") diff --git a/src/fishaudio/resources/asr.py b/src/fishaudio/resources/asr.py index 3699273..076b510 100644 --- a/src/fishaudio/resources/asr.py +++ b/src/fishaudio/resources/asr.py @@ -3,6 +3,7 @@ from collections.abc import AsyncIterable, Iterable from typing import Optional, Union +import httpx import ormsgpack from httpx_ws import aconnect_ws, connect_ws @@ -28,6 +29,7 @@ DEPRECATED_ASR_MODELS, _set_response_headers, warn_if_deprecated_model, + warn_if_no_asr_model, ) from .asr_realtime import REALTIME_PATH, ASRRealtimeStream, AsyncASRRealtimeStream @@ -47,6 +49,23 @@ def _requested_model( return None +def _sent_model( + model: Optional[str], + request_options: Optional[RequestOptions], + client_headers: httpx.Headers, +) -> Optional[str]: + """Return the model header the request sends, as httpx will merge it. + + The method argument wins, then a model header in request_options, then a + model header on the httpx client itself. Each one is skipped only when it + is absent (None), so an empty value is sent as it is. + """ + requested = _requested_model(model, request_options) + if requested is not None: + return requested + return client_headers.get("model") + + def _realtime_config( config: ASRRealtimeConfig, *, @@ -110,8 +129,11 @@ def transcribe( transcribe-1 is deprecated and will be retired on December 31, 2026; after that date, the requests it serves, including those with no ``model`` header, are served by transcribe-2-pro. - Pass model="transcribe-2-pro" to move to it now; a request - without a model gets no warning. "transcribe-1" given as + A request that carries no model, or an empty one, emits a + FutureWarning; pass a model (this argument or a ``model`` + header) to silence it. A model that only an httpx request + hook adds is not seen, so such a request still gets it. + "transcribe-1" given as ``model``, or as a ``model`` header in request_options when ``model`` is not given, emits a FutureWarning; one set only in a custom httpx client's headers is not seen. @@ -136,8 +158,10 @@ def transcribe( print(f"{segment.start}-{segment.end}: {segment.text}") ``` """ - warn_if_deprecated_model( - _requested_model(model, request_options), DEPRECATED_ASR_MODELS + requested = _requested_model(model, request_options) + warn_if_deprecated_model(requested, DEPRECATED_ASR_MODELS) + warn_if_no_asr_model( + _sent_model(model, request_options, self._client.client.headers) ) # Build request payload @@ -312,8 +336,11 @@ async def transcribe( transcribe-1 is deprecated and will be retired on December 31, 2026; after that date, the requests it serves, including those with no ``model`` header, are served by transcribe-2-pro. - Pass model="transcribe-2-pro" to move to it now; a request - without a model gets no warning. "transcribe-1" given as + A request that carries no model, or an empty one, emits a + FutureWarning; pass a model (this argument or a ``model`` + header) to silence it. A model that only an httpx request + hook adds is not seen, so such a request still gets it. + "transcribe-1" given as ``model``, or as a ``model`` header in request_options when ``model`` is not given, emits a FutureWarning; one set only in a custom httpx client's headers is not seen. @@ -338,8 +365,10 @@ async def transcribe( print(f"{segment.start}-{segment.end}: {segment.text}") ``` """ - warn_if_deprecated_model( - _requested_model(model, request_options), DEPRECATED_ASR_MODELS + requested = _requested_model(model, request_options) + warn_if_deprecated_model(requested, DEPRECATED_ASR_MODELS) + warn_if_no_asr_model( + _sent_model(model, request_options, self._client.client.headers) ) # Build request payload diff --git a/src/fishaudio/types/shared.py b/src/fishaudio/types/shared.py index 017b7a1..ad5f08b 100644 --- a/src/fishaudio/types/shared.py +++ b/src/fishaudio/types/shared.py @@ -62,6 +62,17 @@ def _get_response_headers(model: BaseModel) -> httpx.Headers: "speech-1.6": _SPEECH_1_MESSAGE.format(model="speech-1.6"), "s1": _S1_MESSAGE, } +# A transcription sent without a model runs on the API default: transcribe-1 +# until 2026-12-31, transcribe-2-pro after. +# The same words in every Fish Audio SDK. +_NO_ASR_MODEL_MESSAGE = ( + "No speech-to-text model was specified, so this request runs on the API's " + "default model. The default is 'transcribe-1' until it is retired on " + "December 31, 2026. After that date, requests without a model are served " + "by 'transcribe-2-pro' and billed as 'transcribe-2-pro', and transcripts " + "can include speaker markers and audio-event tags. Specify a model to " + "choose it yourself, for example 'transcribe-2-pro'." +) DEPRECATED_ASR_MODELS: dict[str, str] = { "transcribe-1": _TRANSCRIBE_1_MESSAGE, } @@ -120,6 +131,23 @@ def warn_if_deprecated_model( warnings.warn(message, FutureWarning, stacklevel=_stacklevel_outside_package()) +def warn_if_no_asr_model(model: Optional[str]) -> None: + """Emit a FutureWarning when a transcription is sent without a model. + + ``model`` is the value the request sends; None or an empty string means + none, and the API then runs the request on its default model. Attributed to + the line that called the SDK method, as warn_if_deprecated_model is, so + Python's warning filters decide how often it is shown: by default once + per such line; under an "error" filter it raises on every call, before + the request is sent. + """ + if model: + return + warnings.warn( + _NO_ASR_MODEL_MESSAGE, FutureWarning, stacklevel=_stacklevel_outside_package() + ) + + # Audio format types AudioFormat = Literal["wav", "pcm", "mp3", "opus"] diff --git a/tests/unit/test_deprecated_models.py b/tests/unit/test_deprecated_models.py index 2c27278..405f482 100644 --- a/tests/unit/test_deprecated_models.py +++ b/tests/unit/test_deprecated_models.py @@ -11,6 +11,7 @@ from typing import Any, Optional from unittest.mock import Mock, patch +import httpx import pytest from fishaudio.core import AsyncClientWrapper, ClientWrapper, RequestOptions @@ -359,13 +360,6 @@ async def test_async_asr(self, call): warnings.simplefilter("error") await _run_async(call, "transcribe-1-pro") - def test_asr_without_model(self): - """No model named: no warning, although the API default is transcribe-1.""" - with warnings.catch_warnings(): - warnings.simplefilter("error") - with pytest.raises(_Stop): - ASRClient(_sync_wrapper()).transcribe(audio=b"audio") - def test_tts_default_model(self): with warnings.catch_warnings(): warnings.simplefilter("error") @@ -385,6 +379,172 @@ def test_model_ids_match_exactly(self): warn_if_deprecated_model(model) +NO_MODEL_MESSAGE = ( + "No speech-to-text model was specified, so this request runs on the API's " + "default model. The default is 'transcribe-1' until it is retired on " + "December 31, 2026. After that date, requests without a model are served " + "by 'transcribe-2-pro' and billed as 'transcribe-2-pro', and transcripts " + "can include speaker markers and audio-event tags. Specify a model to " + "choose it yourself, for example 'transcribe-2-pro'." +) + + +def _asr_wrapper(make: Callable[[], Mock], headers: Any = None) -> Mock: + wrapper = make() + wrapper.client.headers = httpx.Headers(headers) + return wrapper + + +class TestASRWithoutModel: + """No model on the request: the API runs it on its default model, which + is transcribe-1 until it retires, so the caller is told. Python's warning + filters decide how often, as for a named deprecated model.""" + + def test_sync_warns_at_the_call(self): + client = ASRClient(_asr_wrapper(_sync_wrapper)) + with warnings.catch_warnings(record=True) as record: + warnings.simplefilter("always") + with pytest.raises(_Stop): + line = _line() + 1 + client.transcribe(audio=b"audio") + + emitted = _deprecation_warnings(record) + assert [(w.filename, w.lineno) for w in emitted] == [(__file__, line)] + assert str(emitted[0].message) == NO_MODEL_MESSAGE + + @pytest.mark.asyncio + async def test_async_warns_at_the_await(self): + client = AsyncASRClient(_asr_wrapper(_async_wrapper)) + with warnings.catch_warnings(record=True) as record: + warnings.simplefilter("always") + with pytest.raises(_Stop): + line = _line() + 1 + await client.transcribe(audio=b"audio") + + emitted = _deprecation_warnings(record) + assert [(w.filename, w.lineno) for w in emitted] == [(__file__, line)] + assert str(emitted[0].message) == NO_MODEL_MESSAGE + + def test_default_filter_shows_it_once_per_calling_line(self): + client = ASRClient(_asr_wrapper(_sync_wrapper)) + with warnings.catch_warnings(record=True) as record: + warnings.resetwarnings() + warnings.simplefilter("default") + for _ in range(3): # one line, called three times + with pytest.raises(_Stop): + client.transcribe(audio=b"audio") + with pytest.raises(_Stop): + client.transcribe(audio=b"audio") # another line + assert len(_deprecation_warnings(record)) == 2 + + def test_error_filter_raises_on_every_call_and_sends_nothing(self): + wrapper = _asr_wrapper(_sync_wrapper) + client = ASRClient(wrapper) + with warnings.catch_warnings(): + warnings.simplefilter("error", FutureWarning) + for _ in range(2): # a retry does not get past it either + with pytest.raises(FutureWarning, match="No speech-to-text model"): + client.transcribe(audio=b"audio") + wrapper.request.assert_not_called() + + @pytest.mark.asyncio + async def test_async_error_filter_raises_on_every_call_and_sends_nothing(self): + wrapper = _asr_wrapper(_async_wrapper) + client = AsyncASRClient(wrapper) + with warnings.catch_warnings(): + warnings.simplefilter("error", FutureWarning) + for _ in range(2): + with pytest.raises(FutureWarning, match="No speech-to-text model"): + await client.transcribe(audio=b"audio") + wrapper.request.assert_not_called() + + def test_ignored_call_does_not_silence_a_later_one(self): + client = ASRClient(_asr_wrapper(_sync_wrapper)) + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + with pytest.raises(_Stop): + client.transcribe(audio=b"audio") + with warnings.catch_warnings(record=True) as record: + warnings.simplefilter("default") + with pytest.raises(_Stop): + client.transcribe(audio=b"audio") + assert [str(w.message) for w in _deprecation_warnings(record)] == [ + NO_MODEL_MESSAGE + ] + + @pytest.mark.parametrize( + "how", + ["argument", "request_options", "client_headers"], + ) + @pytest.mark.parametrize("model", ["transcribe-2-pro", "transcribe-1-pro"]) + def test_any_model_silences_it(self, how, model): + headers = {"Model": model} if how == "client_headers" else None + client = ASRClient(_asr_wrapper(_sync_wrapper, headers)) + kwargs: dict[str, Any] = {} + if how == "argument": + kwargs["model"] = model + elif how == "request_options": + kwargs["request_options"] = RequestOptions( + additional_headers={"Model": model} + ) + with warnings.catch_warnings(): + warnings.simplefilter("error") + with pytest.raises(_Stop): + client.transcribe(audio=b"audio", **kwargs) + + @pytest.mark.parametrize("client_model", [None, "transcribe-2-pro"]) + @pytest.mark.parametrize("how", ["argument", "request_options"]) + def test_empty_model_warns_whatever_the_client_default(self, how, client_model): + # httpx sends the request's empty header in place of the client's, + # so the request carries no model either way. + headers = {"Model": client_model} if client_model else None + client = ASRClient(_asr_wrapper(_sync_wrapper, headers)) + kwargs: dict[str, Any] = ( + {"model": ""} + if how == "argument" + else {"request_options": RequestOptions(additional_headers={"model": ""})} + ) + with warnings.catch_warnings(record=True) as record: + warnings.simplefilter("always") + with pytest.raises(_Stop): + client.transcribe(audio=b"audio", **kwargs) + assert [str(w.message) for w in _deprecation_warnings(record)] == [ + NO_MODEL_MESSAGE + ] + + def test_argument_wins_over_an_empty_request_options_header(self): + client = ASRClient(_asr_wrapper(_sync_wrapper)) + with warnings.catch_warnings(): + warnings.simplefilter("error") + with pytest.raises(_Stop): + client.transcribe( + audio=b"audio", + model="transcribe-2-pro", + request_options=RequestOptions(additional_headers={"model": ""}), + ) + + def test_empty_client_header_warns(self): + client = ASRClient(_asr_wrapper(_sync_wrapper, {"Model": ""})) + with warnings.catch_warnings(record=True) as record: + warnings.simplefilter("always") + with pytest.raises(_Stop): + client.transcribe(audio=b"audio") + assert len(_deprecation_warnings(record)) == 1 + + def test_named_transcribe_1_keeps_its_own_notice_only(self): + client = ASRClient(_asr_wrapper(_sync_wrapper)) + with warnings.catch_warnings(record=True) as record: + warnings.simplefilter("always") + with pytest.raises(_Stop): + client.transcribe(audio=b"audio", model="transcribe-1") + with pytest.raises(_Stop): + client.transcribe(audio=b"audio") + assert [str(w.message) for w in _deprecation_warnings(record)] == [ + TRANSCRIBE_1_MESSAGE, + NO_MODEL_MESSAGE, + ] + + def _line() -> int: """The line number of the caller of this function.""" return sys._getframe(1).f_lineno