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

Open Beginner friendly
#378 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
62/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
react-native, swift, typescript

Research direction

The bug is in the number-schema conversion in ios/AppleLLMImpl.swift (around lines 449–462), where the maximum branch returns before the minimum check runs. Read the surrounding function, then reproduce with an integer schema declaring minimum 1 and maximum 90. Done means both bounds are applied to the generation guides, the exclusive-bound branches are fixed the same way, and a test covers the combined case.

Written by the indexing model from the issue text.

Description

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!

Dominant language
TypeScript
Stars
1.4k
Forks
64
Avg merge
11h 8m
Merged PRs (30d)
2

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from callstackincubator/ai

All issues in callstackincubator/ai

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.