{"id":44836043,"url":"https://github.com/mattt/swift-xgrammar","last_synced_at":"2026-02-17T01:36:11.080Z","repository":{"id":337225907,"uuid":"1152768217","full_name":"mattt/swift-xgrammar","owner":"mattt","description":"A Swift package for grammar-guided text generation, powered by xgrammar","archived":false,"fork":false,"pushed_at":"2026-02-08T16:47:06.000Z","size":280,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-02-08T18:59:02.542Z","etag":null,"topics":["xgrammar"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mattt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-02-08T12:04:01.000Z","updated_at":"2026-02-08T18:17:11.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/mattt/swift-xgrammar","commit_stats":null,"previous_names":["mattt/swift-xgrammar"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/mattt/swift-xgrammar","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattt%2Fswift-xgrammar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattt%2Fswift-xgrammar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattt%2Fswift-xgrammar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattt%2Fswift-xgrammar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattt","download_url":"https://codeload.github.com/mattt/swift-xgrammar/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattt%2Fswift-xgrammar/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29529513,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-17T00:57:22.232Z","status":"ssl_error","status_checked_at":"2026-02-17T00:54:25.811Z","response_time":115,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["xgrammar"],"created_at":"2026-02-17T01:36:09.019Z","updated_at":"2026-02-17T01:36:11.072Z","avatar_url":"https://github.com/mattt.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# XGrammar\n\nA Swift package for grammar-guided text generation, \npowered by [xgrammar](https://github.com/mlc-ai/xgrammar).\n\nXGrammar constrains token-by-token decoding in language models using \nEBNF grammars, JSON schemas, regex patterns, or structural tags. \nIt ensures 100% structural correctness of generated output with near-zero overhead.\n\n## Requirements\n\n- Swift 6.0+ / Xcode 16+\n- macOS 13+, iOS 16+, tvOS 16+, watchOS 9+, visionOS 1+, or Linux\n\n## Installation\n\n### Swift Package Manager\n\nAdd the following to your `Package.swift` file:\n\n```swift\ndependencies: [\n    .package(url: \"https://github.com/mattt/swift-xgrammar.git\", from: \"0.1.0\")\n]\n```\n\n## Usage\n\n### Creating a Grammar\n\nCreate grammars from EBNF definitions, JSON schemas, regex patterns, or structural tags:\n\n```swift\nimport XGrammar\n\n// From EBNF\nlet grammar = Grammar(ebnf: \"\"\"\n    root ::= \"yes\" | \"no\"\n    \"\"\")\n\n// From a regex pattern\nlet pattern = Grammar(regex: \"[a-zA-Z]+\")\n\n// Built-in JSON grammar\nlet json = Grammar.json\n```\n\n### JSON Schema\n\nGenerate grammars from JSON schemas to constrain output to a specific structure:\n\n```swift\nlet schema = \"\"\"\n    {\n        \"type\": \"object\",\n        \"properties\": {\n            \"name\": { \"type\": \"string\" },\n            \"age\": { \"type\": \"integer\" }\n        },\n        \"required\": [\"name\", \"age\"]\n    }\n    \"\"\"\n\nlet grammar = Grammar(jsonSchema: schema)\n```\n\nConfigure formatting options for the generated JSON:\n\n```swift\nlet grammar = Grammar(\n    jsonSchema: schema,\n    formatting: .compact  // No extra whitespace\n)\n\n// Or with custom formatting\nlet grammar = Grammar(\n    jsonSchema: schema,\n    formatting: JSONSchemaFormatting(\n        indentation: 2,\n        separators: (\", \", \": \")\n    )\n)\n```\n\n### Composing Grammars\n\nCombine grammars with union or concatenation:\n\n```swift\n// Match any of the provided grammars\nlet either = try Grammar.anyOf([\n    Grammar(regex: \"[0-9]+\"),\n    Grammar(regex: \"[a-z]+\")\n])\n\n// Match a sequence of grammars\nlet sequence = try Grammar.sequence([\n    Grammar(ebnf: #\"root ::= \"hello \"\"#),\n    Grammar(ebnf: #\"root ::= \"world\"\"#)\n])\n```\n\n### Constrained Decoding\n\nCompile a grammar for your tokenizer and use a matcher to constrain token selection:\n\n```swift\n// Create tokenizer info from your model's vocabulary\nlet tokenizerInfo = try TokenizerInfo(\n    encodedVocab: vocab,  // [String] from your tokenizer\n    encoding: .byteLevel\n)\n\n// Compile the grammar for this tokenizer\nlet compiled = await grammar.compiled(for: tokenizerInfo)\n\n// Create a matcher for constrained decoding\nlet matcher = try compiled.matcher()\n\n// Allocate a token bitmask\nvar bitmask = Grammar.Matcher.TokenBitmask(\n    vocabSize: tokenizerInfo.vocabulary.size\n)\n\n// During each decoding step:\nmatcher.fillNextTokenBitmask(\u0026bitmask)\nbitmask.maskLogits(\u0026logits)\n// ... sample from masked logits ...\nmatcher.accept(selectedTokenID)\n```\n\n### CoreML Integration\n\nOn Apple platforms (macOS 15+, iOS 18+), apply the bitmask directly to an `MLTensor`:\n\n```swift\n#if canImport(CoreML)\nimport CoreML\n\nlet maskedLogits = await bitmask.masking(logitsTensor)\n#endif\n```\n\n### Compiler with Caching\n\nFor repeated compilations against the same tokenizer, use a compiler to benefit from caching:\n\n```swift\nlet compiler = Grammar.Compiler(\n    tokenizerInfo: tokenizerInfo,\n    maximumThreadCount: 8,\n    cacheSizeLimit: 100 * 1024 * 1024  // 100 MB\n)\n\nlet compiled1 = await compiler.compile(grammar1)\nlet compiled2 = await compiler.compile(grammar2)\n\n// Access the pre-compiled built-in JSON grammar\nlet compiledJSON = await compiler.compiledJSON\n\n// Inspect cache usage\nprint(await compiler.cache.size)\nawait compiler.cache.clear()\n```\n\n### Serialization\n\nGrammars, compiled grammars, and tokenizer info \nall support JSON serialization for caching and transport:\n\n```swift\n// Serialize\nlet data = grammar.jsonData\n\n// Deserialize\nlet restored = try Grammar(jsonData: data)\n```\n\n## Development\n\n### Running Tests\n\n```bash\nmake test\n```\n\n### Formatting\n\nFormat Swift and C/C++ sources:\n\n```bash\nmake format\n```\n\nVerify formatting:\n\n```bash\nmake lint\n```\n\n## Acknowledgments\n\nThis package vendors C++ sources from \n[xgrammar](https://github.com/mlc-ai/xgrammar) v0.1.31.\n\n## License\n\nThis package and the underlying xgrammar library\nare available under the Apache 2.0 license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattt%2Fswift-xgrammar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattt%2Fswift-xgrammar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattt%2Fswift-xgrammar/lists"}