#WebSocket Continue Flag Issue with Slow Sequential Messages

1 messages · Page 1 of 1 (latest)

loud moat
#

Issue Description

When using the Cartesia WebSocket API to send multiple sentences sequentially with delays between messages (e.g., 5 seconds), the API prematurely returns response.done = true despite the continue: true flag being set in the message.

Reproduction Steps

  • Connect to Cartesia WebSocket API
  • Send multiple sentences sequentially with continue: true
  • Add a delay between sentences (e.g., 5 seconds timeout)

Expected Behavior

  • response.done should only be true after the last sentence is processed

Actual Behavior

  • response.done = true is received prematurely
  • Subsequent timestamps received after the "timeout" are restarted from timing 0

Context

In my project, I'm integrating Cartesia with an LLM that generates text in real-time. The LLM takes time to generate each sentence, causing natural delays between WebSocket messages.

#

Here is a code example to reproduce the problem:

import Cartesia from "@cartesia/cartesia-js";

const text = [
    "Sonic and Yoshi team up in a dimension-hopping adventure! ",
    "Racing through twisting zones",
];

const voice_id = "87748186-23bb-4158-a1eb-332911b0b708";
const model_id = "sonic-english";
const id = Math.floor(Math.random() * 1000).toString();
const context_id = "test-context-" + id;

const cartesia = new Cartesia({
    apiKey: process.env.CARTESIA_API_KEY,
});

async function runTextToSpeech() {
        const ws = cartesia.tts.websocket({
            container: "raw",
            encoding: "pcm_f32le",
            sampleRate: 44100,
        });

        await ws.connect();

        const send = async (sentence, isFirst = false) => {
            const config = {
                context_id: context_id,
                model_id: model_id,
                voice: {
                    mode: "id",
                    id: voice_id,
                },
                transcript: sentence,
                add_timestamps: true,
                continue: true,
            };

            if (isFirst) {
                const res = await ws.send(config);
                res.on("message", (response) => {
                    var res = JSON.parse(response.toString());
                    if (res.type == "timestamps")
                        console.log(`Ts: `, JSON.stringify(res.word_timestamps));
                    if (res.done != false)
                        console.log(`Is done: `, JSON.stringify(res));
                });
            } else {
                await ws.continue(config);
            }
            await new Promise(resolve => setTimeout(resolve, 6000));
        };

        for (let i = 0; i < text.length; i++) {
            await send(text[i], i === 0);
        }

        await new Promise(resolve => setTimeout(resolve, 20000));
        await ws.close();
}

runTextToSpeech().catch(console.error);