Repository navigation
Fix: flatten top-level oneOf/anyOf/allOf in tool input schemas - #43
Conversation
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
saarnilauri
left a comment
There was a problem hiding this comment.
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.
c6a2956 to
f4cfd8f
Compare
|
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.
Verification: Unrelated to this PR: |
saarnilauri
left a comment
There was a problem hiding this comment.
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.
|
Thanks for re-running the probes! Opened #48 for the composer.lock conflict. |
Fixes #42
Problem
prepareToolsParam()forwardsFunctionDeclaration::getParameters()intoinput_schemaverbatim. 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 levelBecause 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. Abilityauthors have no reason to know it isn't valid as an Anthropic tool schema.
Reproduction
WooCommerce 11.1.0 registers
woocommerce/product-createandwoocommerce/product-update,both of which discriminate on
product_type_aliasthrough a top-leveloneOf(
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 thesame 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.php1.
prepareToolsParam()now routes through a normaliser2. Three new protected methods added after
prepareToolsParam()flattenTopLevelSchemaCombinators(array $schema): arrayDetects a top-level
oneOf/anyOf/allOf, merges its branches into the parent schemavia
mergeSchemaBranches(), drops the combinator key, and forcestype => object. Only thetop 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): arrayUnions branch
propertiesviamergeSchemaProperties(). Keys already present on the parenttake precedence over branch values.
requiredis intersected across branches foroneOf/anyOf(only fields every branch needs) and unioned forallOf($conjunction = true, since all branches apply at once).mergeSchemaProperties(array $properties, array $incoming): arrayMerges one branch's
propertiesinto the accumulated set. A property seen in an earlierbranch keeps that branch's schema, except
enum, whose values are unioned acrossbranches — WooCommerce gives each
oneOfbranch a single-value discriminator enum(
product_type_alias), so first-branch-wins would silently reduce the tool to one producttype. If any branch leaves the property unconstrained, the merged
enumis dropped entirelyrather than kept partial.
No other part of
prepareToolsParam()changes.Tests
Added cases for:
oneOf/anyOfflattening (properties unioned,requiredintersected, nocombinator left,
type === 'object')allOf(requiredunioned, not intersected)propertiesleft untouchedgetParameters() === nullstill yields['type' => 'object', 'properties' => new stdClass()]This relaxes what the model is told about the schema, not what's actually accepted —
WP_Abilityre-validates input against the realinput_schema, and WooCommerce additionallyenforces 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.