Skip to content

Fix: flatten top-level oneOf/anyOf/allOf in tool input schemas - #43

Merged
jeffpaul merged 1 commit into
WordPress:trunkfrom
Arkenon:fix/tool-schema-top-level-combinators
Oct 7, 2026
Merged

jeffpaul merged 1 commit into
WordPress:trunkfrom
Arkenon:fix/tool-schema-top-level-combinators

Conversation

@Arkenon

@Arkenon Arkenon commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Fixes #42

Problem

prepareToolsParam() forwards FunctionDeclaration::getParameters() into input_schema verbatim. The API refuses a top-level combinator:

Bad Request (400) - tools.79.custom.input_schema: input_schema does not support oneOf, allOf, or anyOf at the top level

Because the full tool list is sent with every request, one function declaration with such a
schema fails the entire request — including plain chat with no tool call intended. The
site is unusable with Anthropic until the offending ability is unregistered.

This matters more now that WordPress ships the Abilities API: abilities declare a plain JSON
Schema input_schema, and a top-level combinator is perfectly valid JSON Schema. Ability
authors have no reason to know it isn't valid as an Anthropic tool schema.

Reproduction

WooCommerce 11.1.0 registers woocommerce/product-create and woocommerce/product-update,
both of which discriminate on product_type_alias through a top-level oneOf
(src/Internal/Abilities/Domain/Traits/ProductAbilityTrait.php). With WooCommerce active,
every Anthropic request fails with the error above.

The same site works against OpenAI, which accepts the schema as-is.

Worth noting: the WordPress MCP adapter passes ability schemas through verbatim
(wordpress/mcp-adapter, RegisterAbilityAsMcpTool::get_data() — no normalisation), yet the
same WooCommerce tools work fine in MCP clients such as Claude Desktop, including actually
creating products. Those clients normalise the schema before calling the API. Doing the same
in this connector brings it in line with what a tool-calling client is expected to do.

Change

File: src/Models/AnthropicTextGenerationModel.php

1. prepareToolsParam() now routes through a normaliser

$inputSchema = $functionDeclaration->getParameters();
if ($inputSchema === null) {
    $inputSchema = [
        'type' => 'object',
        'properties' => new \stdClass(),
    ];
} else {
    // The API refuses a combinator at the top level of input_schema.
    $inputSchema = $this->flattenTopLevelSchemaCombinators($inputSchema);
}

2. Three new protected methods added after prepareToolsParam()

  • flattenTopLevelSchemaCombinators(array $schema): array
    Detects a top-level oneOf / anyOf / allOf, merges its branches into the parent schema
    via mergeSchemaBranches(), drops the combinator key, and forces type => object. Only the
    top level is touched — nested combinators are left as-is. A schema without a top-level
    combinator passes through unchanged.

  • mergeSchemaBranches(array $schema, array $branches, bool $conjunction): array
    Unions branch properties via mergeSchemaProperties(). Keys already present on the parent
    take precedence over branch values. required is intersected across branches for
    oneOf/anyOf (only fields every branch needs) and unioned for allOf ($conjunction = true, since all branches apply at once).

  • mergeSchemaProperties(array $properties, array $incoming): array
    Merges one branch's properties into the accumulated set. A property seen in an earlier
    branch keeps that branch's schema, except enum, whose values are unioned across
    branches — WooCommerce gives each oneOf branch a single-value discriminator enum
    (product_type_alias), so first-branch-wins would silently reduce the tool to one product
    type. If any branch leaves the property unconstrained, the merged enum is dropped entirely
    rather than kept partial.

No other part of prepareToolsParam() changes.

Tests

Added cases for:

  • top-level oneOf / anyOf flattening (properties unioned, required intersected, no
    combinator left, type === 'object')
  • top-level allOf (required unioned, not intersected)
  • enum union across branches, and enum widening (dropped) when one branch has no enum
  • nested combinators inside properties left untouched
  • regression: schema with no top-level combinator returned unchanged
  • idempotency: running the normaliser twice gives the same result
  • getParameters() === null still yields ['type' => 'object', 'properties' => new stdClass()]

This relaxes what the model is told about the schema, not what's actually accepted —
WP_Ability re-validates input against the real input_schema, and WooCommerce additionally
enforces its branch matrix at run time
(ProductAbilityTrait::validate_product_fields_for_config()), so no validation is lost.


This patch was drafted with the help of Claude, then manually reviewed and verified before submitting.

@Arkenon
Arkenon requested a review from a team September 9, 2026 12:14
@github-actions

github-actions Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: Arkenon <arkenon@git.wordpress.org>
Co-authored-by: saarnilauri <laurisaarni@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@jeffpaul jeffpaul added this to the 1.1.0 milestone Oct 1, 2026

@saarnilauri saarnilauri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for a solid PR.

I used couple of agents to look for anything that should be changed before merging and I verified the agent findings against Anthropics API with live calls.

I marked my commets with headline C1-C7 as there are some crossreferences.

Before merge:

  • The missing tests (see C1).
  • Nested combinators that still reach the root and still get the 400 (C3).
  • Shared properties and enums that lose valid alternatives (C5, C6). Both visibly change what the model sends. Property-level anyOf/allOf (C5) fixes both, and the API accepts it.

Non-blocking: branch keywords lifted to the root (C4). The schema is wrong, but I couldn't observe an effect on the model.

Documentation: C2 and C7. C7 is likely resolved by the C5 fix.

The commit message also describes a different change, so please reword it or squash-merge.

How I checked: I extracted the three methods at 8c54e7d and ran them on PHP 8.4.2. I sent the resulting schemas to the Messages API with Haiku 4.5: one request per schema to check acceptance, and 5 forced tool calls per case to check what the model sends.

I hope this helps

The Anthropic API rejects oneOf/anyOf/allOf at the root of a tool's
input_schema (it accepts them inside properties), which broke every
request that included e.g. WooCommerce product abilities. Fixes WordPress#42.

prepareToolsParam() now merges root combinators into one permissive
object schema:
- each branch is flattened recursively first, so nested combinators
  never resurface at the root and the result is idempotent
- properties declared by several branches keep every alternative via a
  property-level anyOf (oneOf/anyOf) or allOf (allOf); identical schemas
  are deduplicated, and an unconstrained branch keeps the property
  unconstrained. A property already on the parent is combined with the
  branch alternatives via allOf
- required is intersected for disjunctions and unioned for allOf
- only title/description are lifted from branches; branch-only keywords
  (not, $ref, if/then/else, $defs, ...) are not copied to the root
- type is forced to object

The merged schema is only a hint to the model; callers must still
validate arguments against the original schema. Adds PHPUnit coverage.
@Arkenon
Arkenon force-pushed the fix/tool-schema-top-level-combinators branch from c6a2956 to f4cfd8f Compare October 5, 2026 11:30
@Arkenon

Arkenon commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks again for the detailed review. All seven points are addressed in the updated branch, which is now rebased onto current trunk (1.0.5) as a single commit with a reworded message.

  • C1: PHPUnit tests are added under tests/Models/. They cover the cases from the description, a regression case for each of your findings, and a WooCommerce-style discriminator. A few go through prepareToolsParam().
  • C2: The docblock now uses your validation-contract wording.
  • C3: Each branch is flattened recursively before merging. Both nested examples now have no root combinator, keep all properties and are idempotent.
  • C4: Disjunction branches only contribute title/description, and only when the parent lacks them. not, $ref, if/then/else, minProperties and similar are no longer lifted. For allOf I kept the same conservative set, since keywords like additionalProperties are scope-sensitive. I also dropped $defs/definitions lifting. Fragment refs resolve against the document root, so #/$defs/X already points at root definitions. A branch's own definitions would have been referenced as #/allOf/0/$defs/X, which can't survive flattening. The docblock notes that refs into branches (#/oneOf/0/...) don't resolve after flattening.
  • C5: Shared properties keep every alternative as a property-level anyOf (or allOf for allOf branches). Identical schemas are deduplicated.
    • For disjunctions, a branch that leaves the property unconstrained keeps it unconstrained. For allOf, such a branch adds nothing.
    • A property the parent already declares becomes allOf: [parent, branches], so it is never wider than the parent.
    • For disjunctions, only branches that declare a property contribute to it. A branch that omits it technically allows anything, but treating that as unconstrained would erase every single-branch property. This is an intentional, documented trade-off.
  • C6: Moot. The enum union is gone, replaced by property-level anyOf. A test confirms [true]/["1","draft"] and [0]/["0"] all survive.
  • C7: The docblock now matches the behaviour: properties are combined via allOf, required is merged as described, and only title/description are lifted.

Verification: composer test passes (20 tests), and so does PHPStan. PHPCS passes except for the line-endings sniff, which only fires on my Windows checkout; the committed files are LF. I didn't repeat your live API probes. If you still have that harness, a re-run against the new output would be a nice confirmation.

Unrelated to this PR: composer install on current trunk fails, because the lock pins both phpcompatibility/php-compatibility dev-develop and nyholm/psr7 1.8.2, which conflict. I left composer.lock untouched.

@saarnilauri saarnilauri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, nice rework. I re-ran the probes against f4cfd8f, using the four new methods with the same model (Haiku 4.5) as before. All seems to work as expected.

Thanks also for flagging the composer.lock conflict on trunk; I hit the same thing locally, and it should get its own issue. Would you mind opening it?

Approving from my side.

@Arkenon

Arkenon commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for re-running the probes! Opened #48 for the composer.lock conflict.

@jeffpaul
jeffpaul merged commit 2edc039 into WordPress:trunk Oct 7, 2026
14 checks passed
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.

Bug: top-level oneOf/anyOf/allOf in tool input_schema breaks every request (API rejects combinators at the top level)

3 participants