Firebase Genkit Error Handling: NoValidCandidatesError not exported

  Kiến thức lập trình

I want to be able to handle errors when the’re an output error using Firebase Genkit. If the prompt is not able to generate a response matching the output schema, I get this error:

name: 'Error',
  message: 'FAILED_PRECONDITION: Generation resulted in no candidates matching provided output schema.',
  stack: 'Error: FAILED_PRECONDITION: Generation resulted in no candidates matching provided output schema.n' +
    '    at /Users/.../functions/node_modules/@genkit-ai/ai/lib/generate.js:560:15n' +
    '    at Generator.next (<anonymous>)n' +
    '    at fulfilled (/Users/tomas/Documents/tindog-backend/functions/node_modules/@genkit-ai/ai/lib/generate.js:53:24)n' +
    '    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)',
  source: 'ts',

This error a NoValidCandidatesError type of error, and I want to be able to understand why this failed.

I tried to import the NoValidCandidatesError class so that I could handle the Genkit output errors properly in my code, so I did:
import {NoValidCandidatesError} from "@genkit-ai/ai/lib/generate";

But unfortunately when I compile my code, I get an error since that error is not exported:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './lib/generate' is not defined by "exports" in .../functions/node_modules/@genkit-ai/ai/package.json

How can I do proper error handling using Genkit if I don’t have access to the error types? Is there a way to get a more clear message when a prompt cannot generate a response for a provided output schema?

LEAVE A COMMENT