Skip to content

Python: fix(sessions): validate AliasChoices state fields when restoring a session snapshot - #10247

Open
DZH166 wants to merge 1 commit into
microsoft:mainfrom
DZH166:fix/session-state-alias-choices
Open

DZH166 wants to merge 1 commit into
microsoft:mainfrom
DZH166:fix/session-state-alias-choices

Conversation

@DZH166

@DZH166 DZH166 commented Nov 10, 2026

Copy link
Copy Markdown

Problem

_default_state_decoder.decode_pydantic passes value.model_dump() field names straight to cls.model_validate (only dropping the type tag):

return cls.model_validate({k: v for k, v in value.items() if k != "type"})

A registered pydantic state type whose field declares only validation_alias=AliasChoices(...) therefore serializes into the snapshot but cannot be restored:

serialized: {'demo': {'entry': {'note': 'hello', 'type': 'repro:AliasedState'}}}
restore  -> ValueError: Failed to deserialize registered state type 'repro:AliasedState' at state.demo.entry.

AgentSession.to_dict() succeeds and AgentSession.from_dict() raises, so a persisted session value is lost. String alias= 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 (AliasChoices first choice / AliasPath / string alias, falling back to the field name) before validating.

Tests

python/packages/core/tests/core/test_sessions.py:

pytest tests/core/test_sessions.py -q    # before: 1 failed, 190 passed, 1 skipped   after: 191 passed, 1 skipped

…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.
Copilot AI balanced review requested due to automatic review settings November 10, 2026 18:45
@DZH166
DZH166 deployed to github-app-auth November 10, 2026 18:45 — with GitHub Actions Active
@DZH166
DZH166 deployed to github-app-auth November 10, 2026 18:45 — with GitHub Actions Active
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Nov 10, 2026
@github-actions github-actions Bot changed the title fix(sessions): validate AliasChoices state fields when restoring a session snapshot Python: fix(sessions): validate AliasChoices state fields when restoring a session snapshot Nov 10, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 AliasChoices field.
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

1 active deployment
github-app-auth — e5c98299 Deployed Nov 10, 2026 by DZH166 via team_check #9228
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants