Skip to content
QwryAI
Documentation
Pages

iOS SDK

JSONValue

Reference for JSONValue, the type the iOS SDK uses for tool input and output

Tool input and output can be any JSON, so the SDK carries them as JSONValue instead of Any. You can read it without casting and pass it between tasks safely.

Overview

enum JSONValue: Sendable, Equatable, Codable

Swift
public enum JSONValue: Sendable, Equatable, Codable {
    case null
    case bool(Bool)
    case number(Double)
    case string(String)
    case array([JSONValue])
    case object([String: JSONValue])
}

You'll meet it in four places:

WhereWhat you do
A tool handler's inputRead it
A tool handler's return valueWrite it
ToolCall.inputRead it
ToolResult.output and message partsRead it

Reading values

Looking up a key

Swift
public subscript(key: String) -> JSONValue? { get }

Looks up a key in an object. Returns nil if the key is missing or the value isn't an object, so you can chain safely:

Swift
let city = input["address"]?["city"]?.stringValue

Getting a Swift type

Swift
public var stringValue: String? { get }
public var numberValue: Double? { get }
public var boolValue: Bool? { get }

Each returns nil if the value is a different type:

Swift
await client.tool("book_table") { input in
    let name  = input["name"]?.stringValue ?? "Guest"
    let party = Int(input["party_size"]?.numberValue ?? 2)
    let vip   = input["vip"]?.boolValue ?? false
    // ...
}

Arrays and every case

There's no array or object accessor: switch on the value to reach them, or to cover every possibility:

Swift
func describe(_ value: JSONValue) -> String {
    switch value {
    case .null:            return "-"
    case .bool(let b):     return b ? "yes" : "no"
    case .number(let n):   return n.formatted()
    case .string(let s):   return s
    case .array(let a):    return a.map(describe).joined(separator: ", ")
    case .object(let o):   return o.map { "\($0): \(describe($1))" }.joined(separator: "; ")
    }
}

let tags: [String] = {
    guard case .array(let items)? = input["tags"] else { return [] }
    return items.compactMap(\.stringValue)
}()

Writing values

Build results from the cases:

Swift
return .object([
    "status": .string("shipped"),
    "eta_days": .number(2),
    "cost": .number(14.99),
    "expedited": .bool(true),
    "items": .array([.string("SKU-1"), .string("SKU-2")]),
    "note": .null
])

JSONValue also accepts Swift literals, so the same result can be written more briefly:

Swift
return [
    "status": "shipped",
    "eta_days": 2,
    "cost": 14.99,
    "expedited": true,
    "items": ["SKU-1", "SKU-2"],
    "note": nil
]

String, integer, decimal, Boolean, nil, array and dictionary literals all work. Values that aren't literals need their case, such as .string(order.status).

Reporting a failure

An object with an error key is how a tool reports a problem. The agent sees the tool as failed:

Swift
return ["error": "No order found with that number"]

See When a tool fails.

Using your own types

JSONValue is Codable, so you can convert to and from your own types with JSONEncoder and JSONDecoder.

Reading input into a struct

Swift
struct BookingInput: Decodable {
    let name: String
    let partySize: Int
    enum CodingKeys: String, CodingKey {
        case name
        case partySize = "party_size"
    }
}

await client.tool("book_table") { input in
    let booking = try JSONDecoder().decode(
        BookingInput.self,
        from: JSONEncoder().encode(input)
    )
    // booking.name, booking.partySize, ...
}

Returning a struct

Swift
struct BookingResult: Encodable {
    let confirmationCode: String
    let time: String
}

await client.tool("book_table") { input in
    let result = BookingResult(confirmationCode: "AB12", time: "19:30")
    return try JSONDecoder().decode(
        JSONValue.self,
        from: JSONEncoder().encode(result)
    )
}

How values are read

When the SDK decodes JSON, it tries the cases in this order: null, Bool, Double, String, array, object. So true is always .bool, and 9 and 9.0 are both .number(9).

Size limit

A tool result can be at most 20 KB of JSON. A bigger one is refused with 400 VALIDATION_INVALID_BODY.

Explore the documentation