'model-output' server message output is not in seq...
# support
w
Hi team, so I wanted to use model-output webhook server message to do some background processing, so I implemented a buffer and everything, but when I try it out it doesn't give me the correct sentence, it will be scrambled like this:
Copy code
glad screen now looking really that hear.I'm I'm to forward you about how has over changed the. image an have I I years to here with — like'd should you share you chatting What on your it. notice you to this image do in? dancing that with see
can I get the sequence index somehow?
this is a snippet of simplified code that I'm using:
Copy code
private modelOutputBuffer: Map<string, string> = new Map();
  private modelOutputTimers: Map<string, NodeJS.Timeout> = new Map();

  private GAP_MS = 250;

  /**
   * Main handler for Vapi server messages.
   */
  async handleServerMessage(
    call: InferResultType<'call'>,
    data: Vapi.ServerMessage,
  ): Promise<void> {
    switch (data.message.type) {
      case 'model-output':
        return this.handleModelOutputUpdate(call, data.message);
      default:
        return;
    }
  }

  getModelOutputMessageSync(obj: Record<string, unknown>): string {
    let output = '';
    for (const [, value] of Object.entries(obj)) {
      if (value !== null && typeof value === 'object') continue;
      output += String(value);
    }
    return output;
  }

  async handleModelOutputUpdate(
    call: InferResultType<'call'>,
    data: Vapi.ServerMessageModelOutput
  ): Promise<void> {
    const callId = call.id;

    const chunk = this.getModelOutputMessageSync(data.output);

    // Append chunk to buffer
    const existing = this.modelOutputBuffer.get(callId) ?? '';
    this.modelOutputBuffer.set(callId, existing + chunk);

    // Clear any existing timeout
    const existingTimer = this.modelOutputTimers.get(callId);
    if (existingTimer) clearTimeout(existingTimer);

    // Debounce timer
    const timer = setTimeout(async () => {
      await this.flushModelOutput(call);
    }, this.GAP_MS);

    this.modelOutputTimers.set(callId, timer);

    this.logger.log(`buffered chunk for ${callId}: ${JSON.stringify(chunk)}`);
  }

  async flushModelOutput(call: InferResultType<'call'>): Promise<void> {
    const callId = call.id;

    const fullOutput = this.modelOutputBuffer.get(callId) ?? '';
    if (!fullOutput) return;

    this.logger.log(`FLUSHED final message: ${JSON.stringify(fullOutput, null, 2)}`);
  }
@Vapi Support Bot please help to look into this
v
Currently, the
model-output
server message does not include a sequence index or ordering field in its payload, according to the official documentation.
If chunk order is critical, you may need to buffer and process the chunks as they arrive, but there is no built-in way to re-order them. Consider reaching out to Vapi support or submitting a feature request for sequence indices in the webhook payload. [See the official server message reference for details.](https://docs.vapi.ai/api-reference/webhooks/server-message) Source: - [Server Message API Reference](https://docs.vapi.ai/api-reference/webhooks/server-message)
c
I see what’s happening. The scrambled text is expected behavior right now.
model-output
server messages do NOT currently include a sequence index or guaranteed ordering, so chunks can arrive out of order and concatenating them will produce jumbled text. There’s no
sequenceNumber
(or similar) you can rely on today. What you can do instead: - Treat
model-output
as best-effort streaming only - Buffer by arrival time and accept possible reordering - Or switch to end-of-call report / conversation-update if you need a clean, ordered transcript If deterministic ordering is critical, this would need to be a feature request (sequence indices in
model-output
payloads).
w
i see… hm ok… i’ll open a feature request
c
Thank you