JSONValue

A JSON value: object, array, string, number, boolean, or null.

The SDK's currency for anything whose shape isn't known at compile time: tool arguments, tool results, provider options, runtime context.

It is an enum rather than Any, so it stays Sendable and its cases are exhaustive. Accessors like stringValue unwrap the common cases without a switch, and it is ExpressibleBy the usual literals, so writing one by hand reads close to writing JSON.

let options: JSONValue = [
  "thinking": ["type": "enabled", "budget_tokens": 2048]
]

let city = args["city"]?.stringValue ?? "London"

Declaration

public enum JSONValue

Defined in Sources/AI/Util/JSONValue.swift.

Cases

case null
case bool(Bool)
case number(Double)
case string(String)
case array([JSONValue])
case object([String: JSONValue])

Initializer

init(
    from decoder: Decoder
) throws

Properties

PropertyTypeDefault
var stringValueString? { if case .string(let s)self { return s } else { return nil } }
var doubleValueDouble? { if case .number(let n)self { return n } else { return nil } }
var intValueInt? { if case .number(let n)self { return Int(n) } else { return nil } }
var boolValueBool? { if case .bool(let b)self { return b } else { return nil } }
var arrayValue[JSONValue]? { if case .array(let a)self { return a } else { return nil } }
var objectValue[String: JSONValue]? { if case .object(let o)self { return o } else { return nil } }

Methods

func encode(
    to encoder: Encoder
) throws
func decode<T: Decodable>(
    _ type: T.Type
) throws -> T

See also