Commit 27c93be
authored
.Net Improve type safety: Return TRecord instead of object in ITextSearch.GetSearchResultsAsync (#13318)
This PR enhances the type safety of the `ITextSearch<TRecord>` interface
by changing the `GetSearchResultsAsync` method to return
`KernelSearchResults<TRecord>` instead of `KernelSearchResults<object>`.
This improvement eliminates the need for manual casting and provides
better IntelliSense support for consumers.
## Motivation and Context
The current implementation of
`ITextSearch<TRecord>.GetSearchResultsAsync` returns
`KernelSearchResults<object>`, which requires consumers to manually cast
results to the expected type. This reduces type safety and degrades the
developer experience by losing compile-time type checking and
IntelliSense support.
This change aligns the return type with the generic type parameter
`TRecord`, providing the expected strongly-typed results that users of a
generic interface would anticipate.
## Changes Made
### Interface (ITextSearch.cs)
- Changed `ITextSearch<TRecord>.GetSearchResultsAsync` return type from
`KernelSearchResults<object>` to `KernelSearchResults<TRecord>`
- Updated XML documentation to reflect strongly-typed return value
- Legacy `ITextSearch` interface (non-generic) remains unchanged,
continuing to return `KernelSearchResults<object>` for backward
compatibility
### Implementation (VectorStoreTextSearch.cs)
- Added new `GetResultsAsTRecordAsync` helper method returning
`IAsyncEnumerable<TRecord>`
- Updated generic interface implementation to use the new strongly-typed
helper
- Retained `GetResultsAsRecordAsync` method for the legacy non-generic
interface
### Tests (VectorStoreTextSearchTests.cs)
- Updated 3 unit tests to use strongly-typed `DataModel` or
`DataModelWithRawEmbedding` instead of `object`
- Improved test assertions to leverage direct property access without
casting
- All 19 tests pass successfully
## Breaking Changes
**Interface Change (Experimental API):**
- `ITextSearch<TRecord>.GetSearchResultsAsync` now returns
`KernelSearchResults<TRecord>` instead of `KernelSearchResults<object>`
- This interface is marked with `[Experimental("SKEXP0001")]`,
indicating that breaking changes are expected during the preview period
- Legacy `ITextSearch` interface (non-generic) is unaffected and
maintains full backward compatibility
## Benefits
- **Improved Type Safety**: Eliminates runtime casting errors by
providing compile-time type checking
- **Enhanced Developer Experience**: Full IntelliSense support for
TRecord properties and methods
- **Cleaner Code**: Consumers no longer need to cast results from object
to the expected type
- **Consistent API Design**: Generic interface now behaves as expected,
returning strongly-typed results
- **Zero Impact on Legacy Code**: Non-generic ITextSearch interface
remains unchanged
## Testing
- All 19 existing unit tests pass
- Updated tests demonstrate improved type safety with direct property
access
- Verified both generic and legacy interfaces work correctly
- Confirmed zero breaking changes to non-generic ITextSearch consumers
## Related Work
This PR is part of the Issue #10456 multi-PR chain for modernizing
ITextSearch with LINQ-based filtering:
- PR #13175: Foundation (ITextSearch<TRecord> interface) - Merged
- PR #13179: VectorStoreTextSearch + deprecation pattern - In Review
- **This PR (2.1)**: API refinement for improved type safety
- PR #13188-13191: Connector migrations (Bing, Google, Tavily, Brave) -
Pending
- PR #13194: Samples and documentation - Pending
All PRs target the `feature-text-search-linq` branch for coordinated
release.
## Migration Guide for Consumers
### Before (Previous API)
```csharp
ITextSearch<DataModel> search = ...;
KernelSearchResults<object> results = await search.GetSearchResultsAsync("query", options);
foreach (var obj in results.Results)
{
var record = (DataModel)obj; // Manual cast required
Console.WriteLine(record.Name);
}
```
### After (Improved API)
```csharp
ITextSearch<DataModel> search = ...;
KernelSearchResults<DataModel> results = await search.GetSearchResultsAsync("query", options);
foreach (var record in results.Results) // Strongly typed!
{
Console.WriteLine(record.Name); // Direct property access with IntelliSense
}
```
## Checklist
- [x] Changes build successfully
- [x] All unit tests pass (19/19)
- [x] XML documentation updated
- [x] Breaking change documented (experimental API only)
- [x] Legacy interface backward compatibility maintained
- [x] Code follows project coding standards
Co-authored-by: Alexander Zarei <alzarei@users.noreply.github.com>1 parent ebd579d commit 27c93be
File tree
3 files changed
+35
-10
lines changed- dotnet/src
- SemanticKernel.Abstractions/Data/TextSearch
- SemanticKernel.Core/Data/TextSearch
- SemanticKernel.UnitTests/Data
3 files changed
+35
-10
lines changedLines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
| 44 | + | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| |||
Lines changed: 24 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
213 | 213 | | |
214 | 214 | | |
215 | 215 | | |
216 | | - | |
| 216 | + | |
217 | 217 | | |
218 | 218 | | |
219 | 219 | | |
220 | | - | |
| 220 | + | |
221 | 221 | | |
222 | 222 | | |
223 | 223 | | |
| |||
367 | 367 | | |
368 | 368 | | |
369 | 369 | | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
370 | 392 | | |
371 | 393 | | |
372 | 394 | | |
| |||
Lines changed: 9 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| 81 | + | |
81 | 82 | | |
82 | 83 | | |
83 | | - | |
| 84 | + | |
84 | 85 | | |
85 | 86 | | |
86 | 87 | | |
| 88 | + | |
87 | 89 | | |
88 | 90 | | |
89 | 91 | | |
| |||
117 | 119 | | |
118 | 120 | | |
119 | 121 | | |
| 122 | + | |
120 | 123 | | |
121 | 124 | | |
122 | | - | |
| 125 | + | |
123 | 126 | | |
124 | 127 | | |
125 | 128 | | |
| 129 | + | |
126 | 130 | | |
127 | 131 | | |
128 | 132 | | |
| |||
270 | 274 | | |
271 | 275 | | |
272 | 276 | | |
273 | | - | |
| 277 | + | |
274 | 278 | | |
275 | 279 | | |
276 | 280 | | |
277 | 281 | | |
278 | | - | |
| 282 | + | |
279 | 283 | | |
280 | 284 | | |
281 | 285 | | |
282 | | - | |
283 | | - | |
| 286 | + | |
284 | 287 | | |
285 | 288 | | |
286 | 289 | | |
| |||
0 commit comments