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.
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
| _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 |
|
DZH166 Thanks for the contribution. This PR does not link a verified microsoft/agent-framework issue, and the body does not explain a valid trivial or inapplicable exception. Please link the repository issue that establishes the direction, or update the PR body with the specific reason an issue is not applicable. |
The constructor only exposes the field name `note` to type checkers, so `note_text=` fails pyright/mypy while the alias is exactly what this test exercises. Validate the aliased dict instead: same runtime path, and the typing gates stay green.
|
Thanks — the repository issue is now linked: #10287 (state snapshot restore for a registered Pydantic type whose field only declares a validation alias, with a runnable reproduction). The PR body carries While linking it I also fixed the typing gates: the new test built the aliased model through its constructor ( |


Fixes #10287
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: