Skip to content

Structured generation schema invalid when using Array<@Generable type>: missing $defs for referenced item type (v0.7.0) #123

@ajason

Description

@ajason

Summary
When using guided generation with session.respond(to: generating:), schemas containing Array may produce invalid JSON Schema for OpenAI-compatible backends.

The emitted schema contains $ref (e.g. #/$defs/MyModule.PromptRefineEdit) inside array items, but the corresponding $defs entry is missing, causing HTTP 400 invalid_json_schema.

Minimal Repro

import AnyLanguageModel

@Generable
struct Edit {
  let old: String
  let new: String
}

@Generable
struct Result {
  let analysis: String
  let edits: [Edit]
}

let session = LanguageModelSession(model: model)
let _ = try await session.respond(
  to: "Return a valid object",
  generating: Result.self
)

Actual Result
Server rejects request schema:

code: invalid_json_schema
param: text.format.schema
message includes:
reference to component '#/$defs/.Edit' which was not found in the schema
Expected Result
Schema should include all referenced definitions in $defs, including element types referenced by arrays.

Suspected Cause
Array: Generable schema path appears to keep items as $ref but drops element schema defs, so refs become dangling.

Additional Context
Locally patched behavior by preserving elementSchema.defs in Array.generationSchema, which fixed the issue in my app.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions