Add Structured Output Support for Microsoft.Extensions.AI IChatClient#180
Merged
tghamm merged 2 commits intotghamm:mainfrom Jan 22, 2026
Merged
Conversation
…ork) Implements ChatOptions.ResponseFormat support for the IChatClient interface, enabling seamless integration with Microsoft Agent Framework's structured output capabilities. Features: - Map ChatResponseFormat.ForJsonSchema() to Anthropic's output_format parameter - Automatic additionalProperties: false injection for JSON schemas (required by Anthropic API) - Strict tool use support with Function.Strict property - Auto-add structured-outputs-2025-11-13 beta header when needed - New WithStrictTools() extension method for explicit strict mode control Files changed: - OutputFormat.cs: New model class for output_format API parameter - MessageParameters.cs: Added OutputFormat property - Function.cs: Added Strict property for strict tool use - ChatClientHelper.cs: ResponseFormat mapping and schema processing - MessagesEndpoint.cs: Beta header handling for structured outputs - ChatOptionsExtensions.cs: WithStrictTools() extension method - StructuredOutputTests.cs: 16 unit tests for new functionality Backwards compatible: No changes to direct API usage or existing IChatClient behavior without ResponseFormat. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Contributor
Author
|
@tghamm Please let me know if there are any changes required for this to be merged |
Owner
|
@TJaenichen awesome addition, thank you very much! |
Contributor
Author
|
Fantastic, thank you! |
|
@TJaenichen thanks for updating the SDK! I am testing the update on my usecase. It seems the following way of setting the response format does not work: opts.ResponseFormat <- ChatResponseFormat.ForJsonSchema(typeof<Completion>)I get an error like the following: Note the above approach works with MS.Ext.AI when targeting OpenAI. It would be appreciated if we can code the same way across both backends for structured output. Thanks |
This was referenced Jan 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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]