Skip to content

Conversation

@eastriverlee
Copy link
Contributor

@eastriverlee eastriverlee commented Dec 24, 2025

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 structured output generation for LlamaLanguageModel and MLXLanguageModel by adding constrained token sampling to generate JSON that conforms to a schema. The implementation includes comprehensive tests covering various data types and structures.

Key changes:

  • Added ConstrainedJSONGenerator that uses token-level sampling to generate schema-conformant JSON
  • Implemented TokenBackend protocol with adapters for both Llama and MLX models
  • Enhanced GenerationGuide to store constraint values for min/max on numbers and arrays
  • Extended GenerationSchema with character validation and schema prompt generation

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
Tests/AnyLanguageModelTests/StructuredGenerationTests.swift Comprehensive test suite covering simple types, nested structs, enums, arrays, and optionals across all supported model types
Tests/AnyLanguageModelTests/GenerableMacroTests.swift Added round-trip tests for enums, nested structs, and arrays
Sources/AnyLanguageModelMacros/GenerableMacro.swift Refactored guide extraction to use a structured Constraints type and properly parse numeric ranges and array count constraints
Sources/AnyLanguageModel/StructuredGeneration.swift New file implementing token-level constrained JSON generation with TokenBackend protocol and ConstrainedJSONGenerator
Sources/AnyLanguageModel/Models/SystemLanguageModel.swift Updated to use schema-based generation for non-String types and added conversion to FoundationModels.DynamicGenerationSchema
Sources/AnyLanguageModel/Models/MLXLanguageModel.swift Implemented MLXTokenBackend and structured JSON generation with proper token sampling and repetition penalty handling
Sources/AnyLanguageModel/Models/LlamaLanguageModel.swift Implemented LlamaTokenBackend and structured JSON generation with batch-based decoding and sampler integration
Sources/AnyLanguageModel/GenerationSchema.swift Added schemaPrompt() method, character validation for JSON strings, improved node equality checking, and support for constraint propagation
Sources/AnyLanguageModel/GenerationGuide.swift Made GenerationGuide store actual constraint values (min/max, minCount/maxCount) for use during schema generation

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

@mattt
Copy link
Owner

mattt commented Jan 5, 2026

@eastriverlee Thank you for your contribution! And thank you for your patience. I'll have a chance to look a this soon.

@mattt
Copy link
Owner

mattt commented Jan 20, 2026

@eastriverlee Thanks again for your patience. I just rebased, resolving the conflicts as best I could. I recently merged #59, which takes a slightly different approach for schema conversion. I'm working to harmonize these implementations now...

@mattt
Copy link
Owner

mattt commented Feb 3, 2026

Nice! We've got CI passing, and everything looking good. Just going to give this one last automated PR review, and we should be good to merge.

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

Copilot reviewed 12 out of 12 changed files in this pull request and generated 8 comments.


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

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

Copilot reviewed 15 out of 15 changed files in this pull request and generated 3 comments.


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

mattt added 7 commits February 3, 2026 05:15
…e, .count, .minimumCount, .maximumCount instead of GenerationGuide(minimum:maximum:)

Updated GenerableMacro to implement  Float/Decimal guide methods to set min/max bounds so constraints are preserved
…inators during structured JSON generation, so the sampler can’t select EOS/EOT mid-structure and return a partial/non-object
@mattt
Copy link
Owner

mattt commented Feb 3, 2026

Decided to let this one cook a bit more to make sure the implementation was correct. Kicking off another review after making some changes. Let's see what comes back this time...

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

Copilot reviewed 17 out of 18 changed files in this pull request and generated 21 comments.


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

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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@mattt mattt merged commit dd8522b into mattt:main Feb 4, 2026
3 checks passed
@mattt
Copy link
Owner

mattt commented Feb 4, 2026

@eastriverlee Merged! Thanks for your work on this and your patience with my review. Really excited to support guided generation / structured output for MLX and Llama.

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.

2 participants