|
| 1 | +--- |
| 2 | +title: Common Errors |
| 3 | +description: Reference for common AI SDK errors and how to resolve them. |
| 4 | +--- |
| 5 | + |
| 6 | +# Common Errors |
| 7 | + |
| 8 | +## `maxTokens` → `maxOutputTokens` |
| 9 | + |
| 10 | +```typescript |
| 11 | +// ❌ Incorrect |
| 12 | +const result = await generateText({ |
| 13 | + model: 'anthropic/claude-opus-4.5', |
| 14 | + maxTokens: 512, |
| 15 | + prompt: 'Write a short story', |
| 16 | +}); |
| 17 | + |
| 18 | +// ✅ Correct |
| 19 | +const result = await generateText({ |
| 20 | + model: 'anthropic/claude-opus-4.5', |
| 21 | + maxOutputTokens: 512, |
| 22 | + prompt: 'Write a short story', |
| 23 | +}); |
| 24 | +``` |
| 25 | + |
| 26 | +## `maxSteps` → `stopWhen: stepCountIs(n)` |
| 27 | + |
| 28 | +```typescript |
| 29 | +// ❌ Incorrect |
| 30 | +const result = await generateText({ |
| 31 | + model: 'anthropic/claude-opus-4.5', |
| 32 | + tools: { weather }, |
| 33 | + maxSteps: 5, |
| 34 | + prompt: 'What is the weather in NYC?', |
| 35 | +}); |
| 36 | + |
| 37 | +// ✅ Correct |
| 38 | +import { generateText, stepCountIs } from 'ai'; |
| 39 | + |
| 40 | +const result = await generateText({ |
| 41 | + model: 'anthropic/claude-opus-4.5', |
| 42 | + tools: { weather }, |
| 43 | + stopWhen: stepCountIs(5), |
| 44 | + prompt: 'What is the weather in NYC?', |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +## `parameters` → `inputSchema` (in tool definition) |
| 49 | + |
| 50 | +```typescript |
| 51 | +// ❌ Incorrect |
| 52 | +const weatherTool = tool({ |
| 53 | + description: 'Get weather for a location', |
| 54 | + parameters: z.object({ |
| 55 | + location: z.string(), |
| 56 | + }), |
| 57 | + execute: async ({ location }) => ({ location, temp: 72 }), |
| 58 | +}); |
| 59 | + |
| 60 | +// ✅ Correct |
| 61 | +const weatherTool = tool({ |
| 62 | + description: 'Get weather for a location', |
| 63 | + inputSchema: z.object({ |
| 64 | + location: z.string(), |
| 65 | + }), |
| 66 | + execute: async ({ location }) => ({ location, temp: 72 }), |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +## `generateObject` → `generateText` with `output` |
| 71 | + |
| 72 | +`generateObject` is deprecated. Use `generateText` with the `output` option instead. |
| 73 | + |
| 74 | +```typescript |
| 75 | +// ❌ Deprecated |
| 76 | +import { generateObject } from 'ai'; |
| 77 | + |
| 78 | +const result = await generateObject({ |
| 79 | + model: 'anthropic/claude-opus-4.5', |
| 80 | + schema: z.object({ |
| 81 | + recipe: z.object({ |
| 82 | + name: z.string(), |
| 83 | + ingredients: z.array(z.string()), |
| 84 | + }), |
| 85 | + }), |
| 86 | + prompt: 'Generate a recipe for chocolate cake', |
| 87 | +}); |
| 88 | + |
| 89 | +// ✅ Correct |
| 90 | +import { generateText, Output } from 'ai'; |
| 91 | + |
| 92 | +const result = await generateText({ |
| 93 | + model: 'anthropic/claude-opus-4.5', |
| 94 | + output: Output.object({ |
| 95 | + schema: z.object({ |
| 96 | + recipe: z.object({ |
| 97 | + name: z.string(), |
| 98 | + ingredients: z.array(z.string()), |
| 99 | + }), |
| 100 | + }), |
| 101 | + }), |
| 102 | + prompt: 'Generate a recipe for chocolate cake', |
| 103 | +}); |
| 104 | + |
| 105 | +console.log(result.output); // typed object |
| 106 | +``` |
| 107 | + |
| 108 | +## Manual JSON parsing → `generateText` with `output` |
| 109 | + |
| 110 | +```typescript |
| 111 | +// ❌ Incorrect |
| 112 | +const result = await generateText({ |
| 113 | + model: 'anthropic/claude-opus-4.5', |
| 114 | + prompt: `Extract the user info as JSON: { "name": string, "age": number } |
| 115 | +
|
| 116 | + Input: John is 25 years old`, |
| 117 | +}); |
| 118 | +const parsed = JSON.parse(result.text); |
| 119 | + |
| 120 | +// ✅ Correct |
| 121 | +import { generateText, Output } from 'ai'; |
| 122 | + |
| 123 | +const result = await generateText({ |
| 124 | + model: 'anthropic/claude-opus-4.5', |
| 125 | + output: Output.object({ |
| 126 | + schema: z.object({ |
| 127 | + name: z.string(), |
| 128 | + age: z.number(), |
| 129 | + }), |
| 130 | + }), |
| 131 | + prompt: 'Extract the user info: John is 25 years old', |
| 132 | +}); |
| 133 | + |
| 134 | +console.log(result.output); // { name: 'John', age: 25 } |
| 135 | +``` |
| 136 | + |
| 137 | +## Other `output` options |
| 138 | + |
| 139 | +```typescript |
| 140 | +// Output.array - for generating arrays of items |
| 141 | +const result = await generateText({ |
| 142 | + model: 'anthropic/claude-opus-4.5', |
| 143 | + output: Output.array({ |
| 144 | + element: z.object({ |
| 145 | + city: z.string(), |
| 146 | + country: z.string(), |
| 147 | + }), |
| 148 | + }), |
| 149 | + prompt: 'List 5 capital cities', |
| 150 | +}); |
| 151 | + |
| 152 | +// Output.choice - for selecting from predefined options |
| 153 | +const result = await generateText({ |
| 154 | + model: 'anthropic/claude-opus-4.5', |
| 155 | + output: Output.choice({ |
| 156 | + options: ['positive', 'negative', 'neutral'] as const, |
| 157 | + }), |
| 158 | + prompt: 'Classify the sentiment: I love this product!', |
| 159 | +}); |
| 160 | + |
| 161 | +// Output.json - for untyped JSON output |
| 162 | +const result = await generateText({ |
| 163 | + model: 'anthropic/claude-opus-4.5', |
| 164 | + output: Output.json(), |
| 165 | + prompt: 'Return some JSON data', |
| 166 | +}); |
| 167 | +``` |
0 commit comments