Skip to content

fix(client): correct "exactly one" check in JSON schema validator#309

Merged
TomerAberbach merged 2 commits intoanthropics:nextfrom
MaxwellCalkin:fix/json-schema-validator-xor-logic
Mar 9, 2026
Merged

fix(client): correct "exactly one" check in JSON schema validator#309
TomerAberbach merged 2 commits intoanthropics:nextfrom
MaxwellCalkin:fix/json-schema-validator-xor-logic

Conversation

@MaxwellCalkin
Copy link
Contributor

@MaxwellCalkin MaxwellCalkin commented Mar 8, 2026

Summary

  • Bug: The JSON schema validator's check for "exactly one of type, anyOf, or $ref" uses chained XOR (a.xor(b).xor(c)), which evaluates to true when an odd number of values are true (1 or 3), not when exactly one is true.
  • Impact: A malformed schema containing all three of type, anyOf, AND $ref would incorrectly pass local validation instead of being rejected with an error.
  • Fix: Replace the chained XOR with listOf(...).count { it } == 1 which correctly validates exactly one field is present.

Truth table showing the bug

anyOf type $ref Count a.xor(b).xor(c) Correct
F F F 0 false false
T F F 1 true true
F T F 1 true true
F F T 1 true true
T T F 2 false false
T F T 2 false false
F T T 2 false false
T T T 3 true (BUG) false

Test plan

  • Verified existing structured output tests still pass with the fix
  • A test with a schema node containing all three fields (type, anyOf, and $ref) would validate the fix catches this edge case

AI Disclosure

This PR was authored by Claude Opus 4.6 (Anthropic), an AI agent operated by Maxwell Calkin (@MaxwellCalkin).

The validation that ensures exactly one of `type`, `anyOf`, or `$ref`
is present in a schema node used chained XOR operations:
`a.xor(b).xor(c)`. This evaluates to `true` when an odd number of
values are true (1 or 3), not when exactly one is true.

This means a malformed schema node containing all three fields (`type`,
`anyOf`, AND `$ref`) would incorrectly pass validation instead of being
rejected.

Replace the chained XOR with an explicit count check that correctly
validates exactly one of the three fields is present.
@MaxwellCalkin MaxwellCalkin requested a review from a team as a code owner March 8, 2026 22:07
@TomerAberbach TomerAberbach changed the base branch from main to next March 9, 2026 15:48
@TomerAberbach TomerAberbach changed the title fix: correct "exactly one" check in JSON schema validator fix(client): correct "exactly one" check in JSON schema validator Mar 9, 2026
@TomerAberbach TomerAberbach merged commit 6e58ba9 into anthropics:next Mar 9, 2026
4 checks passed
@stainless-app stainless-app bot mentioned this pull request Mar 9, 2026
stainless-app bot pushed a commit that referenced this pull request Mar 11, 2026
* fix: correct "exactly one" check in JSON schema validator

The validation that ensures exactly one of `type`, `anyOf`, or `$ref`
is present in a schema node used chained XOR operations:
`a.xor(b).xor(c)`. This evaluates to `true` when an odd number of
values are true (1 or 3), not when exactly one is true.

This means a malformed schema node containing all three fields (`type`,
`anyOf`, AND `$ref`) would incorrectly pass validation instead of being
rejected.

Replace the chained XOR with an explicit count check that correctly
validates exactly one of the three fields is present.

* chore: format

---------

Co-authored-by: Tomer Aberbach <tomer@stainless.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants