Repository navigation
Conversation
…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.
Contributor
There was a problem hiding this comment.
🟡 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
InMemoryCollectionround-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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Fixes #10230
Problem
_default_vector_model_decoder.decode_pydanticrebuilds the validation mapping with:An
AliasChoicesvalidation alias is not astrandfield.aliasisNonefor 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: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:The new test round-trips a record whose id field declares only
validation_alias=AliasChoices(...)throughInMemoryCollection.