Skip to content

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

Merged
kunal-vaishnavi merged 1 commit into
microsoft:mainfrom
ZedingZhang:fix/chat-client-generator-service
Oct 6, 2026
Merged

kunal-vaishnavi merged 1 commit into
microsoft:mainfrom
ZedingZhang:fix/chat-client-generator-service

Conversation

@ZedingZhang

Copy link
Copy Markdown
Contributor

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.

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
Contributor Author

@microsoft-github-policy-service agree

@ZedingZhang
ZedingZhang (ZedingZhang) force-pushed the fix/chat-client-generator-service branch from 387dcd9 to d3cffb4 Compare September 16, 2026 02:17
@ZedingZhang

Copy link
Copy Markdown
Contributor Author

Hi Baiju Meswani (@baijumeswani), friendly ping on this small C# PR addressing #1553. It exposes the cached Generator through IChatClient.GetService, with lifetime/concurrency documentation and tiny-model test coverage. The GitHub Actions runs are still awaiting maintainer approval, and Azure Pipelines also needs an authorized trigger. Could you please take a look or route it to the appropriate reviewer? Thanks!

@kunal-vaishnavi kunal-vaishnavi 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.

Thanks for your contribution!

@ZedingZhang

Copy link
Copy Markdown
Contributor Author

Thanks for the review, kunal-vaishnavi! The new C# test passed in CI. The only failing check is Linux CUDA, where test_engine_run_releases_gil[cpu] failed at worker_progressed.is_set() (76 passed, 1 failed). This appears unrelated to the C#-only changes. Could you please rerun the failed job?

@kunal-vaishnavi

Copy link
Copy Markdown
Contributor

/azp run macOS arm64 Build

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@ZedingZhang
ZedingZhang (ZedingZhang) force-pushed the fix/chat-client-generator-service branch from d3cffb4 to 0deaf4a Compare September 29, 2026 07:33
@kunal-vaishnavi

Copy link
Copy Markdown
Contributor

/azp run macOS arm64 Build

@kunal-vaishnavi
kunal-vaishnavi enabled auto-merge (squash) October 6, 2026 22:15
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@kunal-vaishnavi
kunal-vaishnavi merged commit 6f737f3 into microsoft:main Oct 6, 2026
16 checks passed
@ZedingZhang
ZedingZhang (ZedingZhang) deleted the fix/chat-client-generator-service branch October 6, 2026 22:41
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

3 participants