[Bug]: Metadata fields with value 0 being stored as null in Azure AI Search index
Bug Description
When creating metadata fields for AzureAiSearchVectorStore using MetadataIndexFieldType.INT32, the following code evaluates a 0 as false and stores 0 as null instead of keeping the original integer in base.py:
def _default_index_mapping(
self, enriched_doc: Dict[str, str], metadata: Dict[str, Any]
) -> Dict[str, str]:
index_doc: Dict[str, str] = {}
for field in self._field_mapping:
index_doc[self._field_mapping[field]] = enriched_doc[field]
for metadata_field_name, (
index_field_name,
_,
) in self._metadata_to_index_field_map.items():
metadata_value = metadata.get(metadata_field_name)
# ERROR IS HERE
if metadata_value: --> Should be: if metadata_value is not None:
index_doc[index_field_name] = metadata_value
return index_doc
Version
0.12.12
Steps to Reproduce
Try to store any metadata field equal to 0 using AzureAISearchVectorStore as vector store
Relevant Logs/Tracbacks
@QuentinMahieu Hello there! I'm here to help you with any bugs or questions you have. Let's tackle this together.
The issue you're encountering is due to the conditional check if metadata_value: in the _default_index_mapping function. This check evaluates to False for any value that is considered "falsy" in Python, including 0. As a result, metadata fields with a value of 0 are being stored as null.
To fix this, you should modify the condition to explicitly check for None instead of using a general truthiness check. Change the line:
if metadata_value:
to:
if metadata_value is not None:
This change will ensure that 0 is correctly stored as an integer value rather than being converted to null [1].
To continue talking to Dosu, mention @dosu.
Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other