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
Copy file name to clipboardExpand all lines: docs/guides/per-model-type-safety.md
+5-172Lines changed: 5 additions & 172 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,80 +3,10 @@ title: Per-Model Type Safety
3
3
id: per-model-type-safety
4
4
---
5
5
6
-
The AI SDK now provides **model-specific type safety** for `providerOptions`. Each model's capabilities (defined in `model-meta.ts`) determine which provider options are allowed, and TypeScript will enforce this at compile time.
6
+
The AI SDK provides **model-specific type safety** for `providerOptions`. Each model's capabilities determine which provider options are allowed, and TypeScript will enforce this at compile time.
7
7
8
8
## How It Works
9
-
10
-
### Architecture
11
-
12
-
1.**Manual Type Map per Adapter**
13
-
Each adapter (e.g., OpenAI) defines an explicit type map that associates each model name with its allowed provider option fragments:
14
-
15
-
```typescript
16
-
// In ai-openai/src/model-meta.ts
17
-
exporttypeOpenAIChatModelProviderOptionsByName= {
18
-
// Models WITH structured output support (include OpenAIStructuredOutputOptions)
19
-
"gpt-5":OpenAIBaseOptions&
20
-
OpenAIReasoningOptions&
21
-
OpenAIStructuredOutputOptions&
22
-
OpenAIToolsOptions&
23
-
OpenAIStreamingOptions&
24
-
OpenAIMetadataOptions;
25
-
"gpt-4o":OpenAIBaseOptions&
26
-
OpenAIStructuredOutputOptions&
27
-
OpenAIToolsOptions&
28
-
OpenAIStreamingOptions&
29
-
OpenAIMetadataOptions;
30
-
31
-
// Models WITHOUT structured output support (exclude OpenAIStructuredOutputOptions)
32
-
"gpt-4-turbo":OpenAIBaseOptions&
33
-
OpenAIToolsOptions&
34
-
OpenAIStreamingOptions&
35
-
OpenAIMetadataOptions;
36
-
"gpt-4":OpenAIBaseOptions&
37
-
OpenAIToolsOptions&
38
-
OpenAIStreamingOptions&
39
-
OpenAIMetadataOptions;
40
-
// ...
41
-
};
42
-
```
43
-
44
-
2.**Adapter Integration**
45
-
The adapter passes this type map as the 5th generic parameter to `BaseAdapter`:
// OK - OpenAIStructuredOutputOptions is included for gpt-5
98
28
type: "json_schema",
99
29
json_schema: {
100
30
/* ... */
@@ -121,106 +51,9 @@ const invalidCall = chat({
121
51
TypeScript will produce:
122
52
123
53
```
124
-
error TS2353: Object literal may only specify known properties, and 'text' does not exist in type 'OpenAIBaseOptions & OpenAIToolsOptions & OpenAIStreamingOptions & OpenAIMetadataOptions'.
125
-
```
126
-
127
-
## Adding New Models
128
-
129
-
When adding a new model to an adapter:
130
-
131
-
1.**Add model metadata** with capabilities in `model-meta.ts`:
132
-
133
-
```typescript
134
-
exportconst NEW_MODEL_META = {
135
-
// ... metadata
136
-
} asconstsatisfiesModelMeta<
137
-
OpenAIBaseOptions&OpenAIStructuredOutputOptions
138
-
>;
139
-
```
140
-
141
-
2.**Update the type map** in `OpenAIChatModelProviderOptionsByName`:
error TS2353: Object literal may only specify known properties, and 'text' does not exist in type ...'.
214
55
```
215
-
216
-
This approach failed because TypeScript's `satisfies` operator validates types but doesn't preserve generic type parameters in the resulting value. The value retains its concrete object type, not `ModelMeta<T>`, so the conditional type extraction fails.
217
-
218
-
**Solution**: Manually define the type map with explicit intersections for each model. This ensures accurate type narrowing and is the recommended approach.
219
-
220
-
### Type-Only Property
221
-
222
-
The `_modelProviderOptionsByName` property on adapters is a **type-only** property (definite assignment `!`). It exists purely for TypeScript's type system and is never accessed at runtime. This allows the core standalone functions to extract model-specific types via indexed access without runtime overhead.
223
-
56
+
224
57
## Benefits
225
58
226
59
-**Compile-time safety**: Catch incorrect provider options before deployment
0 commit comments