Skip to content

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

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

DZH166 wants to merge 4 commits into
microsoft:mainfrom
DZH166:fix/session-state-alias-choices

Conversation

@DZH166

@DZH166 DZH166 commented Nov 10, 2026 •

Copy link
Copy Markdown

Fixes #10287

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
@eavanvalkenburg

Copy link
Copy Markdown
Member

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.
@DZH166
DZH166 deployed to github-app-auth November 12, 2026 03:29 — with GitHub Actions Active
@DZH166
DZH166 deployed to github-app-auth November 12, 2026 03:30 — with GitHub Actions Active
@DZH166

DZH166 commented Nov 12, 2026

Copy link
Copy Markdown
Author

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

While linking it I also fixed the typing gates: the new test built the aliased model through its constructor (note_text=), which pyright/mypy reject because the generated signature only advertises the field name. It now goes through model_validate with the alias, which is the same path the decoder takes (commit 76a9cba).

@DZH166
DZH166 deployed to github-app-auth November 15, 2026 10:51 — with GitHub Actions Active

This branch was successfully deployed

1 active deployment
github-app-auth — bdb819cb Deployed Nov 15, 2026 by DZH166 via add_label #27882
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.

Python: [Bug]: A registered Pydantic state type with a validation alias cannot be restored from a session snapshot

3 participants