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
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:
| Where | What you do |
|---|---|
| A tool handler's input | Read it |
| A tool handler's return value | Write it |
ToolCall.input | Read it |
ToolResult.output and message parts | Read it |
Reading values
Looking up a key
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:
let city = input["address"]?["city"]?.stringValueGetting a Swift type
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:
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:
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:
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:
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:
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
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
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.