Skip to content

fix(jina): use dataclass replace to avoid modifying input documents#2765

Merged
davidsbatista merged 2 commits into
deepset-ai:mainfrom
GunaPalanivel:fix/jina-document-embedder-immutability-2174
Jan 19, 2026
Merged

fix(jina): use dataclass replace to avoid modifying input documents#2765
davidsbatista merged 2 commits into
deepset-ai:mainfrom
GunaPalanivel:fix/jina-document-embedder-immutability-2174

Conversation

@GunaPalanivel
Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes

This PR fixes the Jina Document Embedder and Ranker to avoid mutating input Documents when setting embeddings or scores.

Changes Made

JinaDocumentEmbedder:

  • run() method: Use replace(doc, embedding=emb) instead of doc.embedding = emb

JinaRanker:

  • run() method: Use replace(doc, score=relevance_score) instead of doc.score = relevance_score

Note: JinaDocumentImageEmbedder already uses replace() and needed no changes.

Tests Added

  • test_run_does_not_modify_original_documents for document embedder
  • test_run_does_not_modify_original_documents for ranker

How did you test it?

cd integrations/jina
hatch run test:unit  # 51 passed, 3 deselected
hatch run fmt-check  # All checks passed

Notes for the reviewer

This follows the established pattern from:

This PR fixes the Jina document embedder and ranker to avoid mutating input
Documents when setting embeddings or scores.

Instead of mutating the original documents:
  doc.embedding = emb
  doc.score = relevance_score

We now create new document instances using dataclass replace:
  replace(doc, embedding=emb)
  replace(doc, score=relevance_score)

This follows the established pattern from haystack-ai/haystack#9693 and
aligns with other integrations (FastEmbed, Optimum, Nvidia, Bedrock, Cohere,
Google GenAI).

Related to: deepset-ai#2174
@GunaPalanivel GunaPalanivel requested a review from a team as a code owner January 19, 2026 14:05
@GunaPalanivel GunaPalanivel requested review from davidsbatista and removed request for a team January 19, 2026 14:05
Copy link
Copy Markdown
Contributor

@davidsbatista davidsbatista left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks @GunaPalanivel 👍🏽

@davidsbatista davidsbatista merged commit 44274ef into deepset-ai:main Jan 19, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants