Skip to content

feat: graceful apple context window error handling#212

Open
JKobrynski wants to merge 8 commits into
callstackincubator:mainfrom
JKobrynski:feat/apple-context-window-handling
Open

feat: graceful apple context window error handling#212
JKobrynski wants to merge 8 commits into
callstackincubator:mainfrom
JKobrynski:feat/apple-context-window-handling

Conversation

@JKobrynski
Copy link
Copy Markdown

@JKobrynski JKobrynski commented May 22, 2026

Related issue - fixes #125

Summary

Adds explicit handling for Apple Foundation Models context window overflow errors.

When LanguageModelSession fails with exceededContextWindowSize, the Apple provider now exposes a stable CONTEXT_WINDOW_EXCEEDED error code. This allows apps to detect the failure and decide how to recover, for example by starting a new conversation or trimming previous messages.

Changes

  • Map Apple’s native context window overflow error to CONTEXT_WINDOW_EXCEEDED
  • Forward the error code through streaming errors
  • Expose AppleLLMErrorCodes.ContextWindowExceeded
  • Update the Expo example so text streams surface the error properly
  • Document recommended app-level recovery strategies

@vercel
Copy link
Copy Markdown

vercel Bot commented May 22, 2026

@JKobrynski is attempting to deploy a commit to the Callstack Team on Vercel.

A member of the Team first needs to authorize it.

@JKobrynski JKobrynski marked this pull request as ready for review May 22, 2026 13:44
@artus9033 artus9033 self-requested a review May 24, 2026 22:57
@artus9033 artus9033 added the enhancement New feature or request label May 24, 2026
@artus9033 artus9033 requested a review from Copilot May 24, 2026 22:58
Copy link
Copy Markdown
Contributor

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

Adds explicit handling for Apple Foundation Models context-window overflow by introducing a stable CONTEXT_WINDOW_EXCEEDED error code and propagating it through streaming APIs, alongside documentation and example updates.

Changes:

  • Introduces AppleLLMErrorCodes.ContextWindowExceeded and a JS error wrapper that carries an optional code.
  • Forwards native error code through stream error events and converts stream errors into Error instances with code.
  • Updates iOS native layer to detect exceededContextWindowSize, emit the stable error code, and documents recovery strategies (plus updates the Expo example to surface stream errors).

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
website/src/docs/apple/generating.md Documents context window behavior, stable error code, and recommended recovery patterns (including streaming guidance).
packages/apple-llm/src/stream.ts Wraps stream error events using createAppleLLMError(..., code) to preserve an error code.
packages/apple-llm/src/NativeAppleLLM.ts Extends stream error event shape with optional code.
packages/apple-llm/src/index.ts Exports new Apple error types and constants.
packages/apple-llm/src/errors.ts Adds AppleLLMErrorCodes and createAppleLLMError helper for code-carrying errors.
packages/apple-llm/src/ai-sdk.ts Forwards stream error code into Error objects for AI SDK streaming consumers.
packages/apple-llm/ios/AppleLLMImpl.swift Adds native mapping for context-window overflow errors and forwards code through streaming callbacks.
packages/apple-llm/ios/AppleLLMError.swift Adds contextWindowExceeded case and stable CONTEXT_WINDOW_EXCEEDED code mapping.
packages/apple-llm/ios/AppleLLM.mm Emits stream error payloads with optional code field.
apps/expo-example/src/screens/ChatScreen/index.tsx Captures streamText errors via onError so text streaming surfaces failures properly.

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

Comment thread packages/apple-llm/ios/AppleLLMImpl.swift Outdated
Copy link
Copy Markdown
Contributor

@artus9033 artus9033 left a comment

Choose a reason for hiding this comment

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

LGTM from my end after:

  1. let's double-check if the above Copilot comment is a false-positive
  2. this PR is missing a convenience feature - exposing a JS API that would allow users to count the number of tokens a string takes, before running inference on the model (+ docs update for that); let's address this here, or in a follow-up PR

@artus9033 artus9033 changed the title feat: apple context window handling feat: graceful apple context window error handling May 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

My app crashes after exceeding the 4096 token context window of Apple Foundation Model

3 participants