Opened 6 weeks ago
Closed 4 weeks ago
#65781 closed defect (bug) (fixed)
AI Client: support checks return the prompt builder instead of false when the wrapped builder throws
| Reported by: | bejignesh | Owned by: | gziolo |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.2 |
| Component: | AI | Version: | 7.0 |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description
WP_AI_Client_Prompt_Builder::__call() catches an exception from the wrapped SDK builder, stores it as the error state, and then only special cases the generating methods:
try { $callable = $this->get_builder_callable( $name ); $result = $callable( ...$arguments ); ... } catch ( Exception $e ) { $this->error = $this->exception_to_wp_error( $e ); if ( self::is_generating_method( $name ) ) { return $this->error; } return $this; }
is_supported() and the seven is_supported_for_*() methods are not generating methods, so they fall through to the fluent return and hand back the WP_AI_Client_Prompt_Builder instance. That object is truthy, so a support check that failed reads as a success:
if ( wp_ai_client_prompt( 'Write a haiku' )->is_supported() ) { // Runs even though the support check could not be completed. }
The class handles this correctly everywhere else. The error state guard at the top of __call() returns false for support checks, and so does the prevented prompt path. Only an exception raised during the support check itself takes the wrong branch.
Steps to reproduce
ModalityEnum::document() is a documented factory on the bundled client, but PromptBuilder::inferCapabilityFromOutputModalities() has no branch for it and throws a RuntimeException in its else. That call sits outside the try block isSupported() uses to swallow InvalidArgumentException, so it reaches the WordPress wrapper.
$result = wp_ai_client_prompt( 'Test text' ) ->as_output_modalities( ModalityEnum::document() ) ->is_supported(); var_dump( $result ); // object(WP_AI_Client_Prompt_Builder), expected bool(false)
Any other throw from the wrapped builder during a support check does the same thing. The document modality is just the shortest path to one.
Expected
Support check methods always return a bool. That is what the @method annotations on the class say, and what test_boolean_methods_return_boolean() already asserts ("is_supported_for_text_generation should not return the decorator").
Patch
Return false from support check methods in the catch block, matching what they already return when the builder was in an error state beforehand. The error is still stored, so a later generating call still returns the WP_Error.
Note
This is not the same as #65505. That ticket is about catch ( Exception ) missing Error, and the patch there widens the catch to Throwable while leaving the return logic as is, so this behaviour survives it. The two fixes are independent and touch different lines.
Change History (4)
This ticket was mentioned in PR #12800 on WordPress/wordpress-develop by @bejignesh.
6 weeks ago
#1
- Keywords has-patch has-unit-tests added
@bejignesh commented on PR #12800:
4 weeks ago
#3
All three applied in 69fc18c.
On the first one, bool|WP_Error was never reachable, including before this patch. Every support check path returns a bool: the error state guard and the prevented prompt branch both return false, the success path returns what the wrapped builder returns, and the catch block returned $this before this change and returns false after it. Nothing in src/ calls is_supported() outside the class, so the annotation was wrong without anything depending on it.
The __call() docblock now reads "returns a WP_Error when a terminate method is called, or false when a support check method is called", and the class docblock notes that support checks return false rather than the error state instance.
phpcs and phpstan are clean and the ai-client group passes at 266 tests.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
WP_AI_Client_Prompt_Builder::__call()catches an exception from the wrapped SDK builder, stores it as the error state, and then only special cases the generating methods:is_supported()and the sevenis_supported_for_*()methods are not generating methods, so they fall through to the fluent return and give back the builder instance. It is truthy, so a support check that failed reads as a success:Everywhere else in the class this is handled. The error state guard at the top of
__call()returnsfalsefor support checks, and so does the prevented prompt path. Only an exception raised during the support check itself takes the wrong branch.## Reproducing it
ModalityEnum::document()is a documented factory, butPromptBuilder::inferCapabilityFromOutputModalities()has no branch for it and throws aRuntimeExceptionin its else. That call is outside the try blockisSupported()uses to swallowInvalidArgumentException, so it reaches the wrapper.Any throw from the wrapped builder during a support check behaves the same way. The document modality is just the shortest route to one.
## Fix
Return
falsefor support check methods from the catch block, so they match what they already return when the builder was in an error state beforehand. The error is still stored, so a later generating call still gets theWP_Error, and the test covers that.## Not a duplicate of #65505
#65505 is about the catch missing
Error. Its patch widenscatch ( Exception )tocatch ( Throwable )and leaves the return logic untouched, so a support check still comes back as the builder after it lands. The two changes are independent and touch different lines.## Testing
test_support_check_methods_return_false_when_builder_throws()fails on trunk with:and passes with the patch. The full
ai-clientgroup is green, 258 tests.phpcspasses on both changed files.## Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5
Used for: Reading through the AI client for places where the code disagrees with its own documented behaviour, which is how this turned up, and drafting this description. I confirmed the throw path in the bundled client myself, wrote and ran the test, checked it fails without the patch, ran
phpcs, and I take responsibility for the change.