AI SDK 提供了针对 modelOptions 的特定模型类型安全。每个模型的能力决定了允许哪些模型选项,TypeScript 会在编译时强制执行此操作。
import { chat } from "@tanstack/ai";
import { openaiText } from "@tanstack/ai-openai";
// ✅ gpt-5 supports structured outputs - `text` is allowed
const validCall = chat({
adapter: openaiText("gpt-5"),
messages: [],
modelOptions: {
// OK - text is included for gpt-5
text: {
type: "json_schema",
json_schema: {
/* ... */
},
},
},
});
// ❌ gpt-4-turbo does NOT support structured outputs - `text` is rejected
const invalidCall = chat({
adapter: openaiText("gpt-4-turbo"),
messages: [],
modelOptions: {
text: {}, // ❌ TypeScript error: 'text' does not exist in type
},
});
TypeScript 将会产生
error TS2353: Object literal may only specify known properties, and 'text' does not exist in type ...'.