CosyVoice server events

Updated at:

Server event reference for CosyVoice: task startup, sentence synthesis, completion, failure, and corresponding requests.

Event flow

  1. Send run-task to start and configure the task, then wait for task-started.
  2. After receiving task-started, send text through continue-task while receiving result-generated events and synthesized audio.
  3. After sending all text, send finish-task and keep receiving remaining results and audio until task-finished arrives. A failed task returns task-failed.

result-generated returns sentence metadata. Audio is transmitted separately as binary frames over WebSocket or through an Audio Track over AOQ. For connection setup, authentication, and audio transport, see WebSocket API or AOQ access.

task-started

Indicates that the task has started successfully and the client can begin sending text for synthesis.

ImportantWait for task-started before sending continue-task. run-task only starts and configures the task. Send the text to synthesize through continue-task.

headerobject
task_idstring

The task ID generated by the client.

eventstring

Event type. Fixed value: task-started.

payloadobject

Empty object.

{
  "header": {
    "action": "run-task",
    "task_id": "b6e6cdf6-e9fa-429c-b1c1-cd231977c918",
    "streaming": "duplex"
  },
  "payload": {
    "input": {},
    "task_group": "audio",
    "task": "tts",
    "function": "SpeechSynthesizer",
    "model": "cosyvoice-v3-plus",
    "parameters": {
      "text_type": "PlainText",
      "voice": "longanyang",
      "format": "mp3",
      "sample_rate": 22050
    }
  }
}
{
  "header": {
    "task_id": "b6e6cdf6-e9fa-429c-b1c1-cd231977c918",
    "task_uuid": "1dd2b10b644e45b798f58ef96405215e",
    "event": "task-started",
    "attributes": {}
  },
  "payload": {}
}

result-generated

Returns sentence metadata, including sentence starts, audio chunk markers, and sentence ends.

Importantsentence-end marks the completion of a sentence, not the entire task. The task ends when task-finished arrives. Audio is returned through WebSocket binary frames or an AOQ Audio Track.

headerobject
task_idstring

The task ID generated by the client.

eventstring

Event type. Fixed value: result-generated.

payloadobject
outputobject

Output information.

sentenceobject
indexinteger

Sentence index, starting from 0.

wordsarray

Word-level timestamp array.

textstring

Text content of the word.

begin_indexinteger

Start character index of the word within the sentence. Starts at 0.

end_indexinteger

End character index of the word within the sentence. Starts at 1.

begin_timeinteger

Start time of the word's corresponding audio, in milliseconds.

end_timeinteger

End time of the word's corresponding audio, in milliseconds.

typestring

Sub-event type. Valid values:

  • sentence-begin: Sentence start. Returns the text to be synthesized.
  • sentence-synthesis: Marks an audio frame. One audio frame is transmitted over the WebSocket binary channel immediately after each event.
  • sentence-end: Sentence end. Returns the text content and cumulative character count.
original_textstring

Text of the sentence as segmented for synthesis.

usageobject

Billing information, returned in the sentence-end event.

Noteusage contains cumulative usage for the current task. Do not add usage values across events. After the task completes, use the final cumulative usage in task-finished.

charactersinteger

Cumulative number of billed characters.

The following examples set word_timestamp_enabled=true when starting the task.

sentence-begin

{
  "header": {
    "action": "continue-task",
    "task_id": "3785722c-8fb0-42cf-9fbe-21dd8d8c29bf",
    "streaming": "duplex"
  },
  "payload": {
    "input": {
      "text": "Welcome to the speech synthesis service."
    }
  }
}
{
  "header": {
    "task_id": "3785722c-8fb0-42cf-9fbe-21dd8d8c29bf",
    "event": "result-generated",
    "attributes": {
      "x-dashscope-inner-response-type": "ws"
    }
  },
  "payload": {
    "output": {
      "sentence": {
        "index": 0,
        "words": [
          {
            "text": "Welcome",
            "begin_index": 0,
            "end_index": 1,
            "begin_time": 0,
            "end_time": 560
          }
        ]
      },
      "type": "sentence-begin",
      "original_text": "Welcome to the speech synthesis service.",
      "normalized_text": "Welcome to the speech synthesis service."
    }
  }
}

sentence-synthesis

{
  "header": {
    "action": "continue-task",
    "task_id": "3785722c-8fb0-42cf-9fbe-21dd8d8c29bf",
    "streaming": "duplex"
  },
  "payload": {
    "input": {
      "text": "Welcome to the speech synthesis service."
    }
  }
}
{
  "header": {
    "task_id": "3785722c-8fb0-42cf-9fbe-21dd8d8c29bf",
    "event": "result-generated",
    "attributes": {
      "x-dashscope-inner-response-type": "ws"
    }
  },
  "payload": {
    "output": {
      "sentence": {
        "index": 0,
        "words": [
          {
            "text": "Welcome",
            "begin_index": 0,
            "end_index": 1,
            "begin_time": 0,
            "end_time": 560
          }
        ]
      },
      "type": "sentence-synthesis"
    }
  }
}

sentence-end

{
  "header": {
    "action": "continue-task",
    "task_id": "3785722c-8fb0-42cf-9fbe-21dd8d8c29bf",
    "streaming": "duplex"
  },
  "payload": {
    "input": {
      "text": "Welcome to the speech synthesis service."
    }
  }
}
{
  "header": {
    "task_id": "3785722c-8fb0-42cf-9fbe-21dd8d8c29bf",
    "event": "result-generated",
    "attributes": {
      "x-dashscope-inner-response-type": "ws"
    }
  },
  "payload": {
    "output": {
      "sentence": {
        "index": 0,
        "words": [
          {
            "text": "Welcome",
            "begin_index": 0,
            "end_index": 1,
            "begin_time": 0,
            "end_time": 560
          },
          {
            "text": " to",
            "begin_index": 1,
            "end_index": 2,
            "begin_time": 680,
            "end_time": 880
          },
          {
            "text": " the",
            "begin_index": 2,
            "end_index": 3,
            "begin_time": 880,
            "end_time": 1000
          },
          {
            "text": " speech",
            "begin_index": 3,
            "end_index": 4,
            "begin_time": 1000,
            "end_time": 1080
          },
          {
            "text": " synthesis",
            "begin_index": 4,
            "end_index": 5,
            "begin_time": 1320,
            "end_time": 1720
          },
          {
            "text": " service",
            "begin_index": 5,
            "end_index": 6,
            "begin_time": 1920,
            "end_time": 2160
          },
          {
            "text": ".",
            "begin_index": 6,
            "end_index": 7,
            "begin_time": 2280,
            "end_time": 2880
          }
        ]
      },
      "type": "sentence-end",
      "original_text": "Welcome to the speech synthesis service.",
      "normalized_text": "Welcome to the speech synthesis service."
    },
    "usage": {
      "characters": 40
    }
  }
}

task-finished

The server returns a task-finished event when the task completes. The client can then close the WebSocket connection or reuse it to start a new task.

headerobject
task_idstring

The task ID generated by the client.

eventstring

Event type. Fixed value: task-finished.

payloadobject
usageobject
charactersinteger

Cumulative number of billed characters.

{
  "header": {
    "action": "finish-task",
    "task_id": "b6e6cdf6-e9fa-429c-b1c1-cd231977c918",
    "streaming": "duplex"
  },
  "payload": {
    "input": {}
  }
}
{
  "header": {
    "task_id": "b6e6cdf6-e9fa-429c-b1c1-cd231977c918",
    "event": "task-finished",
    "attributes": {
      "x-dashscope-inner-response-type": "ws"
    }
  },
  "payload": {
    "output": {},
    "usage": {
      "characters": 40
    }
  }
}

task-failed

The server returns a task-failed event when the task fails. On receiving this event, the client must close the WebSocket connection and handle the error.

headerobject
task_idstring

The task ID generated by the client.

eventstring

Event type. Fixed value: task-failed.

error_codestring

Error code.

error_messagestring

Detailed error message.

This example uses an unsupported audio format. After the task starts and text is sent, the server returns an error. The error code and message depend on the cause of failure.

{
  "header": {
    "action": "run-task",
    "task_id": "27928f57-cf3b-4f3d-a245-aef64895a3c9",
    "streaming": "duplex"
  },
  "payload": {
    "input": {},
    "task_group": "audio",
    "task": "tts",
    "function": "SpeechSynthesizer",
    "model": "cosyvoice-v3-plus",
    "parameters": {
      "text_type": "PlainText",
      "voice": "longanyang",
      "format": "invalid-format",
      "sample_rate": 22050
    }
  }
}
{
  "header": {
    "task_id": "27928f57-cf3b-4f3d-a245-aef64895a3c9",
    "event": "task-failed",
    "error_code": "InvalidParameter",
    "error_message": "Request format is invalid!"
  },
  "payload": {}
}