haystack icon indicating copy to clipboard operation
haystack copied to clipboard

MetadataRouter.from_dict mutates the caller's serialized configuration

Open Quantum-Enigma-dot opened this issue 1 month ago • 5 comments

Describe the bug

MetadataRouter.from_dict() mutates the serialized dictionary supplied by the caller when output_type is present.

During the first deserialization, the serialized output_type string inside data["init_parameters"] is replaced with a live Python type. Reusing the same serialized dictionary in a second MetadataRouter.from_dict() call then fails because deserialize_type() receives an already-deserialized object instead of a string.

Error message

TypeError: can only concatenate str (not "types.GenericAlias") to str

To Reproduce

from haystack.components.routers import MetadataRouter
from haystack.dataclasses import Document


router = MetadataRouter(
    rules={
        "english": {
            "field": "meta.language",
            "operator": "==",
            "value": "en",
        }
    },
    output_type=list[Document],
)

data = router.to_dict()

MetadataRouter.from_dict(data)
MetadataRouter.from_dict(data)

Actual behavior

The first call changes:

data["init_parameters"]["output_type"]

from its serialized string representation into a Python generic type.

The second call fails with a TypeError.

Expected behavior

MetadataRouter.from_dict() should not mutate the caller-owned serialized dictionary.

The same serialized configuration should remain reusable across repeated deserialization calls.

Additional context

A similar caller-mutation issue was previously corrected for ConditionalRouter.from_dict() and BranchJoiner.from_dict() in PR #12935.

I reproduced this against current main. I have also prepared a focused regression test and minimal fix and would like to submit the corresponding pull request.

FAQ Check

  • I reproduced the issue against current main.
  • I searched existing issues and pull requests and did not find an exact report or active implementation.

System

  • OS: Windows 11
  • Python: 3.11
  • Haystack commit: 76fc2d3d7492ec243eed40eb939a836f2a8b29da

Quantum-Enigma-dot avatar Oct 24 '26 06:10 Quantum-Enigma-dot

I reproduced this on macOS / Python 3.11 at 76fc2d3d. It doesn't seem specific to MetadataRouter: I ran to_dict() → from_dict(d) → from_dict(d) across components, and the input dict was mutated (and the second call failed) for ListJoiner, InMemoryBM25Retriever, InMemoryEmbeddingRetriever, DocumentWriter, ChatPromptBuilder, MockChatGenerator, DocumentSplitter, DocumentPreprocessor, PyPDFToDocument, PDFMinerToDocument, DOCXToDocument, OpenAIChatGenerator, Agent, FaithfulnessEvaluator, LLMEvaluator, Tool and SuperComponent. Only BranchJoiner (fixed in #12935) leaves its input unchanged. Pipeline.from_dict() is not affected, so this hits direct component-level from_dict() calls and nested deserialization.

Would maintainers prefer per-component fixes like #12935, or a shared approach (e.g. shallow-copying init_parameters before the *_inplace helpers)? Happy to help with follow-up PRs once #13421 lands, so as not to conflict with it.

Hs1000 avatar Oct 24 '26 11:10 Hs1000

Thanks for testing this across the wider component set. That confirms the caller-mutation pattern extends beyond the MetadataRouter-specific case reported here.

I kept #13421 intentionally scoped to MetadataRouter so the regression test and behavior change remain focused. I’ll wait for maintainer guidance on whether the preferred direction is to merge this narrow fix first and address the other components separately, or handle the mutation protection through a shared deserialization approach.

Thanks as well for avoiding conflicting work while #13421 is under review.

Quantum-Enigma-dot avatar Oct 24 '26 16:10 Quantum-Enigma-dot

Hi! I've been investigating the remaining deserialization mutation cases, particularly in the compaction components.

I noticed the related fixes in #13421 and #13471. Would it be useful if I picked up the remaining compaction cases as a separate contribution once #13421 is merged?

I'd be happy to investigate and prepare a focused fix with regression tests, while avoiding overlap with the existing work.

Thanks!

Jenil1905 avatar Oct 31 '26 11:10 Jenil1905

Thanks for checking the remaining cases and for avoiding overlap with #13421. A separate focused contribution for the compaction components sounds reasonable to me, but I’ll leave the preferred scope and sequencing to the maintainers.

Quantum-Enigma-dot avatar Oct 31 '26 11:10 Quantum-Enigma-dot

Hi, I have a focused bug-fix PR open for MetadataRouter deserialization input mutation: #13421. The regression and fix have been independently verified. Could someone advise whether the maintainers prefer the narrow component fix or a shared deserialization approach?

Quantum-Enigma-dot avatar Nov 01 '26 06:11 Quantum-Enigma-dot