Skip to content

Commit c5f08a9

Browse files
authored
fix(ai): Add 'includeSafetyAttributes' to Predict request payloads (#9216)
1 parent 84b8bed commit c5f08a9

File tree

5 files changed

+21
-1
lines changed

5 files changed

+21
-1
lines changed

.changeset/good-cooks-deliver.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/ai': patch
3+
---
4+
5+
Add 'includeSafetyAttributes' field to Predict request payloads.

packages/ai/src/requests/request-helpers.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ describe('request formatting methods', () => {
214214
expect(body.instances[0].prompt).to.equal(prompt);
215215
expect(body.parameters.sampleCount).to.equal(1);
216216
expect(body.parameters.includeRaiReason).to.be.true;
217+
expect(body.parameters.includeSafetyAttributes).to.be.true;
217218

218219
// Parameters without default values should be undefined
219220
expect(body.parameters.storageUri).to.be.undefined;
@@ -258,6 +259,7 @@ describe('request formatting methods', () => {
258259
personGeneration: safetySettings.personFilterLevel,
259260
aspectRatio,
260261
includeRaiReason: true,
262+
includeSafetyAttributes: true,
261263
storageUri: undefined
262264
});
263265
});

packages/ai/src/requests/request-helpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ export function createPredictRequestBody(
156156
addWatermark,
157157
safetyFilterLevel,
158158
personGeneration: personFilterLevel,
159-
includeRaiReason: true
159+
includeRaiReason: true,
160+
includeSafetyAttributes: true
160161
}
161162
};
162163
return body;

packages/ai/src/requests/response-helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ export async function handlePredictResponse<
295295
mimeType: prediction.mimeType,
296296
gcsURI: prediction.gcsUri
297297
} as T);
298+
} else if (prediction.safetyAttributes) {
299+
// Ignore safetyAttributes "prediction" to avoid throwing an error below.
298300
} else {
299301
throw new AIError(
300302
AIErrorCode.RESPONSE_ERROR,

packages/ai/src/types/imagen/internal.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ export interface ImagenResponseInternal {
6161
* The reason why the image was filtered.
6262
*/
6363
raiFilteredReason?: string;
64+
/**
65+
* The safety attributes.
66+
*
67+
* This type is currently unused in the SDK. It is sent back because our requests set
68+
* `includeSafetyAttributes`. This property is currently only used to avoid throwing an error
69+
* when encountering this unsupported prediction type.
70+
*/
71+
safetyAttributes?: unknown;
6472
}>;
6573
}
6674

@@ -84,6 +92,7 @@ export interface ImagenResponseInternal {
8492
* "personGeneration": "allow_all",
8593
* "sampleCount": 2,
8694
* "includeRaiReason": true,
95+
* "includeSafetyAttributes": true,
8796
* "aspectRatio": "9:16"
8897
* }
8998
* }
@@ -111,6 +120,7 @@ export interface PredictRequestBody {
111120
safetyFilterLevel?: string;
112121
personGeneration?: string; // Maps to personFilterLevel
113122
includeRaiReason: boolean;
123+
includeSafetyAttributes: boolean;
114124
};
115125
}
116126

0 commit comments

Comments
 (0)