Skip to content

Commit f52da94

Browse files
authored
fix(api): correct safetySettings payload structure (#61)
* fix(api): correct safetySettings payload structure The Gemini API expects the 'safetySettings' object to be a top-level property within the main request body, not nested inside 'generationConfig'. This commit moves the 'safetySettings' object to the correct location in the stream request payload to resolve the 400 Bad Request error. * refactor(api): optimize safetySettings creation * fix: added default KV namespace ID
1 parent 9518368 commit f52da94

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/gemini-client.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,7 @@ export class GeminiApiClient {
349349
modelId,
350350
req,
351351
isRealThinkingEnabled,
352-
includeReasoning,
353-
this.env
352+
includeReasoning
354353
);
355354

356355
const { tools, toolConfig } = GenerationConfigValidator.createValidateTools(req);
@@ -362,7 +361,17 @@ export class GeminiApiClient {
362361
needsThinkingClose = streamThinkingAsContent; // Only need to close if we streamed as content
363362
}
364363

365-
const streamRequest = {
364+
const streamRequest: {
365+
model: string;
366+
project: string;
367+
request: {
368+
contents: unknown;
369+
generationConfig: unknown;
370+
tools: unknown;
371+
toolConfig: unknown;
372+
safetySettings?: unknown;
373+
};
374+
} = {
366375
model: modelId,
367376
project: projectId,
368377
request: {
@@ -373,6 +382,11 @@ export class GeminiApiClient {
373382
}
374383
};
375384

385+
const safetySettings = GenerationConfigValidator.createSafetySettings(this.env);
386+
if (safetySettings.length > 0) {
387+
streamRequest.request.safetySettings = safetySettings;
388+
}
389+
376390
yield* this.performStreamRequest(
377391
streamRequest,
378392
needsThinkingClose,

src/helpers/generation-config-validator.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ export class GenerationConfigValidator {
126126
modelId: string,
127127
options: Partial<ChatCompletionRequest> = {},
128128
isRealThinkingEnabled: boolean,
129-
includeReasoning: boolean,
130-
env?: Env
129+
includeReasoning: boolean
131130
): Record<string, unknown> {
132131
const generationConfig: Record<string, unknown> = {
133132
temperature: options.temperature ?? DEFAULT_TEMPERATURE,
@@ -143,14 +142,6 @@ export class GenerationConfigValidator {
143142
generationConfig.responseMimeType = "application/json";
144143
}
145144

146-
// Add safety settings if environment variables are provided
147-
if (env) {
148-
const safetySettings = this.createSafetySettings(env);
149-
if (safetySettings.length > 0) {
150-
generationConfig.safetySettings = safetySettings;
151-
}
152-
}
153-
154145
const modelInfo = geminiCliModels[modelId];
155146
const isThinkingModel = modelInfo?.thinking || false;
156147

0 commit comments

Comments
 (0)