Retrival of METADATA

I chunked the data and the metadata along with it. when retriving i need to show a key in the metadatas as response.

def ask_question(store_name, question):

print("\\n Asking Gemini...")



try:

    response = client.models.generate_content(

        model="gemini-2.5-flash",

        contents=question,

        config=types.GenerateContentConfig(

            tools=\[

                types.Tool(

                    file_search=types.FileSearch(

                        file_search_store_names=\[store_name\]

                    )

                )

            \]

        )

    )



    \# --------------------------

    \# Print answer text

    \# --------------------------

    print("\\n Answer:\\n")

    try:

        print(response.text)

    except:

        print(" No answer text returned.")



    \# --------------------------

    \# Extract metadata of matched chunks

    \# --------------------------

    print("\\n Matched Metadata:\\n")



    refs = None  # ensure variable is defined



    try:

        gm = response.candidates\[0\].grounding_metadata

        refs = gm.search_entry_point.retrieved_references

    except:

        print("⚠ No retrieved references.")

        return



    if not refs:

        print("⚠ No retrieved references.")

        return



    \# DEBUG: print retrieved files

    print("Retrieved files:")

    for ref in refs:

        title = getattr(ref.chunk, "title", "(no title)")

        print(" •", title)



    print()




except Exception as e:

    print(f" Query error: {e}")

This do not seems to do the needful.

It looks like your metadata isn’t being surfaced in the final response because you need to explicitly extract and return it from the chunks when generating output. Make sure your retrieval step pulls both the text and the specific metadata key before sending the final prompt to Gemini. Adding a small post-processing step to format the metadata into the final answer usually fixes this.

And just like managing information in workflows, keeping metadata organized and retrievable is important in real systems too—whether it’s an AI app or handling sensitive case details in something like bail bonds Tampa