Add Structured Output Support for Microsoft.Extensions.AI IChatClient #180
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.
Summary
This PR adds support for structured JSON output when using the
IChatClientinterface from Microsoft.Extensions.AI/Microsoft Agent Framework. It enables theChatOptions.ResponseFormatproperty to work correctly with Anthropic's native structured output API.Changes
New
OutputFormatclass (Anthropic.SDK/Messaging/OutputFormat.cs)output_formatAPI parameterjson_schematype with schema definitionUpdated
MessageParametersOutputFormatproperty for structured JSON output configurationUpdated
FunctionclassStrictproperty to enable strict mode for tool definitionsCopyFrommethod to copy the Strict propertyUpdated
ChatClientHelperChatResponseFormatJsonto Anthropic'soutput_formatparameteradditionalProperties: falseon all object types (required by Anthropic's API)Updated
MessagesEndpointstructured-outputs-2025-11-13beta header when structured output or strict tools are usedNew extension methods (
ChatOptionsExtensions.cs)WithStrictTools()- Enable strict mode for tool definitionsGetStrictToolsEnabled()- Check if strict tools are enabledUsage Example
Microsoft Agent Framework Compatibility
This enables full compatibility with
ChatClientAgentfrom the Microsoft Agent Framework:Technical Notes
structured-outputs-2025-11-13beta header is automatically added when structured output or strict tools are detectedadditionalProperties: falseto all object types in schemas, as required by Anthropic's structured output APIResponseFormatorWithStrictTools()), ensuring existing code continues to work unchangedTests
Added comprehensive unit tests in
StructuredOutputTests.cscovering:🤖 Generated with Claude Code
Co-Authored-By: Claude [email protected]