haystack icon indicating copy to clipboard operation
haystack copied to clipboard

check for `ChromaDocumentStore` dataclass in-place mutation warning

Open davidsbatista opened this issue 7 months ago • 5 comments

After adding the dataclass in-place mutation warning in PR https://github.com/deepset-ai/haystack/pull/10650, running tests in core-integrations surfaces multiple in-repo usages where we still mutate dataclass instances in place.

We should replace these with dataclasses.replace (or other non-mutating patterns) to avoid warnings and unintended side effects.

Actions

  • running tests
  • catching warnings,
  • fixing the mutating behaviour.

davidsbatista avatar Apr 13 '26 08:04 davidsbatista

In production, we've dealt with similar issues while working with large-scale document stores and dataclasses. Mutating dataclass instances in-place can lead to subtle bugs and performance issues, especially when dealing with concurrent access. We've found that using dataclasses.replace helps maintain immutability and avoids unexpected side effects. Additionally, consider using tools like mypy with the dataclass_transform plugin to catch these issues early in the development process.

Here are a few steps to address this:

  • Identify Mutations: Run tests and use static analysis tools to identify all instances where dataclass instances are mutated in-place.
  • Replace Mutations: Use dataclasses.replace to create new instances with the desired changes. This ensures immutability and makes the code easier to reason about.
  • Update Tests: Ensure that all tests pass after making these changes. This includes both unit tests and integration tests.
  • Document Changes: Update the documentation to reflect the immutability of dataclass instances and provide guidance on how to work with them.

smqd19 avatar Apr 22 '26 09:04 smqd19

Hi @davidsbatista

I'd like to work on the ChromaDocumentStore fix for this issue. I recently contributed to Haystack (PR #11175).

I'll follow the same pattern used in the other integrations: replacing in-place mutations with dataclasses.replace()

Could you confirm this is still available?

ShubhamGond105 avatar Apr 23 '26 11:04 ShubhamGond105

Hi @davidsbatista 👋

I investigated ChromaDocumentStore for in-place dataclass mutations. After checking all files in integrations/chroma/:

  • No doc.meta[] = assignments found ✅
  • No doc.embedding = assignments found ✅
  • No doc.content = assignments found ✅
  • Source code (document_store.py) also clean ✅

The only DeprecationWarnings found are from chromadb's own internal code (asyncio.iscoroutinefunction), not from Haystack's test code.

Could this issue be closed, or is there something specific I missed?

ShubhamGond105 avatar Apr 23 '26 18:04 ShubhamGond105

Hi @davidsbatista 👋

I ran the full Chroma test suite to investigate this issue. Here are my findings:

Test Results:

  • 133 passed ✅
  • 40 skipped
  • 0 failures

Warnings Analysis: All DeprecationWarnings come from chromadb's own internal code, NOT from Haystack's ChromaDocumentStore:

  • asyncio.iscoroutinefunction → chromadb telemetry
  • Direct api_key configuration → chromadb embeddings
  • _TestEmbeddingFunction __init__ → chromadb API
  • EmbeddingFunction name() → chromadb API

Code Investigation:

  • No doc.meta[x] = mutations found ✅
  • No doc.embedding = mutations found ✅
  • No doc.content = mutations found ✅
  • Source code (document_store.py) clean ✅

Conclusion: ChromaDocumentStore has NO in-place dataclass mutations. This issue can be closed ✅

Happy to provide any additional details if needed!

Image

ShubhamGond105 avatar Apr 27 '26 18:04 ShubhamGond105

Thanks for your contributions!

davidsbatista avatar Apr 28 '26 08:04 davidsbatista