Fix: Wrap Gemini function response arrays in object to prevent API error (#2745)

This commit is contained in:
Ayush Singh
2026-01-12 13:34:31 +00:00
committed by GitHub
parent e535652fa9
commit 11a97a413b
2 changed files with 19 additions and 1 deletions
@@ -77,7 +77,7 @@ function apiContentToGeminiContent(content: (LanguageModelTextPart | LanguageMod
// Handle case with text content (may also have images)
try {
responsePayload = JSON.parse(textContent);
if (typeof responsePayload !== 'object' || responsePayload === null) {
if (typeof responsePayload !== 'object' || responsePayload === null || Array.isArray(responsePayload)) {
responsePayload = { result: responsePayload };
}
} catch {
@@ -102,6 +102,24 @@ describe('GeminiMessageConverter', () => {
expect(fr.functionResponse.response).toEqual({ foo: 'bar' });
});
it('should wrap array responses in an object', () => {
const toolResult = new LanguageModelToolResultPart('listRepos_12345', [new LanguageModelTextPart('["repo1", "repo2"]')]);
const messages: LanguageModelChatMessage[] = [
{
role: LanguageModelChatMessageRole.Assistant,
content: [toolResult],
name: undefined
}
];
const result = apiMessageToGeminiMessage(messages);
expect(result.contents).toHaveLength(1);
expect(result.contents[0].role).toBe('user');
const fr: any = result.contents[0].parts![0];
expect(fr.functionResponse.response).toEqual({ result: ['repo1', 'repo2'] });
});
it('should be idempotent when called multiple times (no duplication)', () => {
const toolResult = new LanguageModelToolResultPart('doThing_12345', [new LMText('{"value":42}')]);
const messages: LanguageModelChatMessage[] = [