[GH-ISSUE #3957] Why does AgentMiddleware allow untracked side-effect mutations of state in hooks instead of enforcing reducer-only updates? #2771

Open
opened 2026-06-05 17:26:42 -04:00 by yindo · 0 comments
Owner

Originally created by @goodpostidea-tech on GitHub (May 12, 2026).
Original GitHub issue: https://github.com/langchain-ai/docs/issues/3957

Type of issue

question

Language

Python

Description

I noticed that before_model can mutate state directly (e.g., modifying HumanMessage.content via reference) and these mutations persist even when None is returned, bypassing the reducer mechanism entirely. This creates a 'shadow update' path that isn't tracked by the framework. What was the rationale behind not deep-copying state before passing it to hooks, or using frozen/immutable structures? Was there a performance concern, or a deliberate choice to trust developers with mutable access?

Originally created by @goodpostidea-tech on GitHub (May 12, 2026). Original GitHub issue: https://github.com/langchain-ai/docs/issues/3957 ### Type of issue question ### Language Python ### Description I noticed that before_model can mutate state directly (e.g., modifying HumanMessage.content via reference) and these mutations persist even when None is returned, bypassing the reducer mechanism entirely. This creates a 'shadow update' path that isn't tracked by the framework. What was the rationale behind not deep-copying state before passing it to hooks, or using frozen/immutable structures? Was there a performance concern, or a deliberate choice to trust developers with mutable access?
yindo added the langchainexternal labels 2026-06-05 17:26:42 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/docs#2771