You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit b6b9dca
Browse filesBrowse the repository at this point in the historyBrowse files
chore(ai): remove redundant runtime type validation (#24095)
## Background
- several runtime checks in AI SDK check for things that our TypeScript
types already enforce
- this only guards against users bypassing TypeScript by using plain
javascript, `any`, or type assertions, which we shouldn't support
- the previous `streamText` validation was expensive because it had to
validate every message in history on every step
## Summary
- Remove the `ModelMessage` schema validation in `standardizePrompt`.
- Still check for empty messages and system messages, which types can't
express
- Remove type checks that duplicate the types on call settings
- `temperature`, `topP`, `topK`, `presencePenalty`, `frequencyPenalty`
being numbers
- `smoothStream` chunking being one of the possible values
- steering messages checking that role is user
- tool caller config being an array with each caller being a string
- `experimental_streamSpeech` throwing if input didn't match one of the
possible inputs
- `generateObject`/`streamObject` validating `output` types and
requiring a `schema` when present
- `decide` checking state types and question shapes
- batch reference version being checked that it's `2`
- In `decide`, keep only the checks that types cannot express
(non-finite numbers, array holes, etc).
- Updated `InvalidPromptError` docs to mention current causes.
Callers who pass malformed input without going through types will now
get a `TypeError` from the runtime instead of an SDK validation error.
Callers who respect the types see no change.
The biggest benefit is not validating every message in `streamText` on
every step anymore. With 2,201 messages - the amount from the original
issue - and a mocked model that instantly returns:
| Metric | main | this PR | delta |
| --- | --- | --- | --- |
| `streamText`, each later step | 37.5ms | 0.25ms | −37.3ms (~150×
faster) |
| `streamText`, first step | 74.5ms | 0.39ms | −74.1ms (~190× faster) |
| `streamText`, 11-step run | 456.7ms | 3.9ms | −452.8ms (~115× faster)
|
| `generateText`, first step | 37.1ms | 0.33ms | −36.8ms (~110× faster)
|
| `generateText`, each later step | 0.22ms | 0.20ms | no change |
| Single-message `generateText` call | 0.4ms | 0.4ms | no change |
## Related Issues
- Fixed#24048 by just removing that
validation altogether
chore(ai): remove runtime validation of inputs that TypeScript types already guarantee
6
+
7
+
`streamText`, `generateText`, `generateObject` and `streamObject` no longer validate messages against the `ModelMessage` schema. This removes a cost that grew linearly with message history and was paid on every `streamText` step. Type checks on call settings, steering messages, tool caller configuration, `smoothStream` chunking, `experimental_streamSpeech` text input, the shape of `decide` state and questions, and the batch reference version have also been removed. Value constraints that types cannot express, such as integer ranges, non-empty collections, unknown keys, and non-finite numbers or cycles in `decide` JSON input, are still validated.
0 commit comments