Skip to content

Fix null handling in tool schemas for chat templates - #33

Merged
atdrendel merged 1 commit into
mainfrom
fix-nulls-in-tool-definitions
Sep 16, 2026
Merged

atdrendel merged 1 commit into
mainfrom
fix-nulls-in-tool-definitions

Conversation

@atdrendel

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI balanced review requested due to automatic review settings September 16, 2026 22:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The core change alters a shared chat-templating path used for all tool schemas with subtle Swift Optional/existential semantics whose downstream behavior cannot be verified here, and the added tests are on-device model tests that do not run in CI.

Pull request overview

This PR fixes rendering failures that occur when tool JSON schemas contain null values (e.g., nullable types like ["string", "null"] and enums such as ["person", "organization", null]). In this codebase such nulls are represented as NSNull() inside the any Sendable tool dictionaries. The core change makes _Tokenizer.applyChatTemplate recursively convert NSNull to Swift nil before delegating the tool schemas to the upstream tokenizer, so the chat-template engine can render them. The remainder of the PR adds a shared test-input helper and a matching per-model test across every model suite.

Changes:

  • Add a recursive replacingNulls helper in TokenizerLoader.swift and apply it to tools before calling upstream.applyChatTemplate.
  • Add a nullableToolSearchInput(enableThinking:) helper in Helpers.swift that builds a multi-turn tool-search conversation with a nullable contact_update schema.
  • Add canContinueAfterToolSearchRevealsNullableSchema tests to 19 model test files, following each file's existing conventions (e.g., Gemma files guard on SHLLM.isSupportedDevice, Qwen/others rely on the loadModel early-return).
File summaries
File Description
Sources/SHLLM/TokenizerLoader.swift Core fix: recursively replace NSNull with nil in tool schemas before chat templating
Tests/SHLLMTests/Helpers.swift New nullableToolSearchInput helper providing a nullable tool schema and multi-turn messages
Tests/SHLLMTests/Models/Qwen3-*Tests.swift, Qwen3_5-*Tests.swift, Qwen2_5-*Tests.swift New nullable-schema tool test per Qwen suite
Tests/SHLLMTests/Models/Gemma4-*Tests.swift New nullable-schema tool test with isSupportedDevice guard and enable_thinking: true
Tests/SHLLMTests/Models/Orchestrator-8BTests.swift, NemotronNano-30BTests.swift, Ministral-3-14BTests.swift, LFM2-8B-A1BTests.swift, GPTOSS-20BTests.swift, Devstral2Small-24BTests.swift New nullable-schema tool test per model suite
Review details
  • Files reviewed: 23/23 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@atdrendel
atdrendel merged commit e1564bc into main Sep 16, 2026
2 checks passed
@atdrendel
atdrendel deleted the fix-nulls-in-tool-definitions branch September 16, 2026 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants