Skip to content

Add sessions with dynamic instructions - #294

Merged
mattt merged 6 commits into
mainfrom
mattt/dynamic-instructions-session
Oct 4, 2026
Merged

mattt merged 6 commits into
mainfrom
mattt/dynamic-instructions-session

Conversation

@mattt

@mattt mattt commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator

This PR is the last of three that bring in #273 by @qoli. It adds LanguageModelSession.init(model:dynamicInstructions:history:), following Foundation Models 27. The session evaluates the dynamic instructions before every request to the model, including the request that continues a response after tool calls, and sends the resolved instructions and tools in that request's context (#292). The resolved instructions never become part of the session's transcript.

On OS 27, SystemLanguageModel and the other Foundation Models adapters pass the dynamic instructions to Foundation Models directly. On OS 26, SystemLanguageModel throws the new SystemLanguageModel.Error.dynamicInstructionsUnavailable, because Foundation Models there can't resolve them again for the request after tool calls.

On top of @qoli's work, this PR:

  • Leaves instructions entries out of the history, so the dynamic instructions are the only instructions
  • Documents that session.tools and session.instructions are empty for these sessions
  • Replaces the decodingFailure thrown before OS 27 with the new error

mattt added 4 commits October 3, 2026 08:45
…tions-session

# Conflicts:
#	Sources/AnyLanguageModel/DynamicInstructions.swift
#	Tests/AnyLanguageModelTests/DynamicInstructionsBuilderTests.swift
@mattt
mattt requested a balanced review from Copilot October 4, 2026 03:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The documented error contract is inaccurate for tvOS, and the new unsupported-platform branch lacks direct coverage.

Review effort: Balanced
Findings: 1 Medium severity · 1 Low severity

Open (2)
What changed in this PR

Adds per-request dynamic instructions and tools to language model sessions.

Changes:

  • Resolves dynamic instructions before requests and tool continuations.
  • Bridges dynamic sessions to Foundation Models 27.
  • Adds documentation, compatibility checks, and behavioral tests.
File Description
Sources/​AnyLanguageModel/​LanguageModelSession.swift Adds dynamic session initialization and request-context resolution.
Sources/​AnyLanguageModel/​Models/​SystemLanguageModel.swift Adds native Foundation Models bridging and unsupported-platform errors.
README.md Documents dynamic instructions.
Tests/​AnyLanguageModelTests/​DynamicInstructionsTests.swift Tests reevaluation, history, tools, failures, and cancellation.
Tests/​AnyLanguageModelTests/​APICompatibilityFoundationModelsTests.swift Checks Foundation Models API compatibility.
Tests/​AnyLanguageModelTests/​APICompatibilityAnyLanguageModelTests.swift Checks AnyLanguageModel API compatibility.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +311 to +312
guard !session.usesDynamicInstructions else {
throw SystemLanguageModel.Error.dynamicInstructionsUnavailable
Comment thread README.md Outdated
@mattt
mattt requested a balanced review from Copilot October 4, 2026 03:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

The OS 27 native integration requires final human validation, and its compiler requirements are currently documented inaccurately.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
Resolved since last review (1)
Previously missed (2)

In code that hasn't changed since last review

Low severity Document Swift 6.3 fallback behavior on OS 27

README.md:488

The fallback is also selected on OS 27 when the package is compiled with Swift 6.3, because the native branch is excluded by compiler(>=6.4). Document that case; otherwise an OS 27 user can unexpectedly receive the stated error despite satisfying this note.

Low severity Clarify Swift 6.3 toolchain requirement on OS 27

Sources/​AnyLanguageModel/​Models/​SystemLanguageModel.swift:367

This message is misleading for binaries built with Swift 6.3: the compiler(>=6.4) gate above makes this case throw even when the device is already on OS 27. Mention the build-toolchain requirement so users on OS 27 know that upgrading only the runtime is insufficient.

@mattt
mattt merged commit d4e38ca into main Oct 4, 2026
7 checks passed
@mattt
mattt deleted the mattt/dynamic-instructions-session branch October 4, 2026 03:32
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