Repository: livekit/agents
File: livekit-plugins/livekit-plugins-elevenlabs/livekit/plugins/elevenlabs/stt.py (Python)

Code before:
            if use_realtime is True:
                logger.warning(
                    "`use_realtime` parameter is deprecated. "
                    "Specify a realtime model_id to enable streaming. "
                    "Defaulting model_id to 'scribe_v2_realtime' "
                )
                model_id = "scribe_v2_realtime"
            else:
                logger.warning(
                    "`use_realtime` parameter is deprecated. Instead set model_id to determine if streaming is enabled."
                )
                if is_given(model_id) and "realtime" in model_id:
                    raise ValueError(
                        "The currently selected model is a realtime model but use_realtime is False"
                    )
        else:
            use_realtime = True if (is_given(model_id) and "realtime" in model_id) else False

        # Handle model_id defaults
        if not is_given(model_id):
            if use_realtime:
                logger.warning("model_id is not provided. Defaulting to 'scribe_v2_realtime'.")
                model_id = "scribe_v2_realtime"
            else:
                logger.warning("model_id is not provided. Defaulting to 'scribe_v1'.")
                model_id = "scribe_v1"

        super().__init__(
            capabilities=STTCapabilities(
                streaming=use_realtime,
                interim_results=True,
                aligned_transcript="word" if include_timestamps and use_realtime else False,
            )
        )

        if not use_realtime and is_given(server_vad):
            logger.warning("Server-side VAD is only supported for Scribe v2 realtime model")

        elevenlabs_api_key = api_key if is_given(api_key) else os.environ.get("ELEVEN_API_KEY")
        if not elevenlabs_api_key:
            raise ValueError(
                "ElevenLabs API key is required, either as argument or "
                "set ELEVEN_API_KEY environmental variable"
            )

        self._opts = STTOptions(
            api_key=elevenlabs_api_key,
            base_url=base_url if is_given(base_url) else API_BASE_URL_V1,
            language_code=language_code or None,
            tag_audio_events=tag_audio_events,
            sample_rate=sample_rate,

Review comment (refactor):
maybe simplify the validation
```suggestion
        if is_given(realtime_model):
            if is_given(model_id):
                logger.warning(
                    "both `use_realtime` and `model_id` parameters are provided. `use_realtime` will be ignored."
                )
            else:
                logger.warning(
                    "`use_realtime` parameter is deprecated. "
                    "Specify a realtime model_id to enable streaming. "
                    "Defaulting model_id to 'scribe_v2_realtime' "
                )
                model_id = "scribe_v2_realtime" if realtime_model else "scribe_v1"

        model_id = model_id if is_given(model_id) else "scribe_v1"
        realtime_model = model_id == "scribe_v2_realtime"
```

then use `realtime_model` for `streaming` and 
```python
        if not realtime_model and is_given(server_vad):
            logger.warning("Server-side VAD is only supported for Scribe v2 realtime model")
```

Code after:
                logger.warning(
                    "both `use_realtime` and `model_id` parameters are provided. `use_realtime` will be ignored."
                )
            else:
                logger.warning(
                    "`use_realtime` parameter is deprecated. "
                    "Specify a realtime model_id to enable streaming. "
                    "Defaulting model_id to one based on use_realtime parameter. "
                )
                model_id = "scribe_v2_realtime" if use_realtime else "scribe_v1"
        model_id = model_id if is_given(model_id) else "scribe_v1"
        use_realtime = model_id == "scribe_v2_realtime"

        if not use_realtime and is_given(server_vad):
            logger.warning("Server-side VAD is only supported for Scribe v2 realtime model")

        super().__init__(
            capabilities=STTCapabilities(
                streaming=use_realtime,
                interim_results=True,
                aligned_transcript="word" if include_timestamps and use_realtime else False,
            )
        )

        elevenlabs_api_key = api_key if is_given(api_key) else os.environ.get("ELEVEN_API_KEY")
        if not elevenlabs_api_key:
            raise ValueError(
                "ElevenLabs API key is required, either as argument or "
                "set ELEVEN_API_KEY environmental variable"
            )

        self._opts = STTOptions(
            api_key=elevenlabs_api_key,
            base_url=base_url if is_given(base_url) else API_BASE_URL_V1,
            language_code=language_code or None,
            tag_audio_events=tag_audio_events,
            sample_rate=sample_rate,
            server_vad=server_vad,
            include_timestamps=include_timestamps,
            model_id=model_id,
        )
        self._session = http_session
        self._streams = weakref.WeakSet[SpeechStream]()

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

    @property
    def provider(self) -> str:
        return "ElevenLabs"