Skip to content

MetadataRouter.from_dict mutates the caller's serialized configuration #13420

Closed

Description

@Quantum-Enigma-dot

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

Activity

  1. Hs1000 commented on Oct 24, 2026

    @Hs1000

    I reproduced this on macOS / Python 3.11 at 76fc2d3. 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.

  2. Quantum-Enigma-dot commented on Oct 24, 2026

    @Quantum-Enigma-dot
    ContributorAuthor

    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.

  3. Jenil1905 commented on Oct 31, 2026

    @Jenil1905

    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!

  4. Quantum-Enigma-dot commented on Oct 31, 2026

    @Quantum-Enigma-dot
    ContributorAuthor

    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.

  5. Quantum-Enigma-dot commented on Nov 1, 2026

    @Quantum-Enigma-dot
    ContributorAuthor

    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?

  6. added
    P3Low priority, leave it in the backlog
    on Nov 5, 2026
  7. assigned and unassigned on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3Low priority, leave it in the backlog

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions