Skip to content

Add APIs for denoting model availability#4

Merged
mattt merged 4 commits intomainfrom
mattt/availability
Oct 28, 2025
Merged

Add APIs for denoting model availability#4
mattt merged 4 commits intomainfrom
mattt/availability

Conversation

@mattt
Copy link
Owner

@mattt mattt commented Oct 28, 2025

AnyLanguageModel doesn't currently provide equivalent APIs for language models to communicate that they're unavailable. For example, in the case of the system Foundation Model, Apple Intelligence must be enabled, and that's indicated by Availability.UnavailableReason.appleIntelligenceNotEnabled.

One smell arising from this deficiency is that we needed to @testable import AnyLanguageModel to introspect the underlying Foundation Models API to gate tests.

This PR adds that availability functionality. The LanguageModel protocol adds a new UnavailableReason associated type requirement, which provides some flexibility for implementations to define their own specific reasons. For example, a model that proxies to service providers may report "network unreachable" and a model running locally may report "insufficient RAM".

@mattt mattt requested a review from Copilot October 28, 2025 10:10
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 adds availability APIs to the LanguageModel protocol, enabling models to communicate whether they're ready to handle requests. This eliminates the need to use @testable import to access underlying model availability states, as seen in the SystemLanguageModel tests.

Key changes:

  • Introduces an Availability<UnavailableReason> enum with .available and .unavailable(reason) cases
  • Adds UnavailableReason associated type and availability property requirements to the LanguageModel protocol
  • Provides default .available implementation for models where UnavailableReason == Never

Reviewed Changes

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

Show a summary per file
File Description
Sources/AnyLanguageModel/Availability.swift New file defining the Availability enum with generic unavailable reason support
Sources/AnyLanguageModel/LanguageModel.swift Adds availability requirements and convenience property isAvailable to protocol
Sources/AnyLanguageModel/Models/SystemLanguageModel.swift Implements availability API by bridging to FoundationModels availability, adds static .default accessor
Sources/AnyLanguageModel/Models/OpenAILanguageModel.swift Declares UnavailableReason = Never for always-available model
Sources/AnyLanguageModel/Models/OllamaLanguageModel.swift Declares UnavailableReason = Never for always-available model
Sources/AnyLanguageModel/Models/MLXLanguageModel.swift Declares UnavailableReason = Never for always-available model
Sources/AnyLanguageModel/Models/LlamaLanguageModel.swift Declares UnavailableReason = Never for always-available model
Sources/AnyLanguageModel/Models/CoreMLLanguageModel.swift Declares UnavailableReason = Never for always-available model
Sources/AnyLanguageModel/Models/AnthropicLanguageModel.swift Declares UnavailableReason = Never for always-available model
Tests/AnyLanguageModelTests/SystemLanguageModelTests.swift Updates test to use new public availability API instead of internal access
Tests/AnyLanguageModelTests/Shared/MockLanguageModel.swift Implements availability support with customizable provider for testing
Tests/AnyLanguageModelTests/MockLanguageModelTests.swift Adds test coverage for unavailable model scenarios

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

@mattt mattt merged commit 61f5d33 into main Oct 28, 2025
2 checks passed
@mattt mattt deleted the mattt/availability branch October 28, 2025 10:33
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