Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning full Firestore document from Genkit's Firestore Retriever

What's the easiest way to return a full document from Firestore in this retriever (screenshot from Genkit docs)?

My best guess is in the metadata field, so doing something like:

const vectorQuerySnapshot = await vectorQuery.get();
    return {
      documents: vectorQuerySnapshot.docs.map((doc) =>
        // doc.data() represents the Firestore document. You may process it as needed to generate
        // a Genkit document object, depending on your storage format.
        Document.fromText(doc.data().title.title_original, doc.data())
      ),
    };
like image 286
David Oort Avatar asked Oct 31 '25 03:10

David Oort


1 Answers

Document metadata in Genkit is expected to be a Record<string, any> type object. If Firestore's DocumentSnapshot.data() method returns a compatible object, your solution works.

You could also include the Firestore doc in a self-declared "special" field in the metadata (eg: { full_firestore_doc: doc.data() }). This separates your actual Firestore doc from other metadata fields that you may want to include along with your Genkit document.

If you want to include more than title.title_original in your Genkit Document text, you can create a new string with the fields that you want to include and set it as the text. Eg:

vectorQuerySnapshot.docs.map((doc) => {
    const text = `Title: ${doc.title.title_original}\nSomething else: ${doc.other_field}`
    return Document.fromText(text, {full_document: doc})
}),

Hope this helps!

References:

1 Genkit Document

2 Firestore DocumentSnapshot

like image 145
Samuel Bushi Avatar answered Nov 03 '25 00:11

Samuel Bushi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!