vercel/ai · error · CodeModeProtocolError

CODE_MODE_PROTOCOL_ERROR

CODE_MODE_PROTOCOL_ERROR

Error message

Code mode interruption payload is malformed.

What it means

assertInterruptPayload validates the resume payload attached to a host-function resume (context.resume.payload). It must be a non-null, non-array object with a string kind property; otherwise a CodeModeProtocolError is thrown. This guard protects the resume path from malformed payloads produced by corrupted continuation state or by resume mechanisms not originating from this library.

Source

Thrown at packages/code-mode/src/run-code-mode.ts:352

    }
    if (
      RunError.isInstance(error) ||
      (error instanceof Error && error.name === 'HostFunctionInterruptSignal')
    ) {
      throw error;
    }
    throw new RunError('Host tool failed.', 'CODE_MODE_HOST_TOOL_ERROR');
  }
}

function assertInterruptPayload(value: unknown): CodeModeInterruptPayload {
  if (
    typeof value !== 'object' ||
    value === null ||
    Array.isArray(value) ||
    typeof (value as { kind?: unknown }).kind !== 'string'
  ) {
    throw new CodeModeProtocolError(
      'Code mode interruption payload is malformed.',
    );
  }
  return value as CodeModeInterruptPayload;
}

function toRunLimits(
  policy: ResolvedExecutionPolicy,
  source: string,
  userSource: string,
): RunLimits {
  return {
    timeoutMs: policy.timeoutMs,
    memoryLimitBytes: policy.memoryLimitBytes,
    maxStackSizeBytes: policy.maxStackSizeBytes,
    maxResultBytes: expandedSerializationLimit(policy.maxResultBytes),
    maxConsoleOutputBytes: policy.maxConsoleOutputBytes,
    maxSourceBytes: withSerializationOverhead(

View on GitHub (pinned to 69428b1f8b)

Solutions

  1. Regenerate the interrupt by re-running the code-mode program instead of reusing suspect continuation state.
  2. Verify the continuation token is passed through unmodified from the original interrupt result.
  3. Ensure all packages that produce/consume continuations (@ai-sdk/code-mode and the underlying run package) are on compatible versions.
Defensive patterns

Strategy: validation

Validate before calling

function looksLikeInterruptPayload(v: unknown): boolean {
  return typeof v === 'object' && v !== null && !Array.isArray(v) && typeof (v as { kind?: unknown }).kind === 'string';
}
// Validate decoded continuation payloads before resuming.

Type guard

function isWellFormedPayload(v: unknown): v is { kind: string; [k: string]: unknown } {
  return typeof v === 'object' && v !== null && !Array.isArray(v) && typeof (v as { kind?: unknown }).kind === 'string';
}

Try / catch

try {
  await runCodeMode({ js, tools, continuation, interruptResolution });
} catch (error) {
  if (CodeModeProtocolError.isInstance(error)) {
    // discard corrupt continuation state and restart
  } else throw error;
}

Prevention

When it happens

Trigger: Resuming a code-mode run where the continuation token's recorded interruption payload is corrupt, truncated, or was produced by a different/incompatible library version lacking the kind field.

Common situations: Hand-crafting or editing continuation tokens; decoding signed continuation state with the wrong key or codec version; database round-trips that mangle stored payloads.

Understand the failure class

Related errors


AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30). Data as JSON: /api/errors/7e433f89184eefc8. Report an issue: GitHub.