Skip to content

Implement guided generation for AnthropicLanguageModel#110

Merged
mattt merged 2 commits intomainfrom
mattt/anthropic-guided-generation
Feb 5, 2026
Merged

Implement guided generation for AnthropicLanguageModel#110
mattt merged 2 commits intomainfrom
mattt/anthropic-guided-generation

Conversation

@mattt
Copy link
Owner

@mattt mattt commented Feb 5, 2026

Related to #27

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements guided generation support for AnthropicLanguageModel, enabling it to generate structured data types annotated with @Generable instead of just strings. This addresses issue #27, which requested support for the @Generable protocol and @Guide macro.

Changes:

  • Removed fatal error guards that prevented non-String content generation in both streaming and non-streaming response methods
  • Added schema conversion logic to translate GenerationSchema to Anthropic's JSONSchema format
  • Implemented structured response handling with proper JSON parsing for both streaming and non-streaming modes
  • Added helper functions for empty content handling, partial snapshot creation, and schema format conversion
  • Refactored tool schema conversion to reuse the new schema conversion function

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
Sources/AnyLanguageModel/Models/AnthropicLanguageModel.swift Implements guided generation by adding schema-to-JSON conversion, output_config parameter construction, and structured response parsing for both streaming and non-streaming modes
Tests/AnyLanguageModelTests/AnthropicLanguageModelTests.swift Adds test struct AnthropicStructuredForecast and two new test cases (streamingStructured and structuredResponse) to verify structured generation functionality

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mattt mattt merged commit 87101b8 into main Feb 5, 2026
3 checks passed
@mattt mattt deleted the mattt/anthropic-guided-generation branch February 5, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant