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
- Regenerate the interrupt by re-running the code-mode program instead of reusing suspect continuation state.
- Verify the continuation token is passed through unmodified from the original interrupt result.
- 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
- Never hand-edit continuation tokens or their decoded payloads.
- Use lossless serialization when persisting interrupts.
- Keep code-mode and run package versions in sync.
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
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- CODE_MODE_PROTOCOL_ERROR
- Continuation contains a non-JSON-serializable value.
- Code mode interrupt payload must be an object.
- Code mode interrupt payload must include a string kind.
- CODE_MODE_PROTOCOL_ERROR
AI-assisted analysis of vercel/ai@69428b1f8b (2026-08-30).
Data as JSON: /api/errors/7e433f89184eefc8.
Report an issue: GitHub.