.Net: Add tool_call_id to tool result messages in model diagnostics #13497
+5
−1
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.
Motivation and Context
Tool result messages in model diagnostics are missing the
tool_call_idproperty, making it difficult to correlate tool results with their corresponding tool calls in observability tools.Current behavior - tool result messages only have role and content:
{"role": "tool", "name": null, "content": "Partly Cloudy, 22°C", "tool_calls": []}After this change - includes
tool_call_idfor correlation:{"role": "tool", "name": "get_weather", "tool_call_id": "call_abc123", "content": "Partly Cloudy, 22°C", "tool_calls": []}This aligns with the OpenAI API format and enables observability tools (OpenInference, Arize, Galileo) to properly correlate tool calls with their results.
Description
Updated
ToGenAIConventionsFormatinModelDiagnostics.csto:FunctionResultContentin message itemstool_call_idfromFunctionResultContent.CallIdFunctionResultContent.FunctionNamefor thenamefield (instead ofAuthorNamewhich is always null for tool messages)This is a backward-compatible, additive change - existing consumers will simply ignore the new field.
Why Unit Testing Is Difficult
Unit testing this change is challenging due to
ModelDiagnosticsarchitecture:Static readonly field caching: The feature flags (
s_enableDiagnostics,s_enableSensitiveEvents) arestatic readonlyfields initialized at type load time viaAppContextSwitchHelper.GetConfigValue().No test isolation: Once
ModelDiagnosticsis loaded by any test in the assembly, the flag values are permanently cached. SettingAppContext.SetSwitchor environment variables after type initialization has no effect.Reflection blocked: .NET prevents modification of
initonlystatic fields after type initialization:Existing precedent: The test
GetInvalidResponseThrowsExceptionAndIsCapturedByDiagnosticsAsyncinOpenAIChatCompletionServiceTests.csis already skipped with[Fact(Skip = "Not working running in the console")]for the same reason.The change has been manually verified to produce the correct output.
Contribution Checklist
.Net: <description>