Skip to content

(fix): fix sarvam tts language switching#588

Open
yugesh-ganipudi wants to merge 1 commit intojuspay:releasefrom
yugesh-ganipudi:fix-sarvam-tts-language-switching
Open

(fix): fix sarvam tts language switching#588
yugesh-ganipudi wants to merge 1 commit intojuspay:releasefrom
yugesh-ganipudi:fix-sarvam-tts-language-switching

Conversation

@yugesh-ganipudi
Copy link
Contributor

@yugesh-ganipudi yugesh-ganipudi commented Feb 23, 2026

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue with the TTS service where automatic language switching was incorrectly overriding the user's selected language. The service now preserves the current language setting instead of switching to detected languages.

Copilot AI review requested due to automatic review settings February 23, 2026 11:43
@coderabbitai
Copy link

coderabbitai bot commented Feb 23, 2026

Walkthrough

The change modifies the _switch_language_if_needed function to retain the current language instead of switching to a newly detected language when a language mismatch is identified. The function now sets the target language to the current language and updates corresponding log messages accordingly.

Changes

Cohort / File(s) Summary
Language Switching Logic
app/ai/voice/tts/sarvam.py
Modified _switch_language_if_needed to assign the current language code instead of the newly detected language code to target_language_code, with updated log messages reflecting this behavior change.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Poem

🐰 The language stays put, no more chasing,
When detection whispers, we're not replacing,
Current tongue holds steady and true,
One file, three lines, and our preference shines through! 🗣️

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: fixing language switching behavior in the Sarvam TTS module.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_code assignment 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.

Comment on lines 168 to +176
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}")
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Copilot uses AI. Check for mistakes.
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 | 🟠 Major

No-op assignment triggers an unnecessary _send_config() call on every language mismatch.

current_lang_code is read directly from self._settings["target_language_code"] at line 166, so the reassignment on line 172 is a no-op — the dict value is already current_lang_code. _send_config() is then invoked with an unchanged config on every detected mismatch, which is a wasted async (network) call. Because run_tts fires 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).

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 614bb8f and 73bae0f.

📒 Files selected for processing (1)
  • app/ai/voice/tts/sarvam.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants