Repository navigation
Conversation
…ssion snapshot The default state decoder passed model_dump() field names straight to model_validate, so a registered state type whose field only declares validation_alias=AliasChoices(...) serialized fine but failed to restore, losing the persisted session value. Map stored names back to their aliases.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The remapping can overwrite distinct saved values when aliases overlap and break restoration for models that disable alias validation, risking data loss or restoration failures.
2 open issues
What changed in this PR
This PR updates Python session restoration to support registered Pydantic state models whose fields use string aliases or string-based AliasChoices.
Changes:
- Map serialized field names to validation aliases before restoring state.
- Add a session round-trip regression test for an
AliasChoicesfield.
| File | Description |
|---|---|
python/packages/core/tests/core/test_sessions.py |
Adds round-trip coverage for registered state using AliasChoices. |
python/packages/core/agent_framework/_sessions.py |
Adds alias selection and applies it during Pydantic state decoding. |
Review effort: Balanced
Comment on lines
+298
to
+299
| _pydantic_validation_key(key, cls.model_fields[key]) if key in cls.model_fields else key: value | ||
| for key, value in payload.items() |
| # The default encoder dumps field names, so map each one onto a name this model accepts; | ||
| # an alias-only field rejects its own field name and failed to restore from a snapshot. | ||
| return cls.model_validate({ | ||
| _pydantic_validation_key(key, cls.model_fields[key]) if key in cls.model_fields else key: value |
This branch was successfully deployed
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.


Problem
_default_state_decoder.decode_pydanticpassesvalue.model_dump()field names straight tocls.model_validate(only dropping thetypetag):A registered pydantic state type whose field declares only
validation_alias=AliasChoices(...)therefore serializes into the snapshot but cannot be restored:AgentSession.to_dict()succeeds andAgentSession.from_dict()raises, so a persisted session value is lost. Stringalias=fields are broken the same way; no existing test covered aliases for state types.Fix
Map the dumped field names back to their validation aliases (
AliasChoicesfirst choice /AliasPath/ string alias, falling back to the field name) before validating.Tests
python/packages/core/tests/core/test_sessions.py: