feat(ai): add retry support to onError callback in generateObject/streamObject#9083
feat(ai): add retry support to onError callback in generateObject/streamObject#9083fveiraswww wants to merge 3 commits intovercel:mainfrom
Conversation
|
We have a repair function that you can use: https://ai-sdk.dev/docs/ai-sdk-core/generating-structured-data#repairing-invalid-or-malformed-json The reason a repair function approach was taken is that it gives you more control. You can also use streamText with automated roundtrips for error cases and prepareStep for setting temperature. |
|
thanks @lgrammel! I know about the temperature setting is just an example, on retry you actually have access to StreamObjectOptions / GenerateObjectOptions It's more of a double-check: if |
Background
currently
maxRetriesonly handles model api errors. However,generateObjectandstreamObjectcan fail for other reasons (e.g. schema validation errors, JSON parsing errors). this pr enables developers to implement custom retry logic for these casesSummary
adds support for custom retry logic via the onError callback in
generateObjectandstreamObjectretryfunction toonErrorcallback that accepts partial options:{ ...originalOptions, ...newOptions }onErrorcallback into theparseAndValidateObjectResultWithRepaircatch block to enable retries on schema validation failuresStreamObjectOptionstostream-object-options.tsGenerateObjectOptionstogenerate-object-options.tsStreamObjectReturnTypehelper type for better type inferenceonErrorparameter doc togenerate-objectreference (prev undocumented)onErrorparameter doc instream-objectto includeretryfunctionexample usage:
Manual Verification
tested basic retry functionality with
generateObjectandstreamObjectChecklist
pnpm changesetin the project root)pnpm prettier-fixin the project root)Future Work
Related Issues