Background and motivation
Currently there's no abstract way to represent different outputs from tools that aren't fully recognized and have specific abstractions, like Anthropic WebSearch and WebFetch although we have different tools already that share a same abstraction representation.
ImageGenerationToolResultContent.Outputs
CodeInterpreterToolResultContent.Outputs
McpServerToolResultContent.Output
FunctionResultContent(Result + Exception)
The proposal is to have a AIToolResultContent abstraction that we can represent for tool results that may have multiple outputs without having to resort to the AIContent.RawRepresentation for an unrecognized tool result.
In other words.
WebFetchToolResult even not existing as a concrete class in Abstractions, could be better represented as a AIToolResultContent with multiple Outputs as AIContents before any specialization like WebSearchResultContent : AItoolResultContent is added to the library.
Benefits
This middle term abstraction has the benefit of being able to represent unrecognized tool results via the IList<AIContent>? Outputs property. Allowing callers to be able to detect failure/results from tools without necessarily needing to break glass.
API Proposal
/// <summary>
/// Represents an abstract tool call result by a hosted service.
/// </summary>
/// <remarks>
/// This content type represents any hosted AI service tool invocation result.
/// </remarks>
[Experimental("MEAI001")]
public class HostedToolResultContent : AIContent
{
/// <summary>
/// Initializes a new instance of the <see cref="HostedToolResultContent"/> class.
/// </summary>
public HostedToolResultContent ()
{
}
/// <summary>
/// Gets or sets the unique identifier of the image generation item.
/// </summary>
public virtual string? ToolId { get; set; }
/// <summary>
/// Gets or sets the resulting contents from the tool.
/// </summary>
/// <remarks>
/// Content is typically <see cref="ErrorContent"/> for any tool error, <see cref="DataContent"/> for data contents, or <see cref="UriContent"/> for remotely hosted contents, or <see cref="TextContent"/> for text based contents.
/// </remarks>
public virtual IList<AIContent>? Outputs { get; set; }
}
Existing tools that has the IList<AIContent>? Outputs property can be updated to be specializations of this class.
API Usage
// Fancy the value
var message = await client.GetResponseAsync(...);
// Fetching WebSearchResult tools (there's no such tool result representation in abstractions).
for (var toolResultContent in message.Contents.OfType<HostedToolResultContent>())
{
switch (toolResultContent)
{
McpServerToolResultContent: { break; // MCP }
ImageGenerationToolResultContent: { break; // Image Result }
CodeInterpreterToolResultContent: { break; // Code interpreter result }
default: {
// A singular logic can be used across all tools or just for non-identified tools
// Identifying if any tool error happened.
var error = toolResultContent.Outputs.FirstOrDefault(c => c is ErrorContent);
// Generated binary data contents
var generatedDataContents = toolResultContent.Outputs.OfType<DataContent>();
// Generated text based contents
var textContents = toolResultContent.Outputs.OfType<TextContent>();
}
}
}
Alternative Designs
No response
Risks
No response
Background and motivation
Currently there's no abstract way to represent different outputs from tools that aren't fully recognized and have specific abstractions, like Anthropic
WebSearchandWebFetchalthough we have different tools already that share a same abstraction representation.ImageGenerationToolResultContent.OutputsCodeInterpreterToolResultContent.OutputsMcpServerToolResultContent.OutputFunctionResultContent(Result + Exception)The proposal is to have a
AIToolResultContentabstraction that we can represent for tool results that may have multiple outputs without having to resort to theAIContent.RawRepresentationfor an unrecognized tool result.In other words.
WebFetchToolResult even not existing as a concrete class in Abstractions, could be better represented as a
AIToolResultContentwith multipleOutputsasAIContents before any specialization likeWebSearchResultContent : AItoolResultContentis added to the library.Benefits
This middle term abstraction has the benefit of being able to represent unrecognized tool results via the
IList<AIContent>? Outputsproperty. Allowing callers to be able to detect failure/results from tools without necessarily needing to break glass.API Proposal
Existing tools that has the
IList<AIContent>? Outputsproperty can be updated to be specializations of this class.API Usage
Alternative Designs
No response
Risks
No response