Back to Repositories

Testing Vector Store Query Output Parser in llama_index

This test suite validates the VectorStoreQueryOutputParser functionality in llama_index, focusing on parsing JSON-formatted query specifications into structured VectorStoreQuerySpec objects. The tests ensure proper handling of query strings, filters, and top-k parameters.

Test Coverage Overview

The test suite provides comprehensive coverage of the VectorStoreQueryOutputParser class functionality.

Key areas tested include:
  • JSON string parsing to VectorStoreQuerySpec objects
  • ExactMatchFilter creation and validation
  • Query string extraction
  • Top-k parameter handling
Integration points with StructuredOutput and type casting are verified.

Implementation Analysis

The testing approach employs a single focused test case that validates the complete parsing workflow. The implementation uses Python’s type casting and assertion patterns to verify object types and equality.

Technical patterns include:
  • Type casting with Python’s cast() function
  • JSON string parsing validation
  • Structured object comparison
  • Multiple filter parameter handling

Technical Details

Testing tools and setup:
  • Python typing module for type assertions
  • Custom VectorStoreQueryOutputParser class
  • StructuredOutput base class
  • VectorStoreQuerySpec and ExactMatchFilter types
  • JSON-formatted test input strings

Best Practices Demonstrated

The test implementation showcases several testing best practices for parser validation.

Notable practices include:
  • Clear test input preparation
  • Type safety checks
  • Comprehensive object equality validation
  • Structured test organization
  • Explicit expected output definition

run-llama/llama_index

llama-index-core/tests/indices/vector_store/auto_retriever/test_output_parser.py

            
from typing import cast

from llama_index.core.indices.vector_store.retrievers.auto_retriever.output_parser import (
    VectorStoreQueryOutputParser,
)
from llama_index.core.output_parsers.base import StructuredOutput
from llama_index.core.vector_stores.types import (
    ExactMatchFilter,
    VectorStoreQuerySpec,
)


def test_output_parser() -> None:
    output_str = """\
    ```json
    {
        "query": "test query str",
        "filters": [
            {
                "key": "director",
                "value": "Nolan"
            },
            {
                "key": "theme",
                "value": "sci-fi"
            }
        ],
        "top_k": 2
    }
    ```
    """

    parser = VectorStoreQueryOutputParser()
    output = parser.parse(output_str)
    structured_output = cast(StructuredOutput, output)
    assert isinstance(structured_output.parsed_output, VectorStoreQuerySpec)

    expected = VectorStoreQuerySpec(
        query="test query str",
        filters=[
            ExactMatchFilter(key="director", value="Nolan"),
            ExactMatchFilter(key="theme", value="sci-fi"),
        ],
        top_k=2,
    )
    assert structured_output.parsed_output == expected