Skip to content

Python: fix(vectors): validate AliasChoices fields when the default decoder rebuilds a record - #10246

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

DZH166 wants to merge 4 commits into
microsoft:mainfrom
DZH166:fix/vector-record-alias-choices

Conversation

@DZH166

@DZH166 DZH166 commented Nov 10, 2026

Copy link
Copy Markdown

Fixes #10230

Problem

_default_vector_model_decoder.decode_pydantic rebuilds the validation mapping with:

field.validation_alias if isinstance(field.validation_alias, str)
else field.alias if isinstance(field.alias, str)
else name

An AliasChoices validation alias is not a str and field.alias is None for an alias-only field, so the stored field name is handed to pydantic - which only accepts the declared alias choices. InMemoryCollection.upsert() (and every connector using the default decoder) therefore persists a record that cannot be read back:

upsert: ['one']
get:    ValidationError: record_id - Field required
        input_value={'id': 'one', 'text': 'plain sample', 'vector': [0.25, 0.75]}

Fix

Pick the first usable alias (AliasChoices -> its first choice, AliasPath -> its path, plain string alias -> itself), falling back to the field name.

Tests

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

pytest tests/core/test_vectors.py -q    # before: 1 failed, 277 passed   after: 278 passed

The new test round-trips a record whose id field declares only validation_alias=AliasChoices(...) through InMemoryCollection.

…ebuilds a record

The decoder used field.validation_alias only when it was a str and otherwise fell back to the stored field name, so an alias-only field (AliasChoices) was rejected by pydantic when InMemoryCollection.get() re-validated a stored record: upsert() succeeded and get() raised ValidationError. Pick the first usable alias.
Copilot AI balanced review requested due to automatic review settings November 10, 2026 18:44
@DZH166
DZH166 deployed to github-app-auth November 10, 2026 18:44 — with GitHub Actions Active
@DZH166
DZH166 deployed to github-app-auth November 10, 2026 18:44 — 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(vectors): validate AliasChoices fields when the default decoder rebuilds a record Python: fix(vectors): validate AliasChoices fields when the default decoder rebuilds a record 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 alias remapping can silently collapse overlapping choices and corrupt record IDs, while also breaking retrieval for models with alias validation disabled or AliasPath-based inputs.

3 open issues
What changed in this PR

This PR addresses #10230 by updating the Python vector-store default decoder to support Pydantic AliasChoices fields containing a string choice.

Changes:

  • Adds a helper that selects a string validation alias when rebuilding records.
  • Adds an InMemoryCollection round-trip regression test covering the record ID, text, and vector.
File Description
python/​packages/​core/​tests/​core/​test_vectors.py Adds collection round-trip coverage for an alias-only AliasChoices field.
python/​packages/​core/​agent_framework/​_vectors.py Uses a validation-key helper to select aliases during Pydantic record reconstruction.

Review effort: Balanced

else field.alias
if isinstance(field.alias, str)
else name: value[name]
_pydantic_validation_key(name, field): value[name]
Pydantic accepts, so it is used unchanged. ``AliasChoices`` declares several accepted aliases and
validates on any one of them, so the first string choice is used; keying such a field by its own
name only validates when the model also allows population by name, which declaring an alias does
not require. Nested ``AliasPath`` aliases keep the previous field-name fallback.
else field.alias
if isinstance(field.alias, str)
else name: value[name]
_pydantic_validation_key(name, field): value[name]
@eavanvalkenburg

Copy link
Copy Markdown
Member

DZH166 Thanks for the contribution. The linked issue #10230 does not currently have ready-for-implementation or explicit maintainer agreement on this implementation direction. Please wait for that direction before we review or merge this PR.

`record_id` is a validation alias, so the generated constructor only advertises `id` and
pyright/mypy/pyrefly all reject the call. Validate the aliased dict instead, which is the
same path the decoder takes in this test.
@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

Understood — #10230 stays the link, and I'll wait for ready-for-implementation before asking for review again.

One thing fixed in the meantime: the new test constructed the record with record_id=, which the typing gates reject because the generated constructor only advertises id. It now uses model_validate with the alias dict (commit fef4c65), so Test Typing Checks should be clean when the direction is agreed.

@eavanvalkenburg

Copy link
Copy Markdown
Member

DZH166 I’ve replied on #10230 and want that issue discussion to receive a response before we proceed with or approve this PR. Please hold changes here until the issue direction is settled.

@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 — 7ecde4df Deployed Nov 15, 2026 by DZH166 via add_label #27883
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]: AliasChoices vector records fail to round-trip through InMemoryCollection

3 participants