Chat Message Error Schema#
ChatMessageError is the canonical error shape attached to a chat message record. It is defined in packages/types/src/message/common/base.ts and validated at API boundaries via ChatMessageErrorSchema (a Zod schema in the same file).
Interface and Schema#
The ChatMessageError interface carries:
| Field | Required | Purpose |
|---|---|---|
type | Yes | Error type code — the only required field |
message | No | Human-readable description |
body | No | Raw upstream payload for debugging |
attribution | No | user |
severity | No | info |
category | No | Semantic bucket (e.g. auth / quota) for dashboards |
httpStatus | No | HTTP status the runtime returned or would return |
retryable | No | Transport-level retry hint |
countAsFailure | No | Whether this increments operational failure metrics |
isFallback | No | Whether this is a catch-all bucket (tracked to drive finer error codes) |
numericId | No | Stable E<n> reference for docs/support tickets |
The type field is the only required field in both the TypeScript interface and the Zod schema . The Zod validator accepts z.union([z.string(), z.number()]) to accommodate both string-keyed and numeric error codes .
Allowed type Values#
type is a union of three independently maintained enumerations :
ILobeAgentRuntimeErrorType— runtime/provider errors fromAgentRuntimeErrorType, e.g.RateLimitExceeded,ExceededToolLimit,ProviderBizError,DatabasePersistErrorErrorType— chat-level and HTTP errors fromChatErrorType, e.g.InvalidAccessCode,FreePlanLimit,500,502IToolErrorType— tool plugin errors fromToolErrorType; currently onlyPluginSettingsInvalid
API Validation#
UpdateMessageParamsSchema (used by the message.update tRPC mutation) embeds ChatMessageErrorSchema as a nullish field . The message.update handler validates input against this schema before passing it to MessageService.updateMessage.
Error Normalization and Enrichment#
The central normalization point is formatErrorForState in apps/server/src/modules/AgentRuntime/formatErrorForState.ts. It handles four input shapes:
ChatCompletionErrorPayloadLike(haserrorType) — extracts message from nested body layers, promoteserrorType→type- Already-normalized
ChatMessageError(has string/numbertype, not anErrorinstance) — passes through to enrichment Errorinstance — wraps withtype: ChatErrorType.InternalServerError- Arbitrary thrown value — wraps as
AgentRuntimeErrorType.AgentRuntimeError
After normalization, enrichWithSpec looks up the type string in ERROR_CODE_SPECS (via getErrorCodeSpec from @lobechat/model-runtime) and stamps all classification fields (attribution, category, severity, retryable, countAsFailure, isFallback, numericId) in one place. This ensures all downstream consumers — DB JSONB columns, S3 snapshots, gateway WebSocket pushes, dashboards — receive a consistently enriched shape without re-running classification .
Persistence via Message Updates#
Errors are written to the assistant message record in CompletionLifecycle.dispatchHooks:
- When a completion operation ends with
reason === 'error'and the message ID is present,formatErrorForStateis called onstate.error - The result is persisted via
messageModel.update(assistantMessageId, { error: { ...messageError, body: ..., message: ... } }) - The
errorfield maps toUpdateMessageParams.error: ChatMessageError | null, which flows through the tRPCmessage.updateendpoint
MCP Tool Call Errors#
MCP tool failures produce ToolExecutionResult error objects with a code string (not yet ChatMessageError shaped) in ToolExecutionService.executeMCPTool:
| Code | Trigger |
|---|---|
MANIFEST_NOT_FOUND | Tool manifest missing from execution context |
MCP_CONFIG_NOT_FOUND | mcpParams absent from manifest |
MCP_EXECUTION_ERROR | Exception thrown during mcpService.callTool |
MCP_DEVICE_EXECUTION_ERROR | Failure tunnelling through device gateway for stdio MCP |
These { code, message } objects are normalized into ChatMessageError further up the call stack via normalizeExecutionError and eventually formatErrorForState before being written to the message record.
Key Files#
| File | Purpose |
|---|---|
packages/types/src/message/common/base.ts | ChatMessageError interface + ChatMessageErrorSchema Zod schema |
packages/types/src/message/db/params.ts | UpdateMessageParams — embeds ChatMessageErrorSchema |
packages/types/src/agentRuntime.ts | AgentRuntimeErrorType — primary source of type values |
packages/types/src/fetch.ts | ChatErrorType — HTTP + business error codes |
packages/types/src/tool/error.ts | ToolErrorType — tool plugin error codes |
apps/server/src/modules/AgentRuntime/formatErrorForState.ts | Normalization + spec enrichment |
apps/server/src/services/agentRuntime/CompletionLifecycle.ts | Error persistence to message record |
apps/server/src/services/toolExecution/index.ts | MCP tool error construction |
apps/server/src/routers/lambda/message.ts | tRPC message.update endpoint |