Skip to content

[Bug]: BigQuery and Milvus vector search enrichment modify the input elements in place #41989

Todo

Description

@mekala27-45

What happened?

BigQueryVectorSearchEnrichmentHandler adds the search result to its input element in place. Its join_fn writes into the metadata dict of the element it receives and returns that same object:

# sdks/python/apache_beam/ml/rag/enrichment/bigquery_vector_search.py
def join_fn(left: Embedding, right: dict[str, Any]) -> Embedding:
  left.metadata['enrichment_data'] = right
  return left

milvus_search.py has the same function. Enrichment runs join_fn in a plain beam.Map, so left is the element of the input PCollection. Two things follow:

  1. Another transform that reads the same input PCollection sees enrichment_data in its elements, although nothing on that branch adds it.
  2. Items that share one metadata dict all end up with the search result of the last item. This happens when a FlatMap builds several questions from one document and a later step buffers elements, for example BatchElements or RunInference.

Short reproduction of the cause, on master at 11a2b07 with Python 3.12:

from apache_beam.ml.rag.enrichment.bigquery_vector_search import join_fn
from apache_beam.ml.rag.types import Content, EmbeddableItem, Embedding

shared = {'doc_id': 'doc'}
items = [
    EmbeddableItem(
        id=f'doc-q{i}',
        content=Content(text=f'question {i}'),
        embedding=Embedding(dense_embedding=[float(i), 0.5]),
        metadata=shared) for i in (1, 2, 3)
]
joined = [
    join_fn(item, {'id': item.id, 'chunks': [{'content': f'neighbor-of-{i}'}]})
    for i, item in zip((1, 2, 3), items)
]

print('returns the input object:', joined[0] is items[0])
print('input metadata keys     :', sorted(items[0].metadata))
for item in joined:
  print(item.id, '->', item.metadata['enrichment_data']['chunks'][0]['content'])

Output:

returns the input object: True
input metadata keys     : ['doc_id', 'enrichment_data']
doc-q1 -> neighbor-of-3
doc-q2 -> neighbor-of-3
doc-q3 -> neighbor-of-3

Expected: the input item keeps only doc_id, and each joined item keeps its own neighbor.

I see the same result end to end on the DirectRunner with bigquery.Client replaced by a fake (FlatMap, then Enrichment(handler), then BatchElements): all three items come out with neighbor-of-3, and a second branch that reads the input sees enrichment_data in every element.

The Qdrant handler already returns a copy, replace(left, metadata={**left.metadata, 'enrichment_data': right}), and qdrant_search_test.py has a test_join_fn that asserts the input is unchanged. The same kind of problem was fixed for the embedding step in #41233.

Suggested fix: return a copy in the BigQuery and Milvus handlers the way the Qdrant handler does, and add a test_join_fn for each.

Issue Priority

Priority: 2 (default / most bugs should be filed as P2)

Issue Components

  • Component: Python SDK
  • Component: Java SDK
  • Component: Go SDK
  • Component: Typescript SDK
  • Component: IO connector
  • Component: Beam YAML
  • Component: Beam examples
  • Component: Beam playground
  • Component: Beam katas
  • Component: Website
  • Component: Infrastructure
  • Component: Spark Runner
  • Component: Flink Runner
  • Component: Prism Runner
  • Component: Hazelcast Jet Runner
  • Component: Google Cloud Dataflow Runner

Activity

  1. sakshammalik260-cse commented on Dec 5, 2026

    @sakshammalik260-cse
    Contributor

    .take-issue

  2. sakshammalik260-cse commented on Dec 5, 2026

    @sakshammalik260-cse
    Contributor

    Hi @mekala27-45,

    I've submitted the fix for #41989, following your suggested approach of using dataclasses.replace, consistent with the Qdrant implementation.

    The relevant test suites pass locally across Python 3.11–3.14, including regression tests, optional-dependency checks, and repeated pipeline executions.

    I'd appreciate your review and feedback when you have time. Could a maintainer also authorize the pending CI workflows if required?

    Thank you!

  3. mekala27-45 commented on Dec 5, 2026

    @mekala27-45
    Author

    Thanks for picking this up so quickly. Glad the report was useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions