Repository navigation
.Net: Propagate FunctionResult.Metadata to AgentRole.Tool ChatMessageContent.Metadata - #13855
Gustavo Torres (gmtorres) wants to merge 6 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR enables propagation of user defined FunctionResult.Metadata created or modified in auto function invocation filters into the ChatHistory tool message metadata, making it accessible to downstream consumers like OrchestrationResponseCallback.
Changes:
- Populate
ChatMessageContent.Metadatafor tool messages withAutoFunctionInvocationContext.Result.Metadatawhen adding function results to chat history. - Add a unit test validating metadata flow from a filter set
FunctionResult.Metadatato the resulting toolChatMessageContent.Metadata.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| dotnet/src/InternalUtilities/connectors/AI/FunctionCalling/FunctionCallsProcessor.cs | Propagates FunctionResult.Metadata into the tool ChatMessageContent added to ChatHistory. |
| dotnet/src/SemanticKernel.UnitTests/Utilities/AIConnectors/FunctionCallsProcessorTests.cs | Adds coverage to verify function result metadata is preserved on the tool message in chat history. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Automated Code Review
Reviewers: 4 | Confidence: 91%
✓ Correctness
This PR propagates FunctionResult.Metadata to the ChatMessageContent added to chat history after function call execution. The implementation accesses resultContext.Context.Result.Metadata to pass as the metadata parameter to the ChatMessageContent constructor. The code is correct: Result is a non-nullable property on AutoFunctionInvocationContext (always initialized), Metadata is nullable IReadOnlyDictionary which the ChatMessageContent constructor accepts as nullable, and the test properly validates the round-trip through a filter that sets metadata on the FunctionResult. The change is minimal and well-tested.
✓ Security Reliability
This PR propagates FunctionResult.Metadata from function invocation results to the ChatMessageContent added to chat history. The change is minimal and safe. The metadata parameter on the ChatMessageContent constructor is nullable (IReadOnlyDictionary<string, object?>?), so null metadata in the error path (when ExecuteFunctionCallAsync catches an exception and the FunctionResult retains its initial null metadata) is handled gracefully. No injection risks, resource leaks, or unsafe deserialization are introduced. The metadata is user-controlled (set by function implementations or auto-invocation filters), so propagating it to chat history is an intentional and appropriate design choice. The test adequately verifies the new behavior by exercising a filter that sets custom metadata and asserting it appears on the tool message.
✓ Test Coverage
The PR adds metadata propagation from FunctionResult to ChatMessageContent in the tool message added to chat history. The test covers the key scenario where a filter sets metadata on the FunctionResult and verifies it appears on the resulting ChatMessageContent. The test is well-structured with meaningful assertions. However, there are two missing edge-case tests: (1) the default case where no filter modifies metadata (metadata should be null), and (2) the error/exception path where AddFunctionCallResultToChatHistory is also called but the FunctionResult has no metadata. These are not blocking since the null-metadata case is implicitly covered by all existing tests that don't set metadata, but an explicit null-metadata assertion would strengthen confidence.
✗ Design Approach
The single-line change propagates
FunctionResult.Metadatato theChatMessageContentthat wraps the tool response, which is the intended behavior. However, the implementation has a subtle design flaw: by passingmetadatato theChatMessageContentconstructor together withcontent, the metadata is inadvertently copied into the auto-createdTextContentitem insideItems. This happens because theContentproperty setter (called from the constructor afterthis.Metadatais already set) creates anew TextContent(metadata: this.Metadata). As a result, the function result metadata ends up on bothmessage.Metadataandmessage.Items[0](the TextContent), but not onmessage.Items[1](theFunctionResultContent), which is the semantically correct carrier for function-invocation metadata. The fix is simple: construct the message without metadata (so the TextContent is created with null metadata), then assignmessage.Metadataseparately. The test is well-structured and validates the right observable behavior.
Flagged Issues
- Passing
metadatain theChatMessageContentconstructor causes the same metadata dictionary to be copied into the auto-createdTextContentitem (via theContentsetter'snew TextContent(metadata: this.Metadata)path). Function-result metadata is semantically tied to the function invocation, not to a text representation item. The correct approach is to setmessage.Metadataafter construction, so the TextContent stays metadata-free while the message-level metadata still propagates correctly.
Suggestions
- Consider adding a test for the error/exception path (function throws) to verify that
AddFunctionCallResultToChatHistorydoes not surface unexpected metadata on the tool message—currently the error path still flows through this method and will propagate whatever metadata exists on theFunctionResultat the time of failure. - Consider adding a test that verifies
ChatMessageContent.Metadatais null when no filter modifies theFunctionResultmetadata (the default path), ensuring the change doesn't accidentally introduce non-null metadata where none existed before.
Automated review by gmtorres's agents
|
@microsoft-github-policy-service agree |
Move FunctionResult.Metadata from the tool message to the generated FunctionResultContent. The metadata is then scoped to one function result, and it can no longer collide with keys that connectors read from the tool message metadata. - FunctionCallsProcessor sets the metadata on the result item, with and without chat history. - FunctionResultContent copies the metadata when created from a FunctionResult. - KernelFunctionInvokingChatClient attaches the metadata to the result content built for the tool message. The function output is unchanged.
Motivation and Context
There is currently no way to propagate custom metadata from function invocation filters to
OrchestrationResponseCallback. Right nowAddFunctionCallResultToChatHistorydiscardsFunctionResult.Metadataand it is not exposed toOrchestrationResponseCallback.This enables scenarios where we want to consume user-defined metadata in invocation filters, such as function result token count ReductionFilter, exception handling, etc
Description
FunctionResult.Metadatais now copied to theMetadataof theFunctionResultContentitem that represents the function result. From there it reaches the chat history andOrchestrationResponseCallback.The metadata is placed on the result item and not on the tool
ChatMessageContent, for these reasons:ChatCompletionsToolCall.Idfrom it to build the tool reply, so a function metadata key with that name would change the request.Reading the metadata from a message:
Coverage
FunctionCallsProcessorFunctionCallsProcessor.InvokeFunctionCallsAsyncIChatClient(UseKernelFunctionInvocation), streaming and non streamingKernelFunctionInvokingChatClientattaches the metadata to the result content that it builds for the tool message. The value returned by the function, which is also what telemetry records, is not changed.FunctionCallContent.InvokeAsyncFunctionResultContentcreated from aFunctionResultcopies the metadata.Not covered
FunctionCallsProcessorpaths. The existing conversion toMicrosoft.Extensions.AImessages throws for such keys.Tests
FunctionCallsProcessorTests: sequential and concurrent invocation, a reserved OpenAI key that stays on the item, and the path without chat history.FunctionResultContentTests: the constructor copies the metadata.ServiceConversionExtensionsTests: theIChatClientpath, streaming and non streaming. The function output and its trace log are unchanged. Keys that differ only by letter case are accepted. Calls that share one call ID keep their own metadata. A result content returned by the function is copied and not modified.Validation
Run on Windows with the .NET SDK 10.0.404, Debug configuration.
dotnet build dotnet\src\SemanticKernel.Abstractions\SemanticKernel.Abstractions.csproj -c Debug --no-restore --tl:off --warnaserrordotnet build dotnet\src\SemanticKernel.UnitTests\SemanticKernel.UnitTests.csproj -c Debug -f net10.0 --no-restore --tl:off --warnaserrordotnet test dotnet\src\SemanticKernel.UnitTests\SemanticKernel.UnitTests.csproj -c Debug -f net10.0 --no-build --no-restoredotnet test dotnet\src\Connectors\Connectors.OpenAI.UnitTests\Connectors.OpenAI.UnitTests.csproj -c Debug -f net10.0KernelTests.FunctionUsageMetricsAreCapturedByTelemetryAsExpected, which passed on five reruns and does not use the changed code.dotnet format <project> --no-restore --verify-no-changes --include <changed files>forSemanticKernel.AbstractionsandSemanticKernel.UnitTestsSemanticKernel.UnitTestson a local merge of this branch withmainataa46eddff. Source link was disabled because the merge was built from an exported copy.Contribution Checklist