Open
Conversation
- Create minimal UniFFI interface definition (tiktoken.udl) - Implement Rust wrapper for UniFFI compatibility - Use byte arrays directly for non-UTF8 token support - Expose only essential tokenization methods
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.
Summary
Adds optional Swift binding generation using Mozilla's UniFFI behind a feature flag. Non-breaking, minimal changes
(~300 lines).
Approach
Uses Mozilla's UniFFI to automatically generate Swift bindings from Rust:
Changes
This PR will add 4 new files behind #[cfg(feature = "uniffi")]:
src/uniffi_bindings.rs(~60 lines) - Thin Rust wrapper exposing functions to UniFFIsrc/tiktoken.udl(~25 lines) - UniFFI interface definitionuniffi.toml(~5 lines) - UniFFI configbuild_xcframework.sh- Build script to build the Rust library for Apple platforms and bundle it into a cross-platformXCFrameworkUsage
For Swift Developers
You can either add the XCFramework to your project directly, or use the Swift package I created: TiktokenSwift, which includes prebuilt bindings, tests, documentation, and an example project.To add the Swift package:
Swift API Example
Supports all encodings: cl100k_base, o200k_base, r50k_base, p50k_base
For Maintainers/Contributors
To regenerate bindings from source, first make sure you install the required Rust targets:
To build just the cross-device Rust framework, run:
To generate the bindings, run:
To build the frameworks, generate the bindings, and package everything together as an XCFramework, run:
Testing
Impact