From d285f996a2863250a12719b781d68cfbc6b20b05 Mon Sep 17 00:00:00 2001 From: Daniel La Rocque Date: Thu, 21 Aug 2025 10:21:47 -0400 Subject: [PATCH 1/2] fix(ai): Only include unexpected prediction element in error message. If the `responseJson` from the Predict request has an image, the entire image base64 will be included in the error message. This can make the error message >400kB (!!), which could cause performance issues in apps. I ran into this when testing https://github.com/firebase/firebase-js-sdk/pull/9216, since the `safetyAttributes` would be included in a response and trigger this error, but the `predictions` array in `responseJson` would have images. I noticed that when the sample app rendered the `error.message`, it began running into serious performance issues. --- .changeset/calm-guests-pump.md | 5 +++++ packages/ai/src/requests/response-helpers.ts | 4 +--- 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 .changeset/calm-guests-pump.md diff --git a/.changeset/calm-guests-pump.md b/.changeset/calm-guests-pump.md new file mode 100644 index 00000000000..da46e234f77 --- /dev/null +++ b/.changeset/calm-guests-pump.md @@ -0,0 +1,5 @@ +--- +'@firebase/ai': patch +--- + +Fixed an issue where `AIError` messages were too long after including an entire response body. diff --git a/packages/ai/src/requests/response-helpers.ts b/packages/ai/src/requests/response-helpers.ts index 2505b5c9276..4b15419f110 100644 --- a/packages/ai/src/requests/response-helpers.ts +++ b/packages/ai/src/requests/response-helpers.ts @@ -299,9 +299,7 @@ export async function handlePredictResponse< } else { throw new AIError( AIErrorCode.RESPONSE_ERROR, - `Predictions array in response has missing properties. Response: ${JSON.stringify( - responseJson - )}` + `Unexpected element in 'predictions' array in response: '${prediction}'` ); } } From 7ea255489cbe1908b66fa9a5098387b83e4591a7 Mon Sep 17 00:00:00 2001 From: Daniel La Rocque Date: Thu, 21 Aug 2025 10:36:53 -0400 Subject: [PATCH 2/2] stringify unexpected element in error message --- packages/ai/src/requests/response-helpers.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/ai/src/requests/response-helpers.ts b/packages/ai/src/requests/response-helpers.ts index 4b15419f110..fec9b371512 100644 --- a/packages/ai/src/requests/response-helpers.ts +++ b/packages/ai/src/requests/response-helpers.ts @@ -299,7 +299,9 @@ export async function handlePredictResponse< } else { throw new AIError( AIErrorCode.RESPONSE_ERROR, - `Unexpected element in 'predictions' array in response: '${prediction}'` + `Unexpected element in 'predictions' array in response: '${JSON.stringify( + prediction + )}'` ); } }