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

Đang mở Phù hợp với người mới
#378 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
62/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
react-native, swift, typescript
Lĩnh vực
machine-learning, mobile

Hướng nghiên cứu

Lỗi nằm ở phần chuyển đổi schema số trong ios/AppleLLMImpl.swift (khoảng dòng 449–462), nơi nhánh maximum trả về trước khi kiểm tra minimum được thực thi. Hãy đọc hàm xung quanh, sau đó tái hiện lỗi bằng một schema số nguyên khai báo minimum 1 và maximum 90. Công việc hoàn thành khi cả hai giới hạn đều được áp dụng vào các hướng dẫn sinh nội dung, các nhánh giới hạn loại trừ được sửa theo cách tương tự, và có một bài kiểm thử bao phủ trường hợp kết hợp.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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!

Ngôn ngữ chính
TypeScript
Star
1.4k
Fork
64
Merge trung bình
11 giờ 24 phút
Pull request đã merge (30 ngày)
91

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của callstackincubator/ai

Tất cả issue của callstackincubator/ai

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.