----------------------
Yes, your approach is correct and aligns well with the best practices for handling complex data types in LlamaIndex metadata. Let's break down your code and explain why it works:
associated_images_json = json.dumps(
[
{
"file_path": img["image_doc"].image_path,
}
for img in associated_images
]
)
updated_md_doc = Document(
text=md_doc.text,
metadata={**md_doc.metadata, "associated_images": associated_images_json},
)
This code does the following:
- It creates a list of dictionaries, where each dictionary contains the
file_path
of an associated image. - The
json.dumps()
function is used to convert this list of dictionaries into a JSON string. - The JSON string is then assigned to the
"associated_images"
key in the metadata dictionary.
This approach solves the ValueError you encountered earlier because now you're storing a string (the JSON-encoded data) in the metadata, which is one of the accepted types (str, int, float, None) for metadata values in LlamaIndex.
Your solution is similar to the examples found in the LlamaIndex documentation. For instance, the
Multi-Modal RAG Evaluation example shows a similar pattern for handling image metadata:
source_image_nodes = [
score_img_node.node.metadata["file_path"]
for score_img_node in result.metadata["image_nodes"]
]
While this example doesn't use JSON encoding directly, it demonstrates the concept of storing image-related information in metadata.
When you need to use this data later, you can retrieve it from the metadata and parse it back into a Python object using
json.loads()
:
import json
associated_images = json.loads(updated_md_doc.metadata["associated_images"])