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

Aperta Adatta ai principianti
#378 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
62/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
react-native, swift, typescript

Direzione di ricerca

L'errore si trova nella conversione dello schema numerico in ios/AppleLLMImpl.swift (intorno alle righe 449–462), dove il ramo maximum restituisce il controllo prima che venga eseguito il controllo su minimum. Leggi la funzione circostante, poi riproduci il problema con uno schema intero che dichiari minimum 1 e maximum 90. Il lavoro è completo quando entrambi i limiti vengono applicati alle guide di generazione, i rami dei limiti esclusivi vengono corretti allo stesso modo e un test copre il caso combinato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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!

Lingua principale
TypeScript
Stelle
1.4k
Fork
64
Merge medio
11h 24m
PR unite (30g)
91

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di callstackincubator/ai

Tutte le issue di callstackincubator/ai

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.