Skip to content

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

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

DZH166 wants to merge 1 commit 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]

This branch was successfully deployed

1 active deployment
github-app-auth — 04276e67 Deployed Nov 10, 2026 by DZH166 via team_check #9227
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

2 participants