Skip to content

Conversation

@weiyuanyue
Copy link
Contributor

@weiyuanyue weiyuanyue commented Dec 13, 2025

When attempting to load the Retrieval Augmented Generation sample, a NullReferenceException is thrown at line 103 in ScenarioPage.xaml.cs:

modelDetailsList.Add(samples.SelectMany(s => s.Model2Types!).ToList());

Exception Details:

System.NullReferenceException: 'Object reference not set to an instance of an object.'
at System.Collections.Generic.SegmentedArrayBuilder<T>.AddNonICollectionRangeInlined(...)

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 have Model2Types (see line 100 comment). However, this assumption is violated in the TextRetrievalAugmentedGeneration scenario:

  1. RetrievalAugmentedGeneration - Defines Model2Types = [ModelType.EmbeddingModel] (requires language model + embedding model)
  2. KnowledgeRetrieval - Does not define Model2Types (uses Windows API for built-in RAG)

The code only checked if samples[0].Model2Types != null but then performed SelectMany on all samples, causing a null reference exception when encountering KnowledgeRetrieval.

Investigation

I analyzed all scenarios with multiple samples across the codebase:

Scenario Sample Count Model2Types Usage
ImageDescribeImage 2 All False
ImageDetectObjects 2 All False
ImageGenerateImage 3 All False
ImageIncreaseFidelity 2 All False
TextGenerateText 2 All False
TextRetrievalAugmentedGeneration 2 Mixed (True/False) ⚠️
TextSemanticSearch 3 All False

Finding: TextRetrievalAugmentedGeneration is the only scenario where samples have inconsistent Model2Types definitions.

The only other sample using Model2Types is Multipose.xaml.cs (in ImageDetectPoses scenario), but it's the sole sample in that scenario, so no conflict occurs.

Solution

Added a .Where(s => s.Model2Types != null) filter before SelectMany to safely handle scenarios with mixed model requirements:

modelDetailsList.Add(samples.Where(s => s.Model2Types != null).SelectMany(s => s.Model2Types!).ToList());

This fix:

  • ✅ Resolves the immediate crash
  • ✅ Maintains backward compatibility
  • ✅ Aligns with the existing IsModelFromTypes logic (which already handles types == null at line 340-342)
  • ✅ Makes the code more robust for future scenarios with mixed model requirements
  • ✅Updated the comment to reflect the actual behavior instead of the violated assumption.

Testing

The fix allows RetrievalAugmentedGeneration to load successfully while properly collecting model types from all samples that define them.

@weiyuanyue weiyuanyue marked this pull request as ready for review December 13, 2025 05:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants