[Fix] NullReferenceException when loading scenarios with mixed model requirements #533
+2
−2
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.
When attempting to load the Retrieval Augmented Generation sample, a
NullReferenceExceptionis thrown at line 103 inScenarioPage.xaml.cs:Exception Details:
Then crash in some environment #530 ;
Root Cause
The code originally assumed that if the 1st sample in a scenario has
Model2Types, then all samples in that scenario would haveModel2Types(see line 100 comment). However, this assumption is violated in theTextRetrievalAugmentedGenerationscenario:Model2Types = [ModelType.EmbeddingModel](requires language model + embedding model)Model2Types(uses Windows API for built-in RAG)The code only checked if
samples[0].Model2Types != nullbut then performedSelectManyon all samples, causing a null reference exception when encounteringKnowledgeRetrieval.Investigation
I analyzed all scenarios with multiple samples across the codebase:
Finding:
TextRetrievalAugmentedGenerationis the only scenario where samples have inconsistentModel2Typesdefinitions.The only other sample using
Model2TypesisMultipose.xaml.cs(inImageDetectPosesscenario), but it's the sole sample in that scenario, so no conflict occurs.Solution
Added a
.Where(s => s.Model2Types != null)filter beforeSelectManyto safely handle scenarios with mixed model requirements:This fix:
IsModelFromTypeslogic (which already handlestypes == nullat line 340-342)Testing
The fix allows
RetrievalAugmentedGenerationto load successfully while properly collecting model types from all samples that define them.