generateJSON
Generate free-form JSON with no schema.
Asks for valid JSON without constraining its shape, returning a JSONValue.
Use it when the shape genuinely is not known ahead of time. When you do know
it, generateObject is better in every
way: it validates, it decodes, and the model follows a schema more reliably
than an instruction.
let result = try await generateJSON(
model: OpenAIModel("gpt-5.1"),
prompt: "Summarize this log as JSON."
)
print(result.object["level"]?.stringValue ?? "")Signature
func generateJSON(
model: any LanguageModel,
messages: [Message] = [],
system: String? = nil,
prompt: String? = nil,
maxOutputTokens: Int = 1024,
temperature: Double? = nil,
providerOptions: JSONValue? = nil,
maxRetries: Int = 2
) async throws -> GenerateObjectResult<JSONValue>Defined in Sources/AI/Core/GenerateObject.swift.
Parameters
| Parameter | Type | Default |
|---|---|---|
model | any LanguageModel | required |
messages | [Message] | [] |
system | String? | nil |
prompt | String? | nil |
maxOutputTokens | Int | 1024 |
temperature | Double? | nil |
providerOptions | JSONValue? | nil |
maxRetries | Int | 2 |
Returns
GenerateObjectResult<JSONValue>.