LanguageModelSession PRO
LanguageModelSession provides access to Apple Intelligence local language models through iOS Foundation Models. It allows scripts to perform on-device AI tasks such as text generation, structured JSON output, and streaming responses.
This API encapsulates system-level language model capabilities and supports:
- On-device AI inference (Apple Intelligence)
- JSON Schema structured output
- Streaming token responses
- Session-level resource management
- Controlled generation parameters
LanguageModelSession is a global API and does not require importing from the scripting module.
Availability
Indicates whether Foundation Models are available on the current device.
Return value:
Notes:
- Only devices supporting Apple Intelligence will return
true. - You should check availability before creating a session.
Example:
Creating a Session
Parameters:
Description:
- Creates a new language model session.
instructionsact as system-level guidance controlling model behavior.- Instructions remain active throughout the session lifecycle.
Example:
Properties
isResponding
Description:
- Indicates whether the model is currently generating a response.
- Useful for managing UI state.
Prewarming the Model
Description:
- Requests the system to load required model resources in advance.
- Reduces initial response latency.
- Optionally caches a prompt prefix.
Parameters:
Example:
Recommended use cases:
- Before the user initiates an AI task
- When reducing first-token latency is important
Generating Responses
Returns:
Description:
- Produces a complete response.
- Supports structured JSON output via schema validation.
- Automatically attempts JSON parsing.
Parameters:
Parameter details:
-
temperature- Range: 0.0 ~ 1.0
- Higher values increase randomness.
-
maxResponseTokens- Maximum number of tokens to generate.
-
schema- Expected JSON structure.
- If generated content is not valid JSON,
jsonwill benull.
Structured Output Example
Streaming Responses
Returns:
Description:
- Produces streaming output.
- Suitable for chat interfaces or incremental display.
Parameters:
Example:
Releasing Resources
Description:
- Releases resources associated with the session.
- Recommended after finishing usage to reduce memory usage.
Example:
Complete Example
Best Practices
- Reuse a session for multiple requests when possible.
- Call
prewarmbefore first use to reduce latency. - Use
schemafor reliable structured output. - Use
streamResponsefor long-form or interactive responses. - Call
disposewhen finished to release resources.
