Conversation
JSONValue was a typealias for the JSONSchema package's JSONValue, so every public API that takes or returns one tied AnyLanguageModel's major version to JSONSchema's. That includes extraBody on the OpenAI, Open Responses, and Anthropic options, MLX additionalContext, Ollama's custom options, and the GeneratedContent JSONValue initializer and property. Define JSONValue as AnyLanguageModel's own enum with the same cases, plain JSON coding, and literal conformances, so dictionary literals passed to these APIs keep compiling. It keeps the Encodable initializer and objectValue, which the package already uses, and drops the other JSONSchema conveniences. Values read from a JSONSchema are converted at the point of use. Code that imports both AnyLanguageModel and JSONSchema now has two JSONValue types and must qualify the name.
The new JSONValue kept only objectValue from JSONSchema's accessors, so code that read values through isNull, boolValue, intValue, doubleValue, stringValue, or arrayValue stopped compiling. Add those accessors with JSONSchema's semantics: intValue returns nil for a double, and doubleValue converts an int.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The replacement should remain frozen to avoid an additional source break for library-evolution clients.
Get a fresh assessment by requesting another Copilot review.
Review effort: Balanced
Findings: 1
What changed in this PR
Introduces an AnyLanguageModel-owned JSONValue, decoupling public APIs from JSONSchema’s major version.
Changes:
- Adds the public JSON value enum, literals, Codable support, and accessors.
- Adds internal conversion to and from JSONSchema values.
- Updates schema integrations and provides comprehensive tests.
| File | Description |
|---|---|
Sources/AnyLanguageModel/Shared/JSONValue.swift |
Defines the new public JSON value type. |
Sources/AnyLanguageModel/Shared/JSONValue+JSONSchema.swift |
Bridges internal JSONSchema values. |
Sources/AnyLanguageModel/Models/SystemLanguageModel.swift |
Converts schema enum values through the bridge. |
Sources/AnyLanguageModel/Models/LlamaLanguageModel.swift |
Encodes JSONSchema enum arrays directly. |
Sources/AnyLanguageModel/Models/CoreMLLanguageModel.swift |
Encodes JSONSchema enum arrays directly. |
Tests/AnyLanguageModelTests/JSONValueTests.swift |
Tests coding, literals, accessors, and conversion. |
Tests/AnyLanguageModelTests/DynamicSchemaConversionTests.swift |
Narrows the JSONSchema import. |
Tests/AnyLanguageModelTests/OpenAILanguageModelTests.swift |
Removes the unnecessary JSONSchema import. |
Tests/AnyLanguageModelTests/ConvertibleToGeneratedContentTests.swift |
Removes the unnecessary JSONSchema import. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
JSONSchema's JSONValue is frozen, so clients built with library evolution can switch over its cases exhaustively. Keep that for the replacement, since JSON has a fixed set of seven kinds of value.
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.

JSONValueis a typealias for JSONSchema'sJSONValue, so every public API that uses it ties AnyLanguageModel's major version to JSONSchema's. That coversextraBodyon the OpenAI, Open Responses, and Anthropic options, MLXadditionalContext, Ollama'sCustomGenerationOptions, andGeneratedContent.init(_: JSONValue)andGeneratedContent.jsonValuefrom #257.This PR replaces the typealias with AnyLanguageModel's own
JSONValueenum. It has the same seven cases, codes as plain JSON, and conforms toSendable,Hashable,Codable, and the nil, Boolean, integer, float, string, array, and dictionary literal protocols, so literals likeextraBody: ["user": "id"]keep compiling. It keepsinit(_:)forEncodablevalues and JSONSchema's accessors (isNull,boolValue,intValue,doubleValue,stringValue,arrayValue, andobjectValue) with the same behavior. Internally, it converts to and from JSONSchema's type where the package reads a schema.This is source-breaking in two ways. Code that uses the rest of JSONSchema's
JSONValueAPI on these values no longer compiles:description, string interpolation literals,isCompatible(with:strict:), and theString,Int,Double, andBoolinitializers that take aJSONValue. And a file that imports both AnyLanguageModel and JSONSchema now sees twoJSONValuetypes and needs to writeAnyLanguageModel.JSONValueorJSONSchema.JSONValue.