feat: graceful apple context window error handling#212
Open
JKobrynski wants to merge 8 commits into
Open
Conversation
|
@JKobrynski is attempting to deploy a commit to the Callstack Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
There was a problem hiding this comment.
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.ContextWindowExceededand a JS error wrapper that carries an optionalcode. - Forwards native error
codethrough stream error events and converts stream errors intoErrorinstances withcode. - 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.
artus9033
reviewed
May 24, 2026
Contributor
artus9033
left a comment
There was a problem hiding this comment.
LGTM from my end after:
- let's double-check if the above Copilot comment is a false-positive
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related issue - fixes #125
Summary
Adds explicit handling for Apple Foundation Models context window overflow errors.
When
LanguageModelSessionfails withexceededContextWindowSize, the Apple provider now exposes a stableCONTEXT_WINDOW_EXCEEDEDerror 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
CONTEXT_WINDOW_EXCEEDEDAppleLLMErrorCodes.ContextWindowExceeded