"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from .basesdk import BaseSDK
from enum import Enum
from r9s import errors, models, utils
from r9s._hooks import HookContext
from r9s.types import OptionalNullable, UNSET
from r9s.utils.unmarshal_json_response import unmarshal_json_response
from typing import Any, List, Mapping, Optional, Union


class SpeechAcceptEnum(str, Enum):
    AUDIO_AAC = "audio/aac"
    AUDIO_FLAC = "audio/flac"
    AUDIO_MPEG = "audio/mpeg"
    AUDIO_OPUS = "audio/opus"
    AUDIO_PCM = "audio/pcm"
    AUDIO_WAV = "audio/wav"


class TranscribeAcceptEnum(str, Enum):
    APPLICATION_JSON = "application/json"
    TEXT_PLAIN = "text/plain"
    APPLICATION_X_SUBRIP = "application/x-subrip"
    TEXT_VTT = "text/vtt"


class TranslateAcceptEnum(str, Enum):
    APPLICATION_JSON = "application/json"
    TEXT_PLAIN = "text/plain"


class AudioSDK(BaseSDK):
    def speech(
        self,
        *,
        model: str,
        input: str,
        voice: models.AudioSpeechRequestVoice,
        response_format: Optional[models.AudioSpeechRequestResponseFormat] = "mp3",
        speed: Optional[float] = 1,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        accept_header_override: Optional[SpeechAcceptEnum] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.CreateAudioSpeechResponse:
        r"""Text to speech

        Convert text to speech

        :param model: TTS model name
        :param input: Text to convert to speech
        :param voice: Voice type
        :param response_format:
        :param speed: Speech speed
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param accept_header_override: Override the default accept header for this method
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.AudioSpeechRequest(
            model=model,
            input=input,
            voice=voice,
            response_format=response_format,
            speed=speed,
        )

        req = self._build_request(
            method="POST",
            path="/audio/speech",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=True,
            request_has_path_params=False,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value=accept_header_override.value
            if accept_header_override is not None
            else "audio/aac;q=1, audio/flac;q=0.8, audio/mpeg;q=0.7, audio/opus;q=0.5, audio/pcm;q=0.3, audio/wav;q=0",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            get_serialized_body=lambda: utils.serialize_request_body(
                request, False, False, "json", models.AudioSpeechRequest
            ),
            allow_empty_value=None,
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = self.do_request(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="createAudioSpeech",
                oauth2_scopes=None,
                security_source=self.sdk_configuration.security,
            ),
            request=req,
            error_status_codes=[
                "400",
                "401",
                "403",
                "422",
                "429",
                "4XX",
                "500",
                "503",
                "5XX",
            ],
            stream=True,
            retry_config=retry_config,
        )

        response_data: Any = None
        if utils.match_response(http_res, "200", "audio/mpeg"):
            return http_res
        if utils.match_response(http_res, "200", "audio/opus"):
            return http_res
        if utils.match_response(http_res, "200", "audio/aac"):
            return http_res
        if utils.match_response(http_res, "200", "audio/flac"):
            return http_res
        if utils.match_response(http_res, "200", "audio/wav"):
            return http_res
        if utils.match_response(http_res, "200", "audio/pcm"):
            return http_res
        if utils.match_response(http_res, "400", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.BadRequestErrorData, http_res, http_res_text
            )
            raise errors.BadRequestError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "401", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.AuthenticationErrorData, http_res, http_res_text
            )
            raise errors.AuthenticationError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "403", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.PermissionDeniedErrorData, http_res, http_res_text
            )
            raise errors.PermissionDeniedError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "422", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.UnprocessableEntityErrorData, http_res, http_res_text
            )
            raise errors.UnprocessableEntityError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "429", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.RateLimitErrorData, http_res, http_res_text
            )
            raise errors.RateLimitError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "500", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.InternalServerErrorData, http_res, http_res_text
            )
            raise errors.InternalServerError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "503", "application/json"):
            http_res_text = utils.stream_to_text(http_res)
            response_data = unmarshal_json_response(
                errors.ServiceUnavailableErrorData, http_res, http_res_text
            )
            raise errors.ServiceUnavailableError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "4XX", "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise errors.R9SDefaultError("API error occurred", http_res, http_res_text)
        if utils.match_response(http_res, "5XX", "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise errors.R9SDefaultError("API error occurred", http_res, http_res_text)

        http_res_text = utils.stream_to_text(http_res)
        raise errors.R9SDefaultError(
            "Unexpected response received", http_res, http_res_text
        )

    async def speech_async(
        self,
        *,
        model: str,
        input: str,
        voice: models.AudioSpeechRequestVoice,
        response_format: Optional[models.AudioSpeechRequestResponseFormat] = "mp3",
        speed: Optional[float] = 1,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        accept_header_override: Optional[SpeechAcceptEnum] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.CreateAudioSpeechResponse:
        r"""Text to speech

        Convert text to speech

        :param model: TTS model name
        :param input: Text to convert to speech
        :param voice: Voice type
        :param response_format:
        :param speed: Speech speed
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param accept_header_override: Override the default accept header for this method
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.AudioSpeechRequest(
            model=model,
            input=input,
            voice=voice,
            response_format=response_format,
            speed=speed,
        )

        req = self._build_request_async(
            method="POST",
            path="/audio/speech",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=True,
            request_has_path_params=False,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value=accept_header_override.value
            if accept_header_override is not None
            else "audio/aac;q=1, audio/flac;q=0.8, audio/mpeg;q=0.7, audio/opus;q=0.5, audio/pcm;q=0.3, audio/wav;q=0",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            get_serialized_body=lambda: utils.serialize_request_body(
                request, False, False, "json", models.AudioSpeechRequest
            ),
            allow_empty_value=None,
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = await self.do_request_async(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="createAudioSpeech",
                oauth2_scopes=None,
                security_source=self.sdk_configuration.security,
            ),
            request=req,
            error_status_codes=[
                "400",
                "401",
                "403",
                "422",
                "429",
                "4XX",
                "500",
                "503",
                "5XX",
            ],
            stream=True,
            retry_config=retry_config,
        )

        response_data: Any = None
        if utils.match_response(http_res, "200", "audio/mpeg"):
            return http_res
        if utils.match_response(http_res, "200", "audio/opus"):
            return http_res
        if utils.match_response(http_res, "200", "audio/aac"):
            return http_res
        if utils.match_response(http_res, "200", "audio/flac"):
            return http_res
        if utils.match_response(http_res, "200", "audio/wav"):
            return http_res
        if utils.match_response(http_res, "200", "audio/pcm"):
            return http_res
        if utils.match_response(http_res, "400", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.BadRequestErrorData, http_res, http_res_text
            )
            raise errors.BadRequestError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "401", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.AuthenticationErrorData, http_res, http_res_text
            )
            raise errors.AuthenticationError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "403", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.PermissionDeniedErrorData, http_res, http_res_text
            )
            raise errors.PermissionDeniedError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "422", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.UnprocessableEntityErrorData, http_res, http_res_text
            )
            raise errors.UnprocessableEntityError(
                response_data, http_res, http_res_text
            )
        if utils.match_response(http_res, "429", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.RateLimitErrorData, http_res, http_res_text
            )
            raise errors.RateLimitError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "500", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.InternalServerErrorData, http_res, http_res_text
            )
            raise errors.InternalServerError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "503", "application/json"):
            http_res_text = await utils.stream_to_text_async(http_res)
            response_data = unmarshal_json_response(
                errors.ServiceUnavailableErrorData, http_res, http_res_text
            )
            raise errors.ServiceUnavailableError(response_data, http_res, http_res_text)
        if utils.match_response(http_res, "4XX", "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise errors.R9SDefaultError("API error occurred", http_res, http_res_text)
        if utils.match_response(http_res, "5XX", "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise errors.R9SDefaultError("API error occurred", http_res, http_res_text)

        http_res_text = await utils.stream_to_text_async(http_res)
        raise errors.R9SDefaultError(
            "Unexpected response received", http_res, http_res_text
        )

    def transcribe(
        self,
        *,
        file: Union[models.File, models.FileTypedDict],
        model: str,
        language: Optional[str] = None,
        prompt: Optional[str] = None,
        response_format: Optional[
            models.AudioTranscriptionRequestResponseFormat
        ] = "json",
        temperature: Optional[float] = 0,
        timestamp_granularities: Optional[List[models.TimestampGranularities]] = None,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        accept_header_override: Optional[TranscribeAcceptEnum] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.CreateAudioTranscriptionResponse:
        r"""Speech to text

        Transcribe speech to text. Supports multiple models and output formats.

        **Supported models:**
        - whisper-1: Supports json, text, srt, verbose_json, vtt formats
        - gpt-4o-transcribe, gpt-4o-mini-transcribe: Only support json and text formats

        **Note:** timestamp_granularities parameter only works when response_format is set to verbose_json


        :param file: Audio file to transcribe
        :param model: Model name
        :param language: Audio language (ISO-639-1 format)
        :param prompt: Optional text prompt
        :param response_format: Output format. Model support varies:
            - whisper-1: Supports all formats (json, text, srt, verbose_json, vtt)
            - gpt-4o-transcribe, gpt-4o-mini-transcribe: Only json and text

        :param temperature:
        :param timestamp_granularities: Timestamp granularity levels to include. Options: word, segment.
            **Important:** Only works when response_format is set to verbose_json.
            Note: segment timestamps have no additional latency, but word timestamps add latency.

        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param accept_header_override: Override the default accept header for this method
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.AudioTranscriptionRequest(
            file=utils.get_pydantic_model(file, models.File),
            model=model,
            language=language,
            prompt=prompt,
            response_format=response_format,
            temperature=temperature,
            timestamp_granularities=timestamp_granularities,
        )

        req = self._build_request(
            method="POST",
            path="/audio/transcriptions",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=True,
            request_has_path_params=False,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value=accept_header_override.value
            if accept_header_override is not None
            else "application/json;q=1, text/plain;q=0.8, application/x-subrip;q=0.5, text/vtt;q=0",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            get_serialized_body=lambda: utils.serialize_request_body(
                request, False, False, "multipart", models.AudioTranscriptionRequest
            ),
            allow_empty_value=None,
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = self.do_request(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="createAudioTranscription",
                oauth2_scopes=None,
                security_source=self.sdk_configuration.security,
            ),
            request=req,
            error_status_codes=[
                "400",
                "401",
                "403",
                "422",
                "429",
                "4XX",
                "500",
                "503",
                "5XX",
            ],
            retry_config=retry_config,
        )

        response_data: Any = None
        if utils.match_response(http_res, "200", "application/json"):
            return unmarshal_json_response(models.AudioTranscriptionResponse, http_res)
        if utils.match_response(http_res, "200", "text/plain"):
            return http_res.text
        if utils.match_response(http_res, "200", "application/x-subrip"):
            return http_res.text
        if utils.match_response(http_res, "200", "text/vtt"):
            return http_res.text
        if utils.match_response(http_res, "400", "application/json"):
            response_data = unmarshal_json_response(
                errors.BadRequestErrorData, http_res
            )
            raise errors.BadRequestError(response_data, http_res)
        if utils.match_response(http_res, "401", "application/json"):
            response_data = unmarshal_json_response(
                errors.AuthenticationErrorData, http_res
            )
            raise errors.AuthenticationError(response_data, http_res)
        if utils.match_response(http_res, "403", "application/json"):
            response_data = unmarshal_json_response(
                errors.PermissionDeniedErrorData, http_res
            )
            raise errors.PermissionDeniedError(response_data, http_res)
        if utils.match_response(http_res, "422", "application/json"):
            response_data = unmarshal_json_response(
                errors.UnprocessableEntityErrorData, http_res
            )
            raise errors.UnprocessableEntityError(response_data, http_res)
        if utils.match_response(http_res, "429", "application/json"):
            response_data = unmarshal_json_response(errors.RateLimitErrorData, http_res)
            raise errors.RateLimitError(response_data, http_res)
        if utils.match_response(http_res, "500", "application/json"):
            response_data = unmarshal_json_response(
                errors.InternalServerErrorData, http_res
            )
            raise errors.InternalServerError(response_data, http_res)
        if utils.match_response(http_res, "503", "application/json"):
            response_data = unmarshal_json_response(
                errors.ServiceUnavailableErrorData, http_res
            )
            raise errors.ServiceUnavailableError(response_data, http_res)
        if utils.match_response(http_res, "4XX", "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise errors.R9SDefaultError("API error occurred", http_res, http_res_text)
        if utils.match_response(http_res, "5XX", "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise errors.R9SDefaultError("API error occurred", http_res, http_res_text)

        raise errors.R9SDefaultError("Unexpected response received", http_res)

    async def transcribe_async(
        self,
        *,
        file: Union[models.File, models.FileTypedDict],
        model: str,
        language: Optional[str] = None,
        prompt: Optional[str] = None,
        response_format: Optional[
            models.AudioTranscriptionRequestResponseFormat
        ] = "json",
        temperature: Optional[float] = 0,
        timestamp_granularities: Optional[List[models.TimestampGranularities]] = None,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        accept_header_override: Optional[TranscribeAcceptEnum] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.CreateAudioTranscriptionResponse:
        r"""Speech to text

        Transcribe speech to text. Supports multiple models and output formats.

        **Supported models:**
        - whisper-1: Supports json, text, srt, verbose_json, vtt formats
        - gpt-4o-transcribe, gpt-4o-mini-transcribe: Only support json and text formats

        **Note:** timestamp_granularities parameter only works when response_format is set to verbose_json


        :param file: Audio file to transcribe
        :param model: Model name
        :param language: Audio language (ISO-639-1 format)
        :param prompt: Optional text prompt
        :param response_format: Output format. Model support varies:
            - whisper-1: Supports all formats (json, text, srt, verbose_json, vtt)
            - gpt-4o-transcribe, gpt-4o-mini-transcribe: Only json and text

        :param temperature:
        :param timestamp_granularities: Timestamp granularity levels to include. Options: word, segment.
            **Important:** Only works when response_format is set to verbose_json.
            Note: segment timestamps have no additional latency, but word timestamps add latency.

        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param accept_header_override: Override the default accept header for this method
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.AudioTranscriptionRequest(
            file=utils.get_pydantic_model(file, models.File),
            model=model,
            language=language,
            prompt=prompt,
            response_format=response_format,
            temperature=temperature,
            timestamp_granularities=timestamp_granularities,
        )

        req = self._build_request_async(
            method="POST",
            path="/audio/transcriptions",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=True,
            request_has_path_params=False,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value=accept_header_override.value
            if accept_header_override is not None
            else "application/json;q=1, text/plain;q=0.8, application/x-subrip;q=0.5, text/vtt;q=0",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            get_serialized_body=lambda: utils.serialize_request_body(
                request, False, False, "multipart", models.AudioTranscriptionRequest
            ),
            allow_empty_value=None,
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = await self.do_request_async(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="createAudioTranscription",
                oauth2_scopes=None,
                security_source=self.sdk_configuration.security,
            ),
            request=req,
            error_status_codes=[
                "400",
                "401",
                "403",
                "422",
                "429",
                "4XX",
                "500",
                "503",
                "5XX",
            ],
            retry_config=retry_config,
        )

        response_data: Any = None
        if utils.match_response(http_res, "200", "application/json"):
            return unmarshal_json_response(models.AudioTranscriptionResponse, http_res)
        if utils.match_response(http_res, "200", "text/plain"):
            return http_res.text
        if utils.match_response(http_res, "200", "application/x-subrip"):
            return http_res.text
        if utils.match_response(http_res, "200", "text/vtt"):
            return http_res.text
        if utils.match_response(http_res, "400", "application/json"):
            response_data = unmarshal_json_response(
                errors.BadRequestErrorData, http_res
            )
            raise errors.BadRequestError(response_data, http_res)
        if utils.match_response(http_res, "401", "application/json"):
            response_data = unmarshal_json_response(
                errors.AuthenticationErrorData, http_res
            )
            raise errors.AuthenticationError(response_data, http_res)
        if utils.match_response(http_res, "403", "application/json"):
            response_data = unmarshal_json_response(
                errors.PermissionDeniedErrorData, http_res
            )
            raise errors.PermissionDeniedError(response_data, http_res)
        if utils.match_response(http_res, "422", "application/json"):
            response_data = unmarshal_json_response(
                errors.UnprocessableEntityErrorData, http_res
            )
            raise errors.UnprocessableEntityError(response_data, http_res)
        if utils.match_response(http_res, "429", "application/json"):
            response_data = unmarshal_json_response(errors.RateLimitErrorData, http_res)
            raise errors.RateLimitError(response_data, http_res)
        if utils.match_response(http_res, "500", "application/json"):
            response_data = unmarshal_json_response(
                errors.InternalServerErrorData, http_res
            )
            raise errors.InternalServerError(response_data, http_res)
        if utils.match_response(http_res, "503", "application/json"):
            response_data = unmarshal_json_response(
                errors.ServiceUnavailableErrorData, http_res
            )
            raise errors.ServiceUnavailableError(response_data, http_res)
        if utils.match_response(http_res, "4XX", "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise errors.R9SDefaultError("API error occurred", http_res, http_res_text)
        if utils.match_response(http_res, "5XX", "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise errors.R9SDefaultError("API error occurred", http_res, http_res_text)

        raise errors.R9SDefaultError("Unexpected response received", http_res)

    def translate(
        self,
        *,
        file: Union[
            models.AudioTranslationRequestFile,
            models.AudioTranslationRequestFileTypedDict,
        ],
        model: str,
        prompt: Optional[str] = None,
        response_format: Optional[
            models.AudioTranslationRequestResponseFormat
        ] = "json",
        temperature: Optional[float] = 0,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        accept_header_override: Optional[TranslateAcceptEnum] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.CreateAudioTranslationResponse:
        r"""Speech translation

        Translate speech from any supported language to English text.

        **Important:** This endpoint only translates audio into English. The source language is automatically detected by the model.

        **Supported models:** whisper-1 (primary), gpt-4o-transcribe (extended support)


        :param file: Audio file to translate to English
        :param model: Model name (whisper-1 is primary, gpt-4o-transcribe has extended support)
        :param prompt: Optional text prompt to guide the model's style.
            The source language can be specified in the prompt if needed, though the model will auto-detect it.

        :param response_format: Output format for the translated text
        :param temperature: Sampling temperature between 0 and 1
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param accept_header_override: Override the default accept header for this method
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.AudioTranslationRequest(
            file=utils.get_pydantic_model(file, models.AudioTranslationRequestFile),
            model=model,
            prompt=prompt,
            response_format=response_format,
            temperature=temperature,
        )

        req = self._build_request(
            method="POST",
            path="/audio/translations",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=True,
            request_has_path_params=False,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value=accept_header_override.value
            if accept_header_override is not None
            else "application/json;q=1, text/plain;q=0",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            get_serialized_body=lambda: utils.serialize_request_body(
                request, False, False, "multipart", models.AudioTranslationRequest
            ),
            allow_empty_value=None,
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = self.do_request(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="createAudioTranslation",
                oauth2_scopes=None,
                security_source=self.sdk_configuration.security,
            ),
            request=req,
            error_status_codes=[
                "400",
                "401",
                "403",
                "422",
                "429",
                "4XX",
                "500",
                "503",
                "5XX",
            ],
            retry_config=retry_config,
        )

        response_data: Any = None
        if utils.match_response(http_res, "200", "application/json"):
            return unmarshal_json_response(models.AudioTranslationResponse, http_res)
        if utils.match_response(http_res, "200", "text/plain"):
            return http_res.text
        if utils.match_response(http_res, "400", "application/json"):
            response_data = unmarshal_json_response(
                errors.BadRequestErrorData, http_res
            )
            raise errors.BadRequestError(response_data, http_res)
        if utils.match_response(http_res, "401", "application/json"):
            response_data = unmarshal_json_response(
                errors.AuthenticationErrorData, http_res
            )
            raise errors.AuthenticationError(response_data, http_res)
        if utils.match_response(http_res, "403", "application/json"):
            response_data = unmarshal_json_response(
                errors.PermissionDeniedErrorData, http_res
            )
            raise errors.PermissionDeniedError(response_data, http_res)
        if utils.match_response(http_res, "422", "application/json"):
            response_data = unmarshal_json_response(
                errors.UnprocessableEntityErrorData, http_res
            )
            raise errors.UnprocessableEntityError(response_data, http_res)
        if utils.match_response(http_res, "429", "application/json"):
            response_data = unmarshal_json_response(errors.RateLimitErrorData, http_res)
            raise errors.RateLimitError(response_data, http_res)
        if utils.match_response(http_res, "500", "application/json"):
            response_data = unmarshal_json_response(
                errors.InternalServerErrorData, http_res
            )
            raise errors.InternalServerError(response_data, http_res)
        if utils.match_response(http_res, "503", "application/json"):
            response_data = unmarshal_json_response(
                errors.ServiceUnavailableErrorData, http_res
            )
            raise errors.ServiceUnavailableError(response_data, http_res)
        if utils.match_response(http_res, "4XX", "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise errors.R9SDefaultError("API error occurred", http_res, http_res_text)
        if utils.match_response(http_res, "5XX", "*"):
            http_res_text = utils.stream_to_text(http_res)
            raise errors.R9SDefaultError("API error occurred", http_res, http_res_text)

        raise errors.R9SDefaultError("Unexpected response received", http_res)

    async def translate_async(
        self,
        *,
        file: Union[
            models.AudioTranslationRequestFile,
            models.AudioTranslationRequestFileTypedDict,
        ],
        model: str,
        prompt: Optional[str] = None,
        response_format: Optional[
            models.AudioTranslationRequestResponseFormat
        ] = "json",
        temperature: Optional[float] = 0,
        retries: OptionalNullable[utils.RetryConfig] = UNSET,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        accept_header_override: Optional[TranslateAcceptEnum] = None,
        http_headers: Optional[Mapping[str, str]] = None,
    ) -> models.CreateAudioTranslationResponse:
        r"""Speech translation

        Translate speech from any supported language to English text.

        **Important:** This endpoint only translates audio into English. The source language is automatically detected by the model.

        **Supported models:** whisper-1 (primary), gpt-4o-transcribe (extended support)


        :param file: Audio file to translate to English
        :param model: Model name (whisper-1 is primary, gpt-4o-transcribe has extended support)
        :param prompt: Optional text prompt to guide the model's style.
            The source language can be specified in the prompt if needed, though the model will auto-detect it.

        :param response_format: Output format for the translated text
        :param temperature: Sampling temperature between 0 and 1
        :param retries: Override the default retry configuration for this method
        :param server_url: Override the default server URL for this method
        :param timeout_ms: Override the default request timeout configuration for this method in milliseconds
        :param accept_header_override: Override the default accept header for this method
        :param http_headers: Additional headers to set or replace on requests.
        """
        base_url = None
        url_variables = None
        if timeout_ms is None:
            timeout_ms = self.sdk_configuration.timeout_ms

        if server_url is not None:
            base_url = server_url
        else:
            base_url = self._get_url(base_url, url_variables)

        request = models.AudioTranslationRequest(
            file=utils.get_pydantic_model(file, models.AudioTranslationRequestFile),
            model=model,
            prompt=prompt,
            response_format=response_format,
            temperature=temperature,
        )

        req = self._build_request_async(
            method="POST",
            path="/audio/translations",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=True,
            request_has_path_params=False,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value=accept_header_override.value
            if accept_header_override is not None
            else "application/json;q=1, text/plain;q=0",
            http_headers=http_headers,
            security=self.sdk_configuration.security,
            get_serialized_body=lambda: utils.serialize_request_body(
                request, False, False, "multipart", models.AudioTranslationRequest
            ),
            allow_empty_value=None,
            timeout_ms=timeout_ms,
        )

        if retries == UNSET:
            if self.sdk_configuration.retry_config is not UNSET:
                retries = self.sdk_configuration.retry_config

        retry_config = None
        if isinstance(retries, utils.RetryConfig):
            retry_config = (retries, ["429", "500", "502", "503", "504"])

        http_res = await self.do_request_async(
            hook_ctx=HookContext(
                config=self.sdk_configuration,
                base_url=base_url or "",
                operation_id="createAudioTranslation",
                oauth2_scopes=None,
                security_source=self.sdk_configuration.security,
            ),
            request=req,
            error_status_codes=[
                "400",
                "401",
                "403",
                "422",
                "429",
                "4XX",
                "500",
                "503",
                "5XX",
            ],
            retry_config=retry_config,
        )

        response_data: Any = None
        if utils.match_response(http_res, "200", "application/json"):
            return unmarshal_json_response(models.AudioTranslationResponse, http_res)
        if utils.match_response(http_res, "200", "text/plain"):
            return http_res.text
        if utils.match_response(http_res, "400", "application/json"):
            response_data = unmarshal_json_response(
                errors.BadRequestErrorData, http_res
            )
            raise errors.BadRequestError(response_data, http_res)
        if utils.match_response(http_res, "401", "application/json"):
            response_data = unmarshal_json_response(
                errors.AuthenticationErrorData, http_res
            )
            raise errors.AuthenticationError(response_data, http_res)
        if utils.match_response(http_res, "403", "application/json"):
            response_data = unmarshal_json_response(
                errors.PermissionDeniedErrorData, http_res
            )
            raise errors.PermissionDeniedError(response_data, http_res)
        if utils.match_response(http_res, "422", "application/json"):
            response_data = unmarshal_json_response(
                errors.UnprocessableEntityErrorData, http_res
            )
            raise errors.UnprocessableEntityError(response_data, http_res)
        if utils.match_response(http_res, "429", "application/json"):
            response_data = unmarshal_json_response(errors.RateLimitErrorData, http_res)
            raise errors.RateLimitError(response_data, http_res)
        if utils.match_response(http_res, "500", "application/json"):
            response_data = unmarshal_json_response(
                errors.InternalServerErrorData, http_res
            )
            raise errors.InternalServerError(response_data, http_res)
        if utils.match_response(http_res, "503", "application/json"):
            response_data = unmarshal_json_response(
                errors.ServiceUnavailableErrorData, http_res
            )
            raise errors.ServiceUnavailableError(response_data, http_res)
        if utils.match_response(http_res, "4XX", "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise errors.R9SDefaultError("API error occurred", http_res, http_res_text)
        if utils.match_response(http_res, "5XX", "*"):
            http_res_text = await utils.stream_to_text_async(http_res)
            raise errors.R9SDefaultError("API error occurred", http_res, http_res_text)

        raise errors.R9SDefaultError("Unexpected response received", http_res)
