I’ve hit this exact metadata issue before. The real problem isn’t tweaking your return format - it’s getting reliable data flow between retrieval and agent response.
Don’t patch together custom formatters or pray your vector store returns consistent structures. Just automate the whole thing. I built something similar when I needed to track document sources, confidence scores, and metadata across different retrieval calls.
Create an automation that handles document search, formats responses with answer and source info, then feeds everything back to your agent consistently. No more debugging weird vector store responses or maintaining custom wrapper functions.
Bonus: the automation logs all retrieval metadata, tracks your most-used sources, and alerts you when certain documents go missing. Way cleaner than hardcoded return formats that break every time your data structure shifts.
You can build this whole flow visually without touching existing LangChain code. Connect your vector store, add formatting logic, pipe it back to your agent.