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

Abierto Apto para principiantes
#378 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
62/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
react-native, swift, typescript

Línea de trabajo

El error está en la conversión del esquema numérico en ios/AppleLLMImpl.swift (alrededor de las líneas 449–462), donde la rama de maximum retorna antes de que se ejecute la comprobación de minimum. Lee la función circundante y luego reproduce el problema con un esquema entero que declare minimum 1 y maximum 90. Está terminado cuando ambos límites se aplican a las guías de generación, las ramas de límites exclusivos se corrigen de la misma forma y una prueba cubre el caso combinado.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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!

Lenguaje dominante
TypeScript
Estrellas
1.4k
Forks
64
Merge medio
11 h 24 min
PR fusionados (30 d)
91

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de callstackincubator/ai

Todos los issues de callstackincubator/ai

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.