Skip to content

Commit 7b02510

Browse files
committed
docs(ai-chat): document the clientData validation error timing options
The chat.withClientData reference still carried the older signature with only schema, so reportErrorAt and onValidationError were missing from the page that defines the function. clientDataReportErrorAt, the flat option on chat.customAgent, was absent from the docs entirely while its sibling callback was documented in six places. Adds both to the reference with types and defaults, states what reportErrorAt does and does not govern, names the flat equivalents for callers who pass options directly, and cross-links from the types page.
1 parent d40b858 commit 7b02510

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

docs/ai-chat/reference.mdx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,15 +550,25 @@ Use this when you need [`InferChatUIMessage`](#inferchatuimessage) / typed `data
550550
Returns a [`ChatBuilder`](/ai-chat/types#chatbuilder) with a fixed client data schema. Managed-agent hooks and `run` get typed `clientData` without passing `clientDataSchema` in `.agent()` options. Custom agents parse `payload.metadata` on the initial payload and later input frames before passing it to user code.
551551

552552
```ts
553-
chat.withClientData<TSchema>({ schema: TSchema }): ChatBuilder<UIMessage, TSchema>;
553+
chat.withClientData<TSchema>({
554+
schema: TSchema;
555+
reportErrorAt?: "turn-end" | "arrival";
556+
onValidationError?: (event: { error: unknown; payload: ChatTaskWirePayload }) => Promise<void> | void;
557+
}): ChatBuilder<UIMessage, TSchema>;
554558
```
555559

556-
| Parameter | Type | Description |
557-
| --------- | ------------ | -------------------------------------------------- |
558-
| `schema` | `TaskSchema` | Zod, ArkType, Valibot, or any supported schema lib |
560+
| Parameter | Type | Default | Description |
561+
| ------------------- | --------------------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------ |
562+
| `schema` | `TaskSchema` | required | Zod, ArkType, Valibot, or any supported schema lib |
563+
| `reportErrorAt` | `"turn-end" \| "arrival"` | `"turn-end"` | When the client-visible `Invalid client data` error is written for a frame that failed validation mid-turn |
564+
| `onValidationError` | `(event) => void` || Called when an input fails validation. Composes with the task-level `onClientDataValidationError` rather than replacing it |
565+
566+
`reportErrorAt` governs only the stream-visible error. `"turn-end"` holds it until the turn closes, so a bad send cannot truncate an answer the user is already reading; `"arrival"` writes it as soon as validation fails, ending the response in progress. `onValidationError` and the task log fire on arrival in both modes, and the frame is never delivered as a turn either way.
559567

560568
For `chat.customAgent()`, invalid client data is skipped. Async reads emit an error chunk followed by `turn-complete`. A `chat.messages.on()` subscription uses the task's `onClientDataValidationError` callback and task log instead, so an active response is not ended early. Without a schema, metadata is passed through unchanged.
561569

570+
Passing options directly to `chat.customAgent()` instead of through the builder uses the flat equivalents: `clientDataSchema`, `clientDataReportErrorAt`, and `onClientDataValidationError`.
571+
562572
Full guide: [Typed client data](/ai-chat/types#typed-client-data-with-chatwithclientdata).
563573

564574
## `ChatWithUIMessageConfig`

docs/ai-chat/types.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ export const myChat = chat
169169

170170
The schema runs at runtime for both `.agent()` and `.customAgent()`. Custom agents validate the initial payload and later `chat.messages` frames. Invalid frames are not passed to user code. Async reads emit an error chunk followed by `turn-complete`; `chat.messages.on()` reports through `onClientDataValidationError` and the task log so it does not end an active response. Without a schema, metadata is passed through unchanged.
171171

172+
`withClientData` also takes `reportErrorAt` and `onValidationError` alongside `schema`. See [chat.withClientData](/ai-chat/reference#chatwithclientdata) for both, and [Validating client data](/ai-chat/custom-agents#validating-client-data) for the custom-agent walkthrough.
173+
172174
## ChatBuilder
173175

174176
Both `chat.withUIMessage()` and `chat.withClientData()` return a **ChatBuilder** — a chainable object that accumulates configuration before creating the agent with `.agent()`.

0 commit comments

Comments
 (0)