Skip to content

Commit 4a66e35

Browse files
fix(langchain): preserve content-block shape in PIIMiddleware redaction (#39894)
`PIIMiddleware` redacted message content via `str(message.content)`. When `.content` is a list of content blocks rather than a plain string, `str(...)` produces the `repr` of the list — so a redacted user message was stored as the literal string: ``` "[{'type': 'text', 'text': 'my email is [REDACTED_EMAIL]'}]" ``` The PII itself was still detected and redacted; only the shape of the message was destroyed. Anyone sending multimodal or provider-native block content through an agent with `PIIMiddleware` had their messages silently flattened into that repr before reaching the model. `_process_content` now accepts either shape and walks content blocks, redacting the text of each block in place and leaving non-text blocks untouched. Plain-string content behaves exactly as before. While fixing the call sites, this also drops the hand-rebuilding of messages (`HumanMessage(content=..., id=..., name=...)`) in favor of `model_copy`. The enumerated rebuild had been quietly discarding every field it did not list — `additional_kwargs`, `response_metadata`, and so on — on any message that contained PII. ## Release note Fixed `PIIMiddleware` flattening list-of-content-blocks message content into its string `repr` during redaction. Redaction now preserves the original content shape, and no longer drops message fields such as `additional_kwargs` on redacted messages. --- *Written with the help of an AI agent (Claude Code).*
1 parent 38e2113 commit 4a66e35

2 files changed

Lines changed: 127 additions & 44 deletions

File tree

  • libs/langchain_v1
    • langchain/agents/middleware
    • tests/unit_tests/agents/middleware/implementations

‎libs/langchain_v1/langchain/agents/middleware/pii.py‎

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from functools import partial
66
from typing import TYPE_CHECKING, Any, ClassVar, Literal
77

8-
from langchain_core.messages import AIMessage, AnyMessage, BaseMessage, HumanMessage, ToolMessage
8+
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, ToolMessage
99
from langgraph.stream import StreamTransformer
1010
from typing_extensions import override
1111

@@ -660,13 +660,39 @@ def name(self) -> str:
660660
"""Name of the middleware."""
661661
return f"{self.__class__.__name__}[{self.pii_type}]"
662662

663-
def _process_content(self, content: str) -> tuple[str, list[PIIMatch]]:
664-
"""Apply the configured redaction rule to the provided content."""
665-
matches = self.detector(content)
666-
if not matches:
667-
return content, []
668-
sanitized = apply_strategy(content, matches, self.strategy)
669-
return sanitized, matches
663+
def _process_content(
664+
self, content: str | list[str | dict[str, Any]]
665+
) -> tuple[str | list[str | dict[str, Any]], bool]:
666+
"""Apply the configured redaction rule, preserving the shape of `content`.
667+
668+
Only string leaves are redacted: a plain-string `content` directly, and
669+
the `text` of each block in a block-list `content`. Non-text blocks pass
670+
through untouched, so block-list content keeps its structure instead of
671+
collapsing into the `repr` of the list.
672+
673+
Args:
674+
content: The message content to redact.
675+
676+
Returns:
677+
The redacted content and whether anything was rewritten.
678+
"""
679+
if isinstance(content, str):
680+
matches = self.detector(content)
681+
if not matches:
682+
return content, False
683+
return apply_strategy(content, matches, self.strategy), True
684+
685+
new_blocks: list[str | dict[str, Any]] = []
686+
changed = False
687+
for block in content:
688+
new_block: str | dict[str, Any] = block
689+
text = block if isinstance(block, str) else block.get("text")
690+
if isinstance(text, str) and (matches := self.detector(text)):
691+
redacted = apply_strategy(text, matches, self.strategy)
692+
new_block = redacted if isinstance(block, str) else {**block, "text": redacted}
693+
changed = True
694+
new_blocks.append(new_block)
695+
return (new_blocks, True) if changed else (content, False)
670696

671697
@hook_config(can_jump_to=["end"])
672698
@override
@@ -710,18 +736,12 @@ def before_model(
710736
break
711737

712738
if last_user_idx is not None and last_user_msg and last_user_msg.content:
713-
# Detect PII in message content
714-
content = str(last_user_msg.content)
715-
new_content, matches = self._process_content(content)
739+
new_content, changed = self._process_content(last_user_msg.content)
716740

717-
if matches:
718-
updated_message: AnyMessage = HumanMessage(
719-
content=new_content,
720-
id=last_user_msg.id,
721-
name=last_user_msg.name,
741+
if changed:
742+
new_messages[last_user_idx] = last_user_msg.model_copy(
743+
update={"content": new_content}
722744
)
723-
724-
new_messages[last_user_idx] = updated_message
725745
any_modified = True
726746

727747
# Check tool results if enabled
@@ -742,21 +762,12 @@ def before_model(
742762
if not tool_msg.content:
743763
continue
744764

745-
content = str(tool_msg.content)
746-
new_content, matches = self._process_content(content)
765+
new_content, changed = self._process_content(tool_msg.content)
747766

748-
if not matches:
767+
if not changed:
749768
continue
750769

751-
# Create updated tool message
752-
updated_message = ToolMessage(
753-
content=new_content,
754-
id=tool_msg.id,
755-
name=tool_msg.name,
756-
tool_call_id=tool_msg.tool_call_id,
757-
)
758-
759-
new_messages[i] = updated_message
770+
new_messages[i] = tool_msg.model_copy(update={"content": new_content})
760771
any_modified = True
761772

762773
if any_modified:
@@ -824,24 +835,13 @@ def after_model(
824835
if last_ai_idx is None or not last_ai_msg or not last_ai_msg.content:
825836
return None
826837

827-
# Detect PII in message content
828-
content = str(last_ai_msg.content)
829-
new_content, matches = self._process_content(content)
838+
new_content, changed = self._process_content(last_ai_msg.content)
830839

831-
if not matches:
840+
if not changed:
832841
return None
833842

834-
# Create updated message
835-
updated_message = AIMessage(
836-
content=new_content,
837-
id=last_ai_msg.id,
838-
name=last_ai_msg.name,
839-
tool_calls=last_ai_msg.tool_calls,
840-
)
841-
842-
# Return updated messages
843843
new_messages = list(messages)
844-
new_messages[last_ai_idx] = updated_message
844+
new_messages[last_ai_idx] = last_ai_msg.model_copy(update={"content": new_content})
845845

846846
return {"messages": new_messages}
847847

‎libs/langchain_v1/tests/unit_tests/agents/middleware/implementations/test_pii.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,89 @@ def test_with_agent(self) -> None:
551551
messages = result["messages"]
552552
assert any("[REDACTED_EMAIL]" in str(msg.content) for msg in messages)
553553

554+
def test_input_list_content_preserved(self) -> None:
555+
"""List-of-content-blocks input is redacted in place, not stringified.
556+
557+
Regression: `str(msg.content)` flattened block-list content into its
558+
`repr`, so the stored content became the literal string
559+
`"[{'type': 'text', 'text': '...'}]"`.
560+
"""
561+
middleware = PIIMiddleware("email", strategy="redact")
562+
state = AgentState[Any](
563+
messages=[
564+
HumanMessage(content=[{"type": "text", "text": "my email is [email protected]"}])
565+
]
566+
)
567+
568+
result = middleware.before_model(state, Runtime())
569+
570+
assert result is not None
571+
content = result["messages"][0].content
572+
assert content == [{"type": "text", "text": "my email is [REDACTED_EMAIL]"}]
573+
assert "[email protected]" not in str(content)
574+
575+
def test_output_list_content_preserved(self) -> None:
576+
"""List-of-content-blocks AI output is redacted in place, not stringified."""
577+
middleware = PIIMiddleware(
578+
"email", strategy="redact", apply_to_input=False, apply_to_output=True
579+
)
580+
state = AgentState[Any](
581+
messages=[AIMessage(content=[{"type": "text", "text": "reach me at [email protected]"}])]
582+
)
583+
584+
result = middleware.after_model(state, Runtime())
585+
586+
assert result is not None
587+
content = result["messages"][0].content
588+
assert content == [{"type": "text", "text": "reach me at [REDACTED_EMAIL]"}]
589+
assert "[email protected]" not in str(content)
590+
591+
def test_redaction_preserves_message_fields(self) -> None:
592+
"""Redacting keeps fields the hooks never enumerated when rebuilding messages."""
593+
middleware = PIIMiddleware("email", strategy="redact")
594+
original = HumanMessage(
595+
content="my email is [email protected]",
596+
id="msg_1",
597+
name="alice",
598+
additional_kwargs={"source": "web"},
599+
)
600+
state = AgentState[Any](messages=[original])
601+
602+
result = middleware.before_model(state, Runtime())
603+
604+
assert result is not None
605+
redacted = result["messages"][0]
606+
assert redacted.content == "my email is [REDACTED_EMAIL]"
607+
assert redacted.id == "msg_1"
608+
assert redacted.name == "alice"
609+
assert redacted.additional_kwargs == {"source": "web"}
610+
611+
def test_tool_result_list_content_preserved(self) -> None:
612+
"""List-of-content-blocks tool results are redacted in place."""
613+
middleware = PIIMiddleware(
614+
"email", strategy="redact", apply_to_input=False, apply_to_tool_results=True
615+
)
616+
state = AgentState[Any](
617+
messages=[
618+
HumanMessage("Search for user"),
619+
AIMessage(
620+
content="",
621+
tool_calls=[ToolCall(name="search", args={}, id="call_1", type="tool_call")],
622+
),
623+
ToolMessage(
624+
content=[{"type": "text", "text": "found: [email protected]"}],
625+
tool_call_id="call_1",
626+
),
627+
]
628+
)
629+
630+
result = middleware.before_model(state, Runtime())
631+
632+
assert result is not None
633+
content = result["messages"][2].content
634+
assert content == [{"type": "text", "text": "found: [REDACTED_EMAIL]"}]
635+
assert "[email protected]" not in str(content)
636+
554637

555638
class TestCustomDetector:
556639
"""Test custom detector functionality."""

0 commit comments

Comments
 (0)