Repository navigation
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Root unions break OpenAI schemas, while optional and naming collisions can produce invalid or lossy payloads.
Review effort: Balanced
Findings: 2
Open (2)
What changed in this PR
Aligns associated-value enum schemas, serialization, and decoding with Foundation Models.
Changes:
- Represents each enum case as a typed object with a discriminator.
- Adds schema construction for payload enums.
- Adds unit and system-model coverage.
| File | Description |
|---|---|
GenerationSchema.swift |
Builds discriminated case schemas. |
GenerableMacro.swift |
Generates payload serialization and decoding. |
GenerableEnumPayloadTests.swift |
Tests schemas and round trips. |
SystemLanguageModelTests.swift |
Exercises system-model generation. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+432
to
+433
| let caseName = "\(typeName).Discriminated\(name.prefix(1).uppercased() + name.dropFirst())" | ||
| allDefs[caseName] = .object( |
Comment on lines
+1280
to
+1282
| let type = value.type | ||
| let isOptional = type.hasSuffix("?") || type.hasPrefix("Optional<") | ||
| return (value.label, value.label ?? (index == 0 ? "value" : "value\(index)"), type, isOptional) |
Collaborator
|
Thanks for opening this PR, @akbashev! A couple of things before we merge:
On |
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

@Generabledescribes an enum whose cases carry values as{case: String, value: String}. The case names aren't listed, and every value is a string, whatever its type, so a model has nothing that tells it a case's values or their types. Foundation Models' own@Generabledescribes such an enum as one object per case, with atypethat names the case and a property for each value, typed. Code that moves between the two frameworks gets different schemas and different content for the same enum.This PR generates what Foundation Models generates, for the schema, the content, and decoding. A case is an object like
{"type": "set", "field": "count", "to": {"type": "number", "value": 1}}: a labeled value is a property named by its label, and unlabeled ones arevalue,value1, and so on. A case without values is{"type": "reset"}, and an optional value that'snilis left out. The schema comes from a newGenerationSchema(type:description:cases:), which builds one object per case. Enums without associated values are unchanged.Two details differ from Foundation Models:
Module.Step.DiscriminatedSet: Foundation Models names itDiscriminatedSetalone, so two enums with asetcase share one definition, and one of them loses its values.typeis a string with one choice rather than aconst, which means the same and works with every provider, including Gemini, whose schemas have noconst.Content saved in the old form, such as a stored transcript, no longer decodes. The old schema gave models nothing typed to fill in, so little such content is likely to exist.
Tests:
GenerableEnumPayloadTestschecks the content and schema against what Foundation Models generates for the same enums, andguidedGenerationWithEnumPayloadsgenerates a list of such enums with the system model.