-
Notifications
You must be signed in to change notification settings - Fork 187
[Fix][Refactor]Migrate FoundryLocal integration to official Microsoft.AI.Foundry.Local.WinML SDK #535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
weiyuanyue
wants to merge
34
commits into
main
Choose a base branch
from
milly/flmerge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
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
…l SDK's ExcludeExtraLibs pattern
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.
Summary
This PR resolves a critical production-breaking bug that blocked all FoundryLocal model downloads in AI Dev Gallery after upgrading to Foundry Local v0.8.x+. We migrate from fragile custom HTTP API calls to the official
Microsoft.AI.Foundry.Local.WinMLSDK (v0.8.2.1), restoring full functionality and establishing a resilient foundation for future compatibility.Impact: Users can now reliably download, prepare, and use FoundryLocal models. The system is significantly more robust against upstream API changes.
Background & Root Cause
In Jul 2025, Foundry Local changed the internal format of a critical
Namefield in its HTTP API response. While this change was handled internally by Foundry Local, it was not communicated externally, causing silent incompatibility with AIDG's direct HTTP-based integration.After upgrading to Foundry Local v0.8.x:
Business Impact:
Solution: SDK Migration
To eliminate this entire class of failures and future-proof the integration, we migrate to the official SDK:
Key Benefits:
Technical Changes
1. Core Architecture Refactor
FoundryClient.cs(297 lines changed)FoundryLocalManager,ICatalog, andIModelinterfacesIDisposablefor proper resource cleanupFoundryLocalModelProvider.cs(138 lines changed)OpenAISDK with custom service URL constructionEnsureModelReadyAsync()to prevent deadlocks in synchronous contextsNamefield to stableAliasfield_preparedModelsdictionary)FoundryLocalChatClientAdapter.cs(119 new lines)Microsoft.Extensions.AI.IChatClientinterfaceChatOptionsparameter mapping (temperature, top-p, frequency penalty, etc.)MaxTokensdefault (1024) to prevent empty model outputs2. Dependency Updates
Directory.Packages.propsMicrosoft.AI.Foundry.Local.WinML(v0.8.2.1)Microsoft.ML.OnnxRuntimeGenAI.Managed&.WinML(v0.10.1 → v0.11.4)nuget.confighttps://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/ORT/nuget/v3/index.json*Foundry*packages to ORT feed3. Build Configuration
ExcludeExtraLibs.props(35 new lines)Directory.Build.propsIDisposableAnalyzerswarnings (IDISP001, IDISP003, IDISP017)4. Telemetry & Observability
FoundryLocalDownloadEvent.cs(55 new lines)5. Data Model Simplification
FoundryCatalogModel.cs(89 lines reduced)Tag,ProviderType,PromptTemplate,Uri)Name,DisplayName,Alias,FileSizeMb,License,ModelId,RuntimeDeleted Files:
FoundryServiceManager.cs(81 lines)—replaced by SDK'sFoundryLocalManagerFoundryJsonContext.cs(18 lines)—no longer needed with SDKUtils.cs(40 lines)—functionality absorbed into SDKMigration Logic & Business Flow
Before (HTTP-based)
After (SDK-based)
Inference Flow
Known Limitations & Follow-up Work
IDisposableAnalyzers Build Warnings
Microsoft.AI.Foundry.Local.WinML(v0.8.2.1) includesIDisposableAnalyzers(v4.0.8) as a transitive dependencyIDisposablepattern usageDirectory.Build.propsand project filesBreaking Changes
None for end users. Internal API changes only:
fl://<Name>→fl://<Alias>(transparent to users)Functional Testing
Model Catalog & Discovery
DisplayName,Alias, andFileSizeMbAliasproperly (multiple variants per alias)GetAllModelsInCatalog()returns full catalog including non-downloaded modelsModel Download Flow
CancellationTokenworks correctlyModel Preparation & Loading
EnsureModelReadyAsync()successfully prepares models for first useEnsureModelReadyAsync()calls for same model are handled safely (semaphore lock)GetPreparedModel()returnsnullfor unprepared modelsGetPreparedModel()returns validIModelfor prepared modelsLoadAsync) completes successfully on x64LoadAsync) completes successfully on ARM64Chat Inference & Streaming
GetIChatClient()throws appropriate exception when model not preparedMaxTokensparameter properly limits output lengthChatOptionsparameters (temperature, top-p, frequency penalty) are correctly appliedTelemetry & Observability
FoundryLocalDownloadEventincludes correctModelAlias,Success, andErrorMessageLogLevel.Criticalas expectedCode Generation (TODO: Next PR)
VerifyGetIChatClientString()returns valid, compilable C# codeConfirm generated code includes proper SDK initialization patternVerify generated code uses correct modelAlias(not obsoleteName)Confirm generated code includes necessaryusingstatementsRegression Testing
Multi-Provider Compatibility
UI/UX Flows
Project Generation(TODO: Next PR)
Test project generation with FoundryLocal models produces correct code samplesVerify generated projects reference correct NuGet packages (Microsoft.AI.Foundry.Local.WinML,Microsoft.Extensions.AI)Confirm generated projects compile without errorsVerifyNugetPackageReferencesproperty returns correct package listPlatform-Specific Validation
Edge Cases & Error Handling
Service Availability
IsAvailable()returnsfalsewhen FoundryLocal not installed/initializedInvalid Inputs
GetIChatClient()with invalid URL format throws clear exceptionDownloadModel()with non-FoundryCatalogModel returnsfalseEnsureModelReadyAsync()with non-existent alias throws clear exceptionResource Management
FoundryClient.Dispose()is called properly (no resource leaks)_prepareLocksemaphore is disposed correctly