def _add_query_result_events(span, kwargs):
    """
    There's a lot of logic here involved in converting the query result
    format from ChromaDB into the canonical format (taken from Pinecone)

    This is because Chroma query result looks like this:

        {
           ids: [1, 2, 3...],
           distances: [0.3, 0.5, 0.6...],
           metadata: ["some metadata text", "another metadata text",...],
           documents: ["retrieved text", "retrieved text2", ...]
        }

    We'd like instead to log it like this:

        [
            {"id": 1, "distance": 0.3,  "document": "retrieved text", "metadata": "some metadata text",
            {"id": 2, "distance" 0.5, , "document": "retrieved text2": "another metadata text",
            {"id": 3, "distance": 0.6, "document": ..., "metadata": ...
        ]

    If you'd like to understand better why, please read the discussions on PR #370:
    https://github.com/traceloop/openllmetry/pull/370

    The goal is to set a canonical format which we call as a Semantic Convention.
    """
    zipped = itertools.zip_longest(
        kwargs.get("ids", []) or [],
        kwargs.get("distances", []) or [],
        kwargs.get("metadatas", []) or [],
        kwargs.get("documents", []) or [],
    )
    for tuple_ in zipped:
        attributes = {
            EventAttributes.DB_QUERY_RESULT_ID.value: None,
            EventAttributes.DB_QUERY_RESULT_DISTANCE.value: None,
            EventAttributes.DB_QUERY_RESULT_METADATA.value: None,
            EventAttributes.DB_QUERY_RESULT_DOCUMENT.value: None,
        }

        attributes_order = ["ids", "distances", "metadatas", "documents"]
        attributes_mapping_to_canonical_format = {
            "ids": EventAttributes.DB_QUERY_RESULT_ID.value,
            "distances": EventAttributes.DB_QUERY_RESULT_DISTANCE.value,
            "metadatas": EventAttributes.DB_QUERY_RESULT_METADATA.value,
            "documents": EventAttributes.DB_QUERY_RESULT_DOCUMENT.value,
        }
        for j, attr in enumerate(tuple_):
            original_attribute_name = attributes_order[j]
            canonical_name = attributes_mapping_to_canonical_format[
                original_attribute_name
            ]
            try:
                value = attr[0]
                if isinstance(value, dict):
                    value = json.dumps(value)

                attributes[canonical_name] = value
            except (IndexError, TypeError):
                # Don't send missing values as nulls, OpenTelemetry dislikes them!
                del attributes[canonical_name]

        span.add_event(name=Events.DB_QUERY_RESULT.value, attributes=attributes)

    There's a lot of logic here involved in converting the query result
    format from ChromaDB into the canonical format (taken from Pinecone)

    This is because Chroma query result looks like this:

        {
           ids: [1, 2, 3...],
           distances: [0.3, 0.5, 0.6...],
           metadata: ["some metadata text", "another metadata text",...],
           documents: ["retrieved text", "retrieved text2", ...]
        }

    We'd like instead to log it like this:

        [
            {"id": 1, "distance": 0.3,  "document": "retrieved text", "metadata": "some metadata text",
            {"id": 2, "distance" 0.5, , "document": "retrieved text2": "another metadata text",
            {"id": 3, "distance": 0.6, "document": ..., "metadata": ...
        ]

    If you'd like to understand better why, please read the discussions on PR #370:
    https://github.com/traceloop/openllmetry/pull/370

    The goal is to set a canonical format which we call as a Semantic Convention.
    
packages/opentelemetry-instrumentation-chromadb/opentelemetry/instrumentation/chromadb/wrapper.py