Add custom state field reducers (append vs. replace) support#53
Open
MadaraUchiha-314 wants to merge 1 commit intomainfrom
Open
Add custom state field reducers (append vs. replace) support#53MadaraUchiha-314 wants to merge 1 commit intomainfrom
MadaraUchiha-314 wants to merge 1 commit intomainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support for custom reducer semantics on state fields, allowing YAML authors to explicitly choose between
appendandreplacebehavior for list fields in agent state schemas.Changes
Schema Enhancement: Added optional
reducerfield toStateFieldDefwith values"append"or"replace"None(default) uses intelligent defaults:"append"formessageslist fields,"replace"for everything elsereducersetting (always use replace semantics)State Class Builder Refactor: Updated
_build_state_class()to respect per-field reducer settingsMessagesStatebase classTypedDictwith explicitAnnotatedtypes for all fieldsreducer: appendon list types getAnnotated[list, add_messages](LangGraph's smart message reducer)Documentation & Tests: Added comprehensive plan for:
reducerfield and its semanticsImplementation Details
The key insight is that
MessagesStateis just aTypedDictwithAnnotated[list, add_messages]on the messages field. By switching to explicit annotations on all fields, we gain full control over reducer behavior while maintaining backward compatibility—existing YAML withoutreducersettings continues to work identically.The
appendreducer uses LangGraph'sadd_messagesfunction (smart deduplication by message ID) rather than simple list concatenation, making it suitable for message-heavy agent state patterns.Backward Compatibility
✅ Fully backward compatible. Existing agent definitions without
reducerfields maintain current behavior:messageslist field →append(unchanged)replace(unchanged)https://claude.ai/code/session_01R7PWjTfJ943jkev5DmAWV2