Skip to content

Conversation

@eastriverlee
Copy link

@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...

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