-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix: Wrap Gemini function response arrays in object to prevent API error #2745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'] }); | ||
| }); | ||
|
Comment on lines
+105
to
+121
|
||
|
|
||
| it('should be idempotent when called multiple times (no duplication)', () => { | ||
| const toolResult = new LanguageModelToolResultPart('doThing_12345', [new LMText('{"value":42}')]); | ||
| const messages: LanguageModelChatMessage[] = [ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a comment explaining why arrays (and other primitive types) need to be wrapped in an object. This would help future maintainers understand the Gemini API requirement. For example:
// Wrap non-object values (arrays, primitives, null) in an object because
// Gemini's FunctionResponse expects response to be a google.protobuf.Struct (JSON Object)
This provides context about the API constraint that necessitates this wrapping behavior.