Hi Maria Natalia Benavides Sanabria
Thanks for the update and the confirmation that the embedding
field exists in your documents and index.
The KeyError: 'embedding'
is most likely caused because Cosmos DB’s $search
stage doesn’t automatically include all fields in the search results — including embedding
. This means that while the data is stored correctly, it’s not returned by the query, and LangChain fails when trying to access it.
✅ What you can do:
1. Test a raw search query (outside LangChain) with a $project
stage that explicitly includes the embedding
field. This should work.
2.If it works, then you'll need to either:
- Patch LangChain locally to include
$project
in its vector search query, or - Use a custom retriever that performs the aggregation manually (we can share code if needed).
3.Long-Term Fixes / Suggestions
- Open a GitHub issue or PR with LangChain: This is a real gap in Cosmos integration — not automatically projecting required fields.
Alternatively, wrap AzureCosmosDBVectorSearch
to override the internal _search
method, injecting a custom projection.
This is a known limitation and something LangChain may fix in future updates.
Hope this helps. Do let us know if you any further queries.
If this answers your query, do click Accept Answer
and Yes
for was this answer helpful. And, if you have any further query do let us know.