Skip to content

Commit 655fadd

Browse files
committed
use Gemini systemInstruction instead of fake user/model exchange
1 parent f3aee40 commit 655fadd

2 files changed

Lines changed: 25 additions & 20 deletions

File tree

threads/src/providers/google.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,6 @@ export const callGoogle = async (
2020
const apiKey = getApiKey(configApiKey);
2121

2222
const contents = [];
23-
24-
if (instructions) {
25-
contents.push({
26-
role: "user",
27-
parts: [{ text: instructions }],
28-
});
29-
contents.push({
30-
role: "model",
31-
parts: [{ text: "I understand." }],
32-
});
33-
}
34-
3523
const toolCallMap = new Map<string, string>();
3624

3725
for (let i = 0; i < ctx.history.length; i++) {
@@ -101,9 +89,13 @@ export const callGoogle = async (
10189
}
10290
}
10391

104-
const body: any = {
105-
contents,
106-
};
92+
const body: any = { contents };
93+
94+
if (instructions) {
95+
body.systemInstruction = {
96+
parts: [{ text: instructions }],
97+
};
98+
}
10799

108100
if (ctx.tools && ctx.tools.length > 0) {
109101
body.tools = [

threads/tests/google-provider.test.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ describe("Google provider tool call formatting", () => {
219219
expect(capturedBody.contents[0].parts[0].text).toBe("Do something");
220220
});
221221

222-
it("includes instructions as initial user/model exchange", async () => {
222+
it("passes instructions as systemInstruction", async () => {
223223
const ctx: ConversationContext = {
224224
history: [{ role: "user", content: "Hello" }],
225225
tools: [],
@@ -230,10 +230,23 @@ describe("Google provider tool call formatting", () => {
230230
ctx
231231
);
232232

233-
expect(capturedBody.contents).toHaveLength(3);
234-
expect(capturedBody.contents[0].parts[0].text).toBe("You are helpful");
235-
expect(capturedBody.contents[1].parts[0].text).toBe("I understand.");
236-
expect(capturedBody.contents[2].parts[0].text).toBe("Hello");
233+
expect(capturedBody.contents).toHaveLength(1);
234+
expect(capturedBody.contents[0].parts[0].text).toBe("Hello");
235+
expect(capturedBody.systemInstruction).toEqual({
236+
parts: [{ text: "You are helpful" }],
237+
});
238+
});
239+
240+
it("omits systemInstruction when no instructions provided", async () => {
241+
const ctx: ConversationContext = {
242+
history: [{ role: "user", content: "Hello" }],
243+
tools: [],
244+
};
245+
246+
await callGoogle({ model: "gemini-2.0-flash" }, ctx);
247+
248+
expect(capturedBody.systemInstruction).toBeUndefined();
249+
expect(capturedBody.contents).toHaveLength(1);
237250
});
238251

239252
it("streaming: formats tool results correctly", async () => {

0 commit comments

Comments
 (0)