[C#] Expose cached Generator through IChatClient.GetService - #2475
Open
ZedingZhang (ZedingZhang) wants to merge 1 commit into
Open
[C#] Expose cached Generator through IChatClient.GetService#2475ZedingZhang (ZedingZhang) wants to merge 1 commit into
ZedingZhang (ZedingZhang) wants to merge 1 commit into
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
ZedingZhang (ZedingZhang)
force-pushed
the
fix/chat-client-generator-service
branch
from
August 27, 2026 02:35
db92295 to
387dcd9
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the C# OnnxRuntimeGenAIChatClient to expose the currently cached Generator instance via IChatClient.GetService<Generator>(serviceKey: null), enabling advanced callers to perform operations like RewindTo between requests while keeping ownership and lifecycle with the chat client.
Changes:
- Expose the cached
GeneratorfromIChatClient.GetServicewhen requested with a null service key. - Use
Volatile.Readfor consistent, thread-safe cache access alongside existingInterlockedupdates. - Add a C# test validating the
Generatorservice is unavailable before a request and available after a cached request completes.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/csharp/OnnxRuntimeGenAIChatClient.cs | Exposes the cached generator via GetService and documents borrowing/lifetime constraints. |
| test/csharp/TestOnnxRuntimeGenAIAPI.cs | Adds a unit test covering GetService<Generator>(null) behavior with caching enabled. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Author
|
@microsoft-github-policy-service agree |
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
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.
Description
Expose the currently cached
GeneratorthroughIChatClient.GetService<Generator>(). This enables callers to retrieve it between requests for operations such asRewindTo.The returned generator remains owned by the chat client and is documented as a borrowed reference. Callers must not dispose it, retain it across requests, or use it concurrently with an active request.
Changes
Generatorwhen requested without a service key.Volatile.Readto access the cache consistently with its existing interlocked updates.Testing
dotnet build src/csharp/Microsoft.ML.OnnxRuntimeGenAI.csproj --configuration Releasenet8.0andnetstandard2.0passed with 0 errors.dotnet test test/csharp/Microsoft.ML.OnnxRuntimeGenAI.Tests.csproj --configuration Release --no-buildTestChatClientGetServiceGeneratorFixes #1553