Skip to content

[C#] Expose cached Generator through IChatClient.GetService - #2475

Open
ZedingZhang (ZedingZhang) wants to merge 1 commit into
microsoft:mainfrom
ZedingZhang:fix/chat-client-generator-service
Open

[C#] Expose cached Generator through IChatClient.GetService#2475
ZedingZhang (ZedingZhang) wants to merge 1 commit into
microsoft:mainfrom
ZedingZhang:fix/chat-client-generator-service

Conversation

@ZedingZhang

Copy link
Copy Markdown

Description

Expose the currently cached Generator through IChatClient.GetService<Generator>(). This enables callers to retrieve it between requests for operations such as RewindTo.

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

  • Return the cached Generator when requested without a service key.
  • Use Volatile.Read to access the cache consistently with its existing interlocked updates.
  • Document the lifetime and concurrency constraints of the returned generator.
  • Add C# coverage using the repository-bundled tiny GPT-2 model.

Testing

  • dotnet build src/csharp/Microsoft.ML.OnnxRuntimeGenAI.csproj --configuration Release
    • net8.0 and netstandard2.0 passed with 0 errors.
  • dotnet test test/csharp/Microsoft.ML.OnnxRuntimeGenAI.Tests.csproj --configuration Release --no-build
    • 9 passed, 10 skipped because optional external models were unavailable, 0 failed.
  • TestChatClientGetServiceGenerator
    • 1 passed, 0 failed.
  • macOS arm64 native Release build completed successfully.

Fixes #1553

Copilot AI lite review requested due to automatic review settings August 27, 2026 02:32
@ZedingZhang
ZedingZhang (ZedingZhang) requested a review from a team as a code owner August 27, 2026 02:32
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@ZedingZhang
ZedingZhang (ZedingZhang) force-pushed the fix/chat-client-generator-service branch from db92295 to 387dcd9 Compare August 27, 2026 02:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Generator from IChatClient.GetService when requested with a null service key.
  • Use Volatile.Read for consistent, thread-safe cache access alongside existing Interlocked updates.
  • Add a C# test validating the Generator service 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.

@ZedingZhang

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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.

Enabling access to the generator when using the OnnxRuntimeGenAIChatClient

2 participants