Server side fallback doesn't output msg with data.type = 'fallback_activated'
Bug Description
According to PR, when server side fallback happens, the msg with data.type = fallback_activated should be output (https://github.com/livekit/agents/blob/8b202a2abc49db9339b2e4ae3639293e58aeabfc/livekit-agents/livekit/agents/inference/tts.py#L877)) and finally tts event fallback_activated (https://github.com/livekit/agents/blob/8b202a2abc49db9339b2e4ae3639293e58aeabfc/livekit-agents/livekit/agents/inference/tts.py#L924) will be emitted.
However, when we try to force server side fallback happen, we find that it doesn’t output msg with data.type = “fallback_activated” and server side fallback does happen ( output audio does not come from primary model).We suspect there is a mismatch between the public livekit/agent repo and internal livekit server repo. Can you fix it since we need to observe tts fallback behavior by listening to tts event fallback_activated?
Expected Behavior
When server side fallback happens, msg with data.type = 'fallback_activated' should be output and finally tts event fallback_activated should be emitted.
Reproduction Steps
1. Simulate server side fallback through setting tts under api connection config with very short timeout (0.01) and without retry like below:
"""
tts = inference.TTS(
model="xai/tts-1",
extra_kwargs={"text_normalization": False, "optimize_streaming_latency": 1},
fallback=[
inference.tts.FallbackModel(model="inworld/inworld-tts-2", voice="Asuka"),
], ,
conn_options=APIConnectOptions(timeout=0.001, max_retry=0)
)
"""
2. Under the source code "msg = await connection.receive(timeout=self._conn_options.timeout)"(https://github.com/livekit/agents/blob/8b202a2abc49db9339b2e4ae3639293e58aeabfc/livekit-agents/livekit/agents/inference/tts.py#L877), add "logger.warning("try to get msg %s", msg)"
3. start agent and dispatch it to a room
4. talk with agent multiple turn and listen to its voice
You will find the agent is the fallback model but None of those logs with "try to get msg %s" indicate msg contain `data.type` = 'fallback_activated'
Operating System
macOS, linux
Models Used
xai/tts-1, inworld/inworld-tts-2
Package Versions
livekit~=1.1,
livekit-agents[azure,openai,turn-detector,silero,elevenlabs]==1.8.13,
livekit-api~=1.1",
livekit-plugins-noise-cancellation~=0.2.0
Session/Room/Call IDs
room_session_id: RM_jaJQ9DXWGK6J
Proposed Solution
Check the internal livekit server repo to see whether it is consistent with livekit/agent repo
Additional Context
No response
Screenshots and Recordings
No response
I checked the client side of this on 1.8.13 and on current main (a644d59ee), and it handles the notice correctly whenever one arrives, so this looks like the gateway not sending fallback_activated rather than anything in this repo.
What I ran: a local WebSocket server standing in for the gateway, driven by inference.TTS with your exact constructor (xai/tts-1, inworld/inworld-tts-2 / Asuka fallback, APIConnectOptions(timeout=0.001, max_retry=0)), three turns over one pooled connection. The fake gateway sends a fallback_activated frame each turn.
- Notice sent mid-turn (after
session.flush, before audio): onefallback_activatedevent per turn, 3 of 3, with provider/model/voice/cause filled in. Same result on the 1.8.13 tag. - Notice sent right after
done: still emitted, one turn late (it waits in the connection inbox until the next turn reads it). - Notice sent before any turn, right after
session.created: emitted on the first turn.
The session.create the client sends for your config is:
{"model": "xai/tts-1",
"fallback": {"models": [{"model": "inworld/inworld-tts-2", "voice": "Asuka", "extra": {}}]},
"connection": {"timeout": 0.001, "retries": 0}}
So the fallback chain and the 1 ms timeout do reach the gateway, and the timeout=0.001 only applies there: the client's own receive timeout comes from the stream's conn_options, not the constructor's.
Your log at connection.receive sees every text frame except session.handoff.request (the reader at inference/tts.py:703 forwards everything else to the inbox), so if no frame with type: fallback_activated shows up there, the gateway never sent one. #7185 lists a coordinated gateway change (agent-gateway#1363); a maintainer would need to confirm whether that is live for the endpoint your project uses, and whether the gateway emits the notice when the fallback is triggered by the primary's connection timeout. The event is documented as firing when "a fallback model produced content-bearing synthesis output", which your case should meet since you hear the fallback voice.
Related, not a duplicate: #8064 (observability of which model served, already linked above).
@ayushmnnit Thank for the fast response and investigation! What is your next action? It is better to check gateway part in the internal livekit server repo. Actual server side inference related source code is in that internal repo. Some details about the server side fallback are quite mystery and Official document (https://docs.livekit.io/agents/logic/fallback-strategies/#:~:text=Not%20supported-,Inference%20Fallback%20Adapter,-If%20you%20use) doesn't mention it in details. It will be great if you can make that public so that we can check it easily.
I'm an outside contributor, so I can't see the gateway repo either, and making it public isn't something I can do. A LiveKit maintainer would need to pick up the server side.
What would help them: a log of the raw frames on one affected turn. The reader in inference/tts.py passes every frame except session.handoff.request to the stream, so logging each message there with its type will show for certain whether a fallback_activated frame ever arrives. If the list shows the fallback voice's audio but no notice, that is the gateway, and the session_id from that log is what they'd need to trace it.
Thanks for the detailed repro, and thanks for the client-side check above. It was right: the SDK handles the notice correctly, and the frame wasn't being sent.
The fallback_activated notice for models in your fallback list was being rolled out gradually, so most sessions got the fallback audio without the notice. As of today it's enabled for all sessions. When a model from your fallback list takes over, inference.TTS should now emit fallback_activated with the provider, model, voice and cause.
If you rerun your repro and still don't see the event, please reply here with the time of the test and the session_id from the logs, and we'll trace it.
@russellmartin-livekit Thank for the quick reply! We tested it again and find that msg with data.type = “fallback_activated” does output now in the beginning (just once) and audio does not come from primary model. However, if we keep talking with the agent and the agent reply in the next turn, audio does not come from primary model but there is no msg with data.type = “fallback_activated” output anymore.
Can you confirm which server side tts fallback behavior below actually happen in multiple turns?
- try to connect to primary model in the 1st turn-> fallback to next model in the 1st turn -> agent generate reply with fallback model in the 1st turn -> user reply in the 1st turn -> try to connect to primary model again in the 2nd turn -> fallback to next model in the 2nd turn -> agent generate reply with fallback model in the 2nd turn -> ...
or
- try to connect to primary model in the 1st turn-> fallback to next model in the 1st turn -> agent generate reply with fallback model in the 1st turn -> user reply in the 1st turn -> agent keep stay with fallback model without fallback happen in the 2nd turn -> agent generate reply with fallback model in the 2nd turn -> ...
The room id for my test is: "RM_KyFUppAPcTt4" You can check it
@russellmartin-livekit cc:@ayushmnnit Do you have any update for our concern?