Repository navigation
Convert the booleans in Foundation Models tool schemas to Swift Bool values - #653
Merged
Merged
Conversation
- Converts each JSON boolean in a tool's parameter schema to a Swift Bool before the tool spec reaches the chat template. - Leaves each number in the schema unchanged, even a 0 or a 1. - Adds tests that check the booleans become Swift Bool values and the numbers stay numbers. JSONSerialization boxes each JSON boolean in an NSNumber. swift-jinja's Value(any:) matches Int before Bool, so it converts a boxed false to 0. When a tokenizer passed the schema to swift-jinja unchanged, the prompt showed "additionalProperties": 0. The conversion checks the type ID of each NSNumber. An `as? Bool` cast also accepts a boxed 0 or 1, so that cast would turn "minItems": 1 into true.
thechriswebb
force-pushed
the
webb/tool-spec-bools
branch
from
September 29, 2026 20:41
833a051 to
bb3f3cd
Compare
davidkoski
reviewed
Sep 29, 2026
|
|
||
| /// swift-jinja's `Value(any:)` matches `Int` before `Bool`, so it converts a | ||
| /// `false` that `JSONSerialization` boxed in an `NSNumber` to `0`. | ||
| private static func unboxingBooleans(in value: Any) -> Any { |
Member
There was a problem hiding this comment.
We don't need to fix it now, but something to think of:
public static func from(_ value: Any) -> JSONValueis in MLXLMCommon -- I wonder if this could be used? Or perhaps this should move there?
davidkoski
approved these changes
Sep 29, 2026
davidkoski
left a comment
Member
There was a problem hiding this comment.
Looks good, see comment on future work.
wballard
added a commit
to swissarmyhammer/mlx-swift-lm-old
that referenced
this pull request
Oct 3, 2026
Apply the upstream commits ee673d6..55db721 as a 3-way patch. CLAUDE.md is excluded. - c043fb3 pick up mlx-swift 0.32.2 (ml-explore#646) - 0493daf Named image attachments in MLXLMCommon and the Foundation Models adapter (ml-explore#535) - 0dcfe2f Convert the booleans in tool parameter schemas to Swift Bool values (ml-explore#653) - 37dab60 be able to trigger workflows on main. notes about upcoming releases. (ml-explore#647) - 390c5fd require mlx-swift-0.32.3 for SDK issue (ml-explore#657) - 3b339ad adjustments for running tests on NAX/TF32 hardware (ml-explore#649) - ac6f565 update documentation and skills (ml-explore#489) - ce3950e Drop MTP weights from converted Qwen 3.5 VLM checkpoints (ml-explore#642) - 9112fae Synchronize cross-SIMD WHT reads before shared-memory writes (ml-explore#650) - 7e4024e NemotronH: MoE keys are optional, so dense checkpoints load (ml-explore#635) - 1830ae4 Declare the fused GDN projection as compile state so it frees with the model (ml-explore#631) - 6e0a0b2 Synchronize Metal before checking cached buffer memory (ml-explore#667) - 9afc3b5 Recommend upToNextMinor in installation instructions (ml-explore#668) - 55db721 Reduce Qwen3 reranking memory by projecting final tokens only (ml-explore#621) Conflict resolutions: - MLXLanguageModel.swift: keep the drain coordinator and runRespond of stable, and keep historyMessages. Add the upstream attachment label validation before resolveConfiguration. historyMessages now throws, because TranscriptConverter.mlxMessages now throws. - TranscriptConverter.swift: keep the mlxMessages(for:replayReasoning:) and convert(entry:) form of stable. Apply the upstream changes in convert(entry:): it throws, an instructions entry drops its images, and extractImages(from:in:) applies the orientation and rejects content that is not an image. - Chat.swift: keep the doc comments of stable, and add the upstream MessageContentLayout and contentParts(for:layout:). - TurboQuantKernels.swift: keep stable. Stable already fixed the same cross-SIMD WHT race with two barriers per stage. - KVCacheTests.swift: take the upstream file (upstream changed it to a suite). Delete the upstream ArraysCache.advance test, because in this fork advance(_:) also moves offset. Add the fork tests for advance and the trim-then-continue test at file scope. - Qwen35FusedGDNProjectionTests.swift: keep the deletion of the opt-in benchmark (it skips unless an environment variable is set). Add the new upstream memory test. - model-container.md: take upstream (llama3_2_3B_4bit and the macros are the real API). - TranscriptConverterTests.swift: the replayReasoning calls now use try. Tests: MLXGuidedGenerationTests 70, MLXFoundationModelsTests 349, CXGrammarTests 7, MLXHuggingFaceMacrosTests 5 pass. MLXLMTests: 1287 Swift Testing tests pass, and 699 of 700 XCTest tests pass. The failure is the wall-clock limit of EvalTests testVarianceNormalizedKVCache32KIngestAndDecodePeakMetalMemorySmoke (0.29 s, limit 0.25 s) in the full run. The test passes alone two times, and this merge does not change EvalTests.swift. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
The problem
The Foundation Models adapter passes each tool's parameter schema to the chat template. A
@Generableschema setsadditionalPropertiestofalse, at the root and in each nested definition. The adapter builds the schema withJSONSerialization, which stores each boolean in anNSNumber. swift-transformers passes the schema to swift-jinja unchanged. swift-jinja checks for an integer before a boolean when it converts a value. The rendered tool schema therefore showed"additionalProperties": 0. JSON Schema allows only a boolean or a schema object in that field.What this PR does
Boolbefore the schema reaches the chat template. The rendered schema now shows"additionalProperties": false.0or1also passes anas? Boolcast, so the conversion checks the stored type instead.The PR changes only the adapter's own schema conversion. swift-jinja is unchanged. An app that builds its own tool dictionaries with
JSONSerializationstill gets0if its tokenizer passes them to swift-jinja unchanged.Tests
xcodebuild test -only-testing:MLXFoundationModelsTests/ToolCallingConversionsTests: both new tests pass.additionalPropertiescomes out as a SwiftBool, at the schema root and in a nested definition. This test failed before the fix.1stay numbers. With anas? Boolcast in place of the fix, this test failed because the bounds became SwiftBoolvalues.Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.