Implement guided generation for SystemLanguageModel#59
Conversation
There was a problem hiding this comment.
Pull request overview
This PR implements guided generation for SystemLanguageModel, enabling structured output generation using schema-based constraints. The implementation adds support for generating strongly-typed Swift structs from natural language prompts by converting GenerationSchema to FoundationModels' schema format and handling both streaming and non-streaming responses.
Key Changes:
- Added schema-based generation support for non-String types using FoundationModels' schema APIs with proper $defs dependency extraction
- Implemented partial JSON decoding with fallback to placeholder content for graceful error recovery
- Refactored ResponseStream to use fallbackSnapshot pattern instead of storing concrete content values
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| Sources/AnyLanguageModel/Models/SystemLanguageModel.swift | Core implementation of guided generation with schema conversion, streaming support, and placeholder fallback logic |
| Tests/AnyLanguageModelTests/SystemLanguageModelTests.swift | Comprehensive test suite covering simple structs, nested structures, arrays, enum constraints, and streaming |
| Sources/AnyLanguageModel/LanguageModelSession.swift | Refactored ResponseStream to use optional fallbackSnapshot for cleaner handling of streaming vs non-streaming cases |
| Sources/AnyLanguageModel/GenerationSchema.swift | Made defs property accessible to support schema resolution in SystemLanguageModel |
| Sources/AnyLanguageModel/Generable.swift | Enhanced asPartiallyGenerated() with safer conversion logic and fallback to generatedContent reconstruction |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
1fa983e to
679e9d0
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Related to #27