(fix): fix sarvam tts language switching#588
(fix): fix sarvam tts language switching#588yugesh-ganipudi wants to merge 1 commit intojuspay:releasefrom
Conversation
WalkthroughThe change modifies the Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~5 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Tip Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR targets Sarvam TTS auto language switching by modifying the logic in LanguageAwareSarvamTTS._switch_language_if_needed, which detects the script of generated text and updates Sarvam’s target_language_code accordingly.
Changes:
- Adjusted the language-switch logging and
target_language_codeassignment within_switch_language_if_needed. - Updated the post-config “Language switched to …” log message.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if new_lang_code != current_lang_code: | ||
| logger.info( | ||
| f"[SARVAM] Script detected: {detected_script} - switching {current_lang_code} to {new_lang_code}" | ||
| f"[SARVAM] Script detected: {detected_script} - switching {current_lang_code} to {current_lang_code}" | ||
| ) | ||
| self._settings["target_language_code"] = new_lang_code | ||
| self._settings["target_language_code"] = current_lang_code | ||
|
|
||
| try: | ||
| await self._send_config() | ||
| logger.info(f"[SARVAM] Language switched to {new_lang_code}") | ||
| logger.info(f"[SARVAM] Language switched to {current_lang_code}") |
There was a problem hiding this comment.
In _switch_language_if_needed, when new_lang_code != current_lang_code the code now logs switching {current_lang_code} to {current_lang_code} and sets target_language_code back to current_lang_code. This prevents any language switch and still returns True, which contradicts the method name/docstring and likely breaks auto language switching. Update the log/assignment (and the post-send log) to use new_lang_code and set self._settings["target_language_code"] = new_lang_code before _send_config().
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
app/ai/voice/tts/sarvam.py (1)
168-184:⚠️ Potential issue | 🟠 MajorNo-op assignment triggers an unnecessary
_send_config()call on every language mismatch.
current_lang_codeis read directly fromself._settings["target_language_code"]at line 166, so the reassignment on line 172 is a no-op — the dict value is alreadycurrent_lang_code._send_config()is then invoked with an unchanged config on every detected mismatch, which is a wasted async (network) call. Becauserun_ttsfires on every synthesis request, this cost accumulates whenever the detected script differs from the configured language.The log messages compound the confusion:
- Line 170:
"switching {current_lang_code} to {current_lang_code}"— both sides are identical.- Line 176:
"Language switched to {current_lang_code}"— nothing was actually switched.If the fix intent is to always retain the current language when a mismatch is detected, the entire inner block can be collapsed:
🛠️ Proposed fix
if new_lang_code != current_lang_code: logger.info( - f"[SARVAM] Script detected: {detected_script} - switching {current_lang_code} to {current_lang_code}" + f"[SARVAM] Script detected: {detected_script} ({new_lang_code}) - retaining configured language {current_lang_code}" ) - self._settings["target_language_code"] = current_lang_code - - try: - await self._send_config() - logger.info(f"[SARVAM] Language switched to {current_lang_code}") - return True - except Exception as e: - logger.warning( - f"[SARVAM] Failed to send config for language switch: {e}" - ) - return False return False🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@app/ai/voice/tts/sarvam.py` around lines 168 - 184, The code performs a no-op assignment of self._settings["target_language_code"] to current_lang_code and then calls await self._send_config() and logs a misleading "Language switched" message; update the block in sarvam.py (look for variables new_lang_code, current_lang_code, detected_script and the call to self._send_config) so that if the intent is to retain the current language you remove the no-op assignment and do not call _send_config(), instead log a clear message like "Detected script X does not match configured language Y — keeping current language" and return False; alternatively, if the intent is to switch, assign self._settings["target_language_code"] = new_lang_code, then await self._send_config() and log the new_lang_code on success (adjust messages accordingly).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@app/ai/voice/tts/sarvam.py`:
- Around line 168-184: The code performs a no-op assignment of
self._settings["target_language_code"] to current_lang_code and then calls await
self._send_config() and logs a misleading "Language switched" message; update
the block in sarvam.py (look for variables new_lang_code, current_lang_code,
detected_script and the call to self._send_config) so that if the intent is to
retain the current language you remove the no-op assignment and do not call
_send_config(), instead log a clear message like "Detected script X does not
match configured language Y — keeping current language" and return False;
alternatively, if the intent is to switch, assign
self._settings["target_language_code"] = new_lang_code, then await
self._send_config() and log the new_lang_code on success (adjust messages
accordingly).
Summary by CodeRabbit