Make WordPress Core

Opened 7 months ago

Closed 7 months ago

#64867 closed task (blessed) (fixed)

Improve AI Client error handling

Reported by: flixos90 Owned by: flixos90
Priority: normal Milestone: 7.0
Component: AI Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

The WP_AI_Client_Prompt_Builder currently relies on very generic WP_Error objects, without a lot of context. This leads to two problems:

  • Much of the granular context that the PHP AI Client exceptions provide is lost.
  • The WP_Error objects are not very useful in HTTP context, e.g. in REST API responses.

We should enhance the logic in the class to leverage the PHP AI Client exception classes more granularly, and include a status in data for the HTTP status code, so that these WP_Error objects are ready to use as e.g. REST responses.

Change History (5)

This ticket was mentioned in ​PR #11265 on ​WordPress/wordpress-develop by ​@flixos90.


7 months ago
#1

  • Keywords has-patch has-unit-tests added; needs-patch needs-unit-tests removed

See the Trac ticket description for what this PR does.

Trac ticket: https://core.trac.wordpress.org/ticket/64867

## Use of AI Tools

Claude Opus was used to help with tests.

​gclapps0612-cmd commented on ​PR #11265:


7 months ago
#2

I did that to bring to your attention because I really need assistance. I
really wish that I could just come to CA, or where
And currently the office site class bells located. There has been a lot
more than facts stolen, with the right audience, my unique attributes,
drones, medical , cancer, and the stock markets insights. I'm sure
ivwould like to patient sp ideas either way

George Ralph

On Mon, Mar 16, 2026, 10:39 AM Felix Arntz *@*.*> wrote:

*@*. commented on this pull request.


In src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php
<​https://github.com/WordPress/wordpress-develop/pull/11265#discussion_r2940810222>
:

@@ -311,7 +310,7 @@ public function call( string $name, array $arguments ) {

'prompt_prevented',
( 'Prompt execution was prevented by a filter.' ),
array(

  • 'exception_class' => 'WP_AI_Client_Prompt_Prevented',

Note for reviewers: This class doesn't exist, it was pointless and
confusing to include this reference here just for consistency. Unlike the
other WP_Error objects, this one is not based on an exception.

—
Reply to this email directly, view it on GitHub
<​https://github.com/WordPress/wordpress-develop/pull/11265#pullrequestreview-3954340593>,
or unsubscribe
<​https://github.com/notifications/unsubscribe-auth/BXZWJXAOD6KCRGG2LP6AA2D4RAG2VAVCNFSM6AAAAACWTOOQMOVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZTSNJUGM2DANJZGM>
.
You are receiving this because you are subscribed to this thread.Message
ID: *@*.*>

​gclapps0612-cmd commented on ​PR #11265:


7 months ago
#3

I did that to bring to your attention because I really need assistance. I
really wish that I could just come to CA, or where
And currently the office site class bells located. There has been a lot
more than facts stolen, with the right audience, my unique attributes,
drones, medical , cancer, and the stock markets insights. I'm sure
ivwould like to patient sp ideas either way

George Ralph

On Mon, Mar 16, 2026, 10:39 AM Felix Arntz *@*.*> wrote:

*@*. commented on this pull request.


In src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php
<​https://github.com/WordPress/wordpress-develop/pull/11265#discussion_r2940810222>
:

@@ -311,7 +310,7 @@ public function call( string $name, array $arguments ) {

'prompt_prevented',
( 'Prompt execution was prevented by a filter.' ),
array(

  • 'exception_class' => 'WP_AI_Client_Prompt_Prevented',

Note for reviewers: This class doesn't exist, it was pointless and
confusing to include this reference here just for consistency. Unlike the
other WP_Error objects, this one is not based on an exception.

—
Reply to this email directly, view it on GitHub
<​https://github.com/WordPress/wordpress-develop/pull/11265#pullrequestreview-3954340593>,
or unsubscribe
<​https://github.com/notifications/unsubscribe-auth/BXZWJXAOD6KCRGG2LP6AA2D4RAG2VAVCNFSM6AAAAACWTOOQMOVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZTSNJUGM2DANJZGM>
.
You are receiving this because you are subscribed to this thread.Message
ID: *@*.*>

​@flixos90 commented on ​PR #11265:


7 months ago
#4

Thanks @gziolo!

The prompt_builder_error code is now only the fallback for unrecognized exceptions — a semantic shift worth noting explicitly, since any caller currently checking for prompt_builder_error to detect all builder failures will silently miss the new specific codes.

This shouldn't be a problem since it's very specific and we don't need to worry about backward compat with previous betas at this stage where 7.0 stable isn't out yet.

#5 @flixos90
7 months ago

  • Resolution → fixed
  • Status assigned → closed

In 62037:

AI: Improve AI Client error handling.

Prior to this changeset, the WordPress AI Client would handle every exception thrown in the PHP AI Client in the same way, losing meaningful nuance about different kinds of errors along the way.

Now, the WP_Error objects returned by the WordPress AI Client come with more helpful error codes, and they include semantic HTTP status codes as well so that they can be used in relevant contexts out of the box, like e.g. the REST API.

Props flixos90, gziolo.
Fixes #64867.
See #64591.

Note: See TracTickets for help on using tickets.