Skip to content

Honor FunctionInvocationContext.Terminate when the function throws - #7801

Merged
jozkee merged 2 commits into
dotnet:mainfrom
gjz22:fix/terminate-on-function-exception
Oct 8, 2026
Merged

jozkee merged 2 commits into
dotnet:mainfrom
gjz22:fix/terminate-on-function-exception

Conversation

@gjz22

@gjz22 gjz22 commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7800

Problem

If an AIFunction sets FunctionInvocationContext.Terminate = true and then throws, the loop does not terminate. FunctionInvocationProcessor.ProcessSingleFunctionCallAsync hard-coded terminate: false in its exception path.

Fix

One-line change: pass context.Terminate in the exception path, as the success path already does.

Behavior

  • FunctionInvokingChatClient (streaming, non-streaming, approved-function path) stops after the error result.
  • When calls run one at a time, the remaining calls in that turn are skipped, matching non-throwing termination.
  • With AllowConcurrentInvocation, all calls in the turn finish, then the loop stops.
  • FunctionInvokingRealtimeClientSession stops without sending results back, matching non-throwing termination.
  • FunctionInvocationResult can now have Terminate == true with Status == Exception.

Tests

Added 6 tests (chat serial/concurrent/streaming, exception preserved on result, approvals, realtime). All 6 fail without the fix. The full Microsoft.Extensions.AI.Tests project passes on net10.0 (767/767).

ðŸĪ– Generated with Claude Code

Microsoft Reviewers: Open in CodeFlow

FunctionInvocationProcessor hard-coded terminate: false in the exception
path, so a function that set Terminate = true and then threw did not stop
the function-invocation loop. Use context.Terminate as the success path
does. Affects FunctionInvokingChatClient (streaming, non-streaming,
approvals) and FunctionInvokingRealtimeClientSession.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gjz22
gjz22 requested a review from a team as a code owner October 3, 2026 19:01
Copilot AI balanced review requested due to automatic review settings October 3, 2026 19:01
@github-actions github-actions Bot added the area-ai Microsoft.Extensions.AI libraries label Oct 3, 2026

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.

Copilot review overview

ðŸŸĄ Changes recommended

The exception path terminates correctly but omits the established termination diagnostic log.

Review effort: Balanced
Findings: 1 Low severity

Open (1)
What changed in this PR

Ensures function-invocation loops honor termination requests even when functions throw.

Changes:

  • Preserves Terminate on exception results.
  • Adds chat, approval, concurrency, streaming, and realtime coverage.
File Description
FunctionInvocationProcessor.cs Propagates termination on exceptions.
FunctionInvokingChatClientTests.cs Tests chat invocation scenarios.
FunctionInvokingChatClientApprovalsTests.cs Tests approved-function termination.
FunctionInvokingRealtimeClientTests.cs Tests realtime termination.

ðŸ’Ą Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Addresses review feedback: LogFunctionRequestedTermination was only
emitted on the success path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dotnet-comment-bot

Copy link
Copy Markdown
Collaborator

‾ïļ Found issues ‾ïļ

Project Coverage Type Expected Actual
Microsoft.Extensions.Diagnostics.Testing Line 99 98.65 ðŸ”ŧ
Microsoft.Extensions.Telemetry Line 93 92.64 ðŸ”ŧ
Microsoft.Extensions.AI.OpenAI Line 75 68.32 ðŸ”ŧ
Microsoft.Extensions.AI.OpenAI Branch 75 57.59 ðŸ”ŧ
Microsoft.Extensions.DataIngestion.MarkItDown Line 75 4.46 ðŸ”ŧ
Microsoft.Extensions.DataIngestion.MarkItDown Branch 75 0 ðŸ”ŧ
Microsoft.Extensions.Diagnostics.ResourceMonitoring Line 99 96.03 ðŸ”ŧ
Microsoft.Extensions.Diagnostics.ResourceMonitoring Branch 99 92.76 ðŸ”ŧ
Microsoft.Extensions.Diagnostics.ResourceMonitoring.Kubernetes Line 99 97.73 ðŸ”ŧ
Microsoft.Extensions.ServiceDiscovery.Dns Line 75 71.61 ðŸ”ŧ
Microsoft.Extensions.ServiceDiscovery.Abstractions Line 75 42.11 ðŸ”ŧ
Microsoft.Extensions.ServiceDiscovery.Abstractions Branch 75 42.86 ðŸ”ŧ
Microsoft.Extensions.ServiceDiscovery Line 75 68.57 ðŸ”ŧ
Microsoft.Extensions.ServiceDiscovery Branch 75 71.43 ðŸ”ŧ
Microsoft.Extensions.ServiceDiscovery.Yarp Line 75 73.85 ðŸ”ŧ
Microsoft.Extensions.ServiceDiscovery.Yarp Branch 75 70 ðŸ”ŧ
Microsoft.Extensions.VectorData.Abstractions Line 75 37.39 ðŸ”ŧ
Microsoft.Extensions.VectorData.Abstractions Branch 75 22.73 ðŸ”ŧ

🎉 Good job! The coverage increased 🎉
Update MinCodeCoverage in the project files.

Project Expected Actual
Microsoft.Extensions.Http.Diagnostics 94 95
Microsoft.Gen.BuildMetadata 97 100
Microsoft.Gen.MetadataExtractor 57 73
Microsoft.Gen.MetricsReports 67 69
Microsoft.Extensions.AI.Abstractions 82 86
Microsoft.Extensions.AI.Evaluation.NLP 0 78
Microsoft.Extensions.Caching.Hybrid 82 89
Microsoft.Extensions.DataIngestion 75 89
Microsoft.Extensions.DataIngestion.Markdig 75 90
Microsoft.Extensions.Http.Resilience 97 100

Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=1622283&view=codecoverage-tab

@gjz22

gjz22 commented Oct 3, 2026

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree company="Microsoft"

@jeffhandley
jeffhandley requested a review from jozkee October 8, 2026 18:20

@jozkee jozkee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks.

@jozkee
jozkee merged commit 0c82e68 into dotnet:main Oct 8, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FunctionInvokingChatClient: FunctionInvocationContext.Terminate is ignored when the function throws

5 participants