.Net: New Feature: support default value in VectorDataProperty
Differently from a normal data type, where properties can have a default value, when working with VectorStoreGenericDataModel, non nullable properties don't have the default .NET value (e.g. 0 for long) and cannot be defined with a default value either.
Example class:
public class Custom
{
[VectorStoreRecordKey(StoragePropertyName = "id")]
public string Id { get; set; }
[VectorStoreRecordVector(3)]
public ReadOnlyMemory<float> Vec { get; set; }
[VectorStoreRecordData(IsFilterable = true)]
public int Count { get; set; } // This has an implicit default of 0
[VectorStoreRecordData(IsFilterable = true)]
public int Count2 { get; set; } = 5; // Explicit default of 5
}
VectorStoreGenericDataModel example code throwing an exception, because the Count value is not set:
var db = new PostgresVectorStore(dataSource);
var def = new VectorStoreRecordDefinition
{
Properties = new List<VectorStoreRecordProperty>
{
new VectorStoreRecordKeyProperty("Id", typeof(string)),
new VectorStoreRecordVectorProperty("Vec", typeof(ReadOnlyMemory<float>)) { Dimensions = 3, DistanceFunction = DistanceFunction.CosineSimilarity },
new VectorStoreRecordDataProperty("Count", typeof(long))
}
};
var collection = db.GetCollection<string, VectorStoreGenericDataModel<string>>("tests", def);
await collection.CreateCollectionAsync().ConfigureAwait(false);
var record = new VectorStoreGenericDataModel<string>("1");
record.Vectors["Vec"] = new ReadOnlyMemory<float>([0.15f, 0.25f, 0.35f]);
await collection.UpsertAsync(record);
Exception:
Microsoft.Extensions.VectorData.VectorStoreOperationException: Call to vector store failed. ---> Npgsql.PostgresException (0x80004005): 23502: null value in column "Count" of relation "tests" violates not-null constraint
The exception can be fixed changing Count to nullable long? or setting a value
record.Data["Count"] = 123;
However, it would be better if
- The property used .NET default, e.g. 0 in this case
- The property could be configured with a default value, for instance:
var def = new VectorStoreRecordDefinition
{
Properties = new List<VectorStoreRecordProperty>
{
new VectorStoreRecordKeyProperty("Id", typeof(string)),
new VectorStoreRecordVectorProperty("Vec", typeof(ReadOnlyMemory<float>)) { Dimensions = 3, DistanceFunction = DistanceFunction.CosineSimilarity },
new VectorStoreRecordDataProperty("Count", typeof(long))
{
DefaultValue = 0,
}
}
};
Thanks @dluc, this is really useful. Here's how I see it:
The problem is that we create a non-nullable column in the database, and then, since the user dictionary contains no value, we don't send anything for the column when upserting, causing the exception. So this is bug is specific to dynamic mapping (with POCO mapping the property is always there and has the CLR default. Note also that non-relational databases probably don't error, but instead get null; this also isn't good, since trying to read the record back would likely throw when reading the null into e.g. int (TBC)>
So this seems like a bug with our handling of dynamic mapping. As I'll be doing lots of work in the area very shortly (it's the next item on my list), I'll make sure this is handled properly.
Adding something like DefaultValue on VectorStoreRecordDataProperty is quite different though - that would presumably correspond to setting up the default value in the database; AFAIK that's supported only on SQL databases. I don't see us doing this in the short term - we could introduce it via #10359.
Adding something like
DefaultValueon VectorStoreRecordDataProperty is quite different though - that would presumably correspond to setting up the default value in the database; AFAIK that's supported only on SQL databases. I don't see us doing this in the short term - we could introduce it via #10359.
When the code is
public int Count2 { get; set; } = 5;
do we set 5 default in the table schema?
- if Yes: I find this confusing, because I could edit the value, but the table schema would not change
- if No: then
DefaultValueonVectorStoreRecordDataPropertywould work the same way, initializing the runtime value, without affecting the table schema -- plus, this would work across all storage types
do we set 5 default in the table schema?
No. There's no way for us to know that the property is initialized to 5 - that would mean decompiling the actual code for that property (and it could be an arbitrarily-complex piece of code).
then DefaultValue on VectorStoreRecordDataProperty would work the same way, initializing the runtime value, without affecting the table schema -- plus, this would work across all storage types
I'm not following. How would we know when to use that DefaultValue setting, i.e. how do decide when the user set the property (and we should send what the .NET property has), and when the user didn't set the property (and we should send DefaultValue)?
How would we know when to use that DefaultValue setting, i.e. how do decide when the user set the property (and we should send what the .NET property has), and when the user didn't set the property (and we should send DefaultValue)?
When serializing before storing the record, we can detect if a property is missing from the property bag. Since we have the record definition, we can iterate through its properties - if a key is missing in the dictionary, it means the user didn’t set a value. In that case, we can apply the DefaultValue. If the key is present, we use the value the user provided.
@dluc that could make sense for dynamic mapping, where a property is missing - but my question was about what happens when mapping a .NET type with a .NET property - there's no notion of a "missing property" there (properties can be nullable but that's different).
@roji the issue I reported is around VectorStoreGenericDataModel and its internal dictionary, same for the request. Records based on .NET types can already have a default. The ask is to allow setting a default for properties handled by VectorStoreGenericDataModel.
Differently from a normal data type, where properties can have a default value, when working with VectorStoreGenericDataModel, non nullable properties don't have the default .NET value ...
Why not just handle missing properties in VectorStoreGenericDataModel exactly the same as unset properties on .NET types, i.e. just default to the CLR default (e.g. 0 for ints)? Aside from being more consistent with the non-dynamic behavior (with .NET types), this also frees the user from having to add an explicit "Default" setting for each and every property which they might leave unset?
BTW we could always introduce an explicit default configuration setting later if we really want to (this would override the CLR default above), but I'm not sure I see a need coming up...
Why not just handle missing properties in VectorStoreGenericData
you're surely thinking about a solution, but I don't understand the approach, could you share a snippet of code? is it a change to VectorStoreGenericData in MEVD, or code to write as a consumer of MEVD?
I'll try to find some time, but I'm deep into other stuff at the moment... I'll get around to this at some point.
Thanks for raising this issue in microsoft/semantic-kernel!
My Thoughts
This touches on an important aspect of the project. I've been using microsoft/semantic-kernel and have some thoughts:
Why this matters:
- Improves user experience
- Addresses a real pain point
- Aligns with project goals
Next Steps:
- Clarify requirements
- Identify edge cases
- Draft initial approach
I'm subscribed and happy to brainstorm further or contribute if needed. Great work maintaining this project!
@akrimm702 that seems like an automated response that doesn't contribute anything to this issue.
This issue is stale because it has been open for 90 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.