Error types
Genkit knows about two specialized types: GenkitError and UserFacingError.
GenkitError is intended for use by Genkit itself or Genkit plugins.
UserFacingError is intended for ContextProviders and
your code. The separation between these two error types helps you better understand
where your error is coming from.
Genkit plugins for web hosting (e.g. @genkit-ai/express or @genkit-ai/next)
SHOULD capture all other Error types and instead report them as an internal error in the response.
This adds a layer of security to your application by ensuring that internal details of your application
do not leak to attackers.
Get failures on the response with throwOnError: false
Section titled “Get failures on the response with throwOnError: false”By default, generate() throws when something fails: a model error, a tool error, a blocked
response, output that does not match the schema, or the maxTurns limit. In a tool loop that
throws away the rounds that already finished.
Set throwOnError: false to get the failure back on the response instead, together with the
conversation up to the last completed tool round. You can then retry from there without repeating
the tool calls that already ran:
const opts = { model: googleAI.model('gemini-flash-latest'), tools: [getWeather], throwOnError: false,};
let res = await ai.generate({ ...opts, prompt: 'What should I wear in Tokyo today?' });
if (res.error) { console.warn(res.finishReason, res.error.status, res.error.message); if (res.finishReason === 'failed' || res.finishReason === 'aborted') { // res.messages ends at a turn seam, so it is safe to send again. res = await ai.generate({ ...opts, messages: res.messages }); }}A response that reports a failure works like this:
errorholds the classified failure: astatus(such asINTERNAL), amessage, and the original error’sdetails. Checkerror, notfinishReason, to tell whether the call failed.finishReasonisfailedwhen something broke (a model call, a tool, a middleware hook) andabortedwhen the caller stopped the loop: theabortSignalfired, the model call rejected with anAbortErrororTimeoutError, ormaxTurnswas reached. A provider that answers with a status such asDEADLINE_EXCEEDEDorABORTEDstill reportsfailed.- A
failedorabortedresponse has nomessage, sotextis empty andoutputisnull. ItsfinishMessagehas the same text aserror.message. - Other failures keep the model’s own finish reason and finish message beside
error: a blocked response (blocked, withFAILED_PRECONDITION), a response without a message, and output that fails schema validation (usuallystop, withINVALID_ARGUMENT, and the model’s message so you can inspect the raw text). A resume whose restarted tool interrupts again keepsinterrupted; answer it withresume. Don’t send these responses’messagesagain as they are. - For a
failedorabortedresponse,messagesnever ends on a tool request that nothing answered, because no provider accepts that. A failed tool discards the whole round it was part of, and is reported asINTERNALwith a message that starts withtool "<name>" failed:followed by the tool’s error text. If theabortSignalhad fired, it is reported asCANCELLED(tool "<name>" stopped: ...) withfinishReasonabortedinstead. - Text streamed before the failure has already reached
onChunk.
Errors that happen before the request is resolved, such as an unknown model, tool, or resource,
invalid options, or a generate middleware hook that fails before the first turn runs, still
throw, because there is no response to return.
Code that does not set throwOnError gets the same errors as before. Agents use the same
mechanism to save failed turns as resume points; see
Agent error handling.