apple: number schema with both minimum and maximum drops the minimum (DynamicGenerationSchema early return)

オープン 初心者向け
#378 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
62/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
react-native, swift, typescript

調査の方向性

バグは ios/AppleLLMImpl.swift の数値スキーマ変換部分(449〜462行目付近)にあります。maximum の分岐が、minimum のチェックが実行される前に return しています。周囲の関数を読んでから、minimum 1 と maximum 90 を宣言した整数スキーマで再現してください。両方の境界が生成ガイドに適用され、排他的境界の分岐も同じ方法で修正され、組み合わせのケースをテストが網羅していれば完了です。

索引モデルが issue の本文から書いたものです。

説明

Summary

In @react-native-ai/apple 0.12.0, a JSON Schema number or integer that declares both minimum and maximum loses its minimum when converted to a DynamicGenerationSchema. The model can then generate values below the declared lower bound, e.g. 0 or a negative for a field declared minimum: 1, maximum: 90.

Where

ios/AppleLLMImpl.swift (v0.12.0), around lines 449–462:

if let maximum = schemaDict["maximum"] as? Double {
  if type == "integer" {
    return DynamicGenerationSchema(type: Int.self, guides: [GenerationGuide.maximum(Int(maximum))])
  } else {
    return DynamicGenerationSchema(type: Double.self, guides: [GenerationGuide.maximum(maximum)])
  }
}

if let minimum = schemaDict["minimum"] as? Double {   // never reached when "maximum" is present
  ...
}

The maximum branch returns early, so the minimum branch is only reached when there is no maximum. The exclusiveMaximum / exclusiveMinimum branches below have the same early-return shape.

Repro

A tool with this parameter schema (e.g. from Zod z.number().int().min(1).max(90)):

{ "type": "object", "properties": { "days": { "type": "integer", "minimum": 1, "maximum": 90 } }, "required": ["days"] }
  • Expected: a generation guide constraining days to 1…90.
  • Actual: only maximum(90) is applied, and the model can emit days: 0 or a negative.

Suggested fix

Collect all bound guides for the property and pass them together, instead of returning on the first one. For example:

var guides: [GenerationGuide<Int>] = []
if let min = schemaDict["minimum"] as? Double { guides.append(.minimum(Int(min))) }
if let max = schemaDict["maximum"] as? Double { guides.append(.maximum(Int(max))) }
// same for exclusive bounds (+1 / -1), and the Double variant
if !guides.isEmpty { return DynamicGenerationSchema(type: Int.self, guides: guides) }

Or use a single range guide when both bounds are present.

Impact / workaround

We now validate tool arguments ourselves before executing, so out-of-range values are rejected. That still wastes a model turn, though, and anyone relying on the schema alone gets invalid arguments.

Thanks for the library!

主要言語
TypeScript
スター
1.4k
フォーク
64
平均マージ
11時間 24分
マージ済み PR(30日)
91

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

callstackincubator/ai のほかの issue

callstackincubator/ai の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。